How would you add a vector to the end of the collection of vectors of a class?
like..
trees is a class
Code:vector <trees> a; vector <trees> b; //now I want to add b to the vector a // Is it this?? it doesn't seem to work! a.push_back(b);
How would you add a vector to the end of the collection of vectors of a class?
like..
trees is a class
Code:vector <trees> a; vector <trees> b; //now I want to add b to the vector a // Is it this?? it doesn't seem to work! a.push_back(b);
Code:a.insert(a.end(), b.begin(), b.end());
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
simple :S
Code:#include<iostream> #include<vector> //You have this line missing using namespace std; int main() { vector <trees> a; a.push_back("Tree"); //You have the qoutation marks missing return 0; }
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<
(Ignore this line... it is incorrect). Instead of insert, you can use append which might be optimized for the task of inserting at the end.
Hussain Hani, it looks like the OP is trying to add all the values in one vector to the end of another.
Last edited by Daved; 10-18-2006 at 03:30 PM.
wat is OP ??
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<
Ok Daved, you stumped me, where is append located? I tried a simple example, but append isn't recognized.Originally Posted by Daved
>> wat is OP ??
Original Poster.
>> where is append located?
Apparently only in my head. I guess I was thinking of the string class.
Hmmm, do you mean something like a vector of a vector of trees? (edit: whoops, my example uses ints. Ah well. It brings accross the idea. )
Code:#include <iostream> #include <vector> int main( void ) { std::vector< int > vec_trees; std::vector< std::vector<int> > vec_vec_trees; // initialise vec_trees vec_vec_trees.push_back( vec_trees ); return 0; }