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? }
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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());
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
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); }
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.