merging a vector of vectors of int and a vector of int

Mar 21, 2011 at 10:15pm
Hello

I am a beginner in C++ and I am having trouble merging and sorting 'the vector of vectors of integers' and 'vector of integers'.

I have,

(1) std::vector<std::vector<int> > vector_collection;
(2) std::vector<int> vector;
and I want to make
(3) std::vector<std::vector<int> > merged; out of (1) and (2)

I wish to sort the vector (2) in increasing int value and also sort each element of (1), that is, each vector element of vector_collection in increasing value of int.

Then, I want to merge (1) and (2) and sort it with respect to the first element of each vector entry.

I will be very grateful if anyone can help me with this.

Thank you in advance
Last edited on Mar 23, 2011 at 7:15pm
Mar 21, 2011 at 10:43pm
http://www.cplusplus.com/reference/algorithm/sort/

To sort integers, simple sort will be sufficient.
To 'merge' just push_back the new vector and then to sort you'll have to use the sort function with Compare argument.
Mar 23, 2011 at 7:00pm
Ok thank you, I have trouble doing push back integers from a std::vector<int> into a std::vector<std::vector<int> >, can I actually add elements of integers type to a std::vector<std::vector<int> >??

Here is what I am doing

std::vector<std::vector<int> > vector_collection;
std::vector<int> vector; // a vector containing integers say 0, 1, 2, 3, 4

for(int i=0; i<vector.size(); i++){
vector_collection.push_back(i);
}

and the error I get is

error: no matching function for call to ‘std::vector<std::vector<int> >::push_back(int&)’

Any help will be appreciated a lot

Thanks
Mar 23, 2011 at 7:22pm
into a vector of vectors you can only push vectors, not bare integers.
Mar 23, 2011 at 7:54pm
Oh... Thank you. It works now

I did

std::vector<std::vector<int> > vector_collection;
std::vector<int> vector; // a vector containing integers say 0, 1, 2, 3, 4

for(int i=0; i<vector.size(); i++){
std::vector<int> temp;
temp.push_back(i);
vector_collection.push_back(temp);
}

Thank you very much :)
Topic archived. No new replies allowed.