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);
Printable View
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());
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;
}
(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.
wat is OP ??
Ok Daved, you stumped me, where is append located? I tried a simple example, but append isn't recognized.Quote:
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;
}