can't push back non-int 2D vectors

I'm attempting to push_back several two-dimensional string vectors. However, I receive several error messages when I do. Here is an example of trying to push_back a string vector:

1
2
3
vector<vector<string> > myVector;
myVector.push_back(vector<string>());
myVector[0].push_back(1);


produces

error: invalid conversion from 'int' to 'const char*' [-fpermissive]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.tcc:214:5: error:   initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _All
oc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' [-fpermissive]


I'm also pushing back several int vectors and a bool vector (all 2D) but it works fine with those. What's the problem with 2D string vectors?
Thanks!

P.S. My debugger seems to think the problem is on the last line of code.
Last edited on
You're pushing an int, while the container is for a string. Just push it like that:

myVector[0].push_back("1");

and it should work.
Last edited on
1 is not a string so you can't store it in the string-vector.
I thought the value in the () of push_back was the number of spaces you push it back. It's the value you add to the end??
Yes, it is the actual value that gets copied to the end.
Thanks, ha ha. I've never used vectors before but I'm glad I'm finally taking the time to learn how.
If you want to create and fill a 2D vector with default values (say, zeroes), you can do it with the fill constructor:

1
2
// same as: m[ 4 ][ 3 ] = {{ 0 }};
vector <vector <int> > m( 4, vector <int> ( 3, 0 ) );
Topic archived. No new replies allowed.