0
#include<iostream>
#include<string>
#include<vector>
using namespace std;

int main()
{
    string a;
    vector<string> b;
    vector< vector<string> > s;
    while(cin>>a)  {
        b.push_back(a);
        s.push_back(b);

        cout<<s.size()<<endl;//A1
        cout<<s[s.size()-1]<<endl;//A2
    }

    return 0;
}

Any problems with this? If line A2 is deleleted,the program can run.So I think s does exist,but when I add line A2 hoping to see the last element of vector of s ,Cannot run! My IDE:codeblocks(svn build rev 4977 (2008-03-29 18:01:31) gcc 4.2.1 Windows/unicode)
Somebody give a hand? Thanks.

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by mitrmkar
0

That will not work because the elements in 's' are vector<string> vectors themselves,
ostream operator << has no support for that kind of thing by default.
If you really want to pass e.g. a vector<string> to cout, you can write;

ostream & operator <<(ostream & o, const vector<string> & v)
{
	for(vector<string>::const_iterator it = v.begin(); it != v.end(); ++ it)
	{
		o << *it;
	}

	return o;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.