I am trying to store values like "01" and "10" into an array but the int array would take "01" as "1". Hence I decided to use string array.While declaring the string array, I initialized it with
string array[n] = {0};
On compiling the code it shows an error saying :
"terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_constructnull not valid"
While just changing the type of the array to integer, it works fine.
I tried to not initialize the
string array[n]={0};
The code then works fine!
Can we conclude that the string type array cannot overwrite the values already stored in the elements?
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
string arr[n] = {0};
for(int i=0; i<n; i++){
cin>>arr[i];
cout<<arr[i];
}
}
this is the output of the code:
1
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_constructnull not valid