I am new with vectors and STL library. please tell where I'm going wrong?
Code:int t,i;
char ch;
char arr[400];
vector<char> exp;
stack<char> s;
cin>>t;
for(i=0;i<t;i++)
{
cin>>arr;
exp.push_back(arr); <<-- what's wrong here?
}
Printable View
I am new with vectors and STL library. please tell where I'm going wrong?
Code:int t,i;
char ch;
char arr[400];
vector<char> exp;
stack<char> s;
cin>>t;
for(i=0;i<t;i++)
{
cin>>arr;
exp.push_back(arr); <<-- what's wrong here?
}
You are trying to push back an array - you could do that if you have a vector that holds arrays (e.g vector<char [400]> I think the syntax of that would be). If you actually want your vector to hold each character as an individual element, then you need to loop through the array and push_back each element.
--
Mats
Any reason why you don't use a string for input? Then you could easily use the insert method to append characters from the string to the vector.
(insert would work with char array too, but you'd first need to find out where the null terminator is.)Code:string s;
vector<char> v;
v.insert(v.end(), s.begin(), s.end());
Personally, I'd use something like this, assuming I've correctly guessed what you wanted to do. :)
Code:vector<string> data;
string line;
while(getline(cin, line)) {
data.push_back(line);
}