Claud
Claud

Reputation: 1075

Is there any way to push back a vector into a vector? c++

For example if I had one or more 2D vectors and I just want to store them in a data base for example.

 vector<vector<double>> one2DVector;
 vector<vector<double>> two2DVector;

 //Obviously can't do that but this is what I want to do
 vector<vector> dataBase;

 dataBase.push_back(one2DVector);
 dataBase.push_back(one2DVector);

Upvotes: 0

Views: 4031

Answers (6)

Roger Dahl
Roger Dahl

Reputation: 15734

You have a vector of vector<double> but you're trying to push vector<vector>.

Upvotes: 0

Michael Burr
Michael Burr

Reputation: 340218

You should try making your dataBase a

vector< vector< vector <double> > >

instead of a vector<vector> (whatever that is).

Typedefs might help make this a bit more readable

typedef vector<vector<double> > vec_2D;

vec_2D one2DVector;
vec_2D two2DVector;

vector<vec_2D> dataBase;

Keep in mind that this dataBase will hold copies of the 2D vectors, which I'm guessing is not really what you want (but maybe it is). You might want to consider having your database be a container of pointers or container-appropriate smart pointers (like shared_ptr).

Upvotes: 1

antonio081014
antonio081014

Reputation: 3593

vector< vector< vector <double> > > database;
vector<vector<double>> one2DVector;
vector<vector<double>> two2DVector;
...
//insert elements into two-dimension arrays.
...
database.push_back(one2DVector);
database.push_back(two2DVector);

Upvotes: 3

Piotr Dobrogost
Piotr Dobrogost

Reputation: 42425

It seems like you want dataBase to be a vector of 2d vectors. If this is the case then you have to define it like so

vector<vector<vector<double> > > dataBase;

Upvotes: 1

111111
111111

Reputation: 16158

 vector<vector<double>> one2DVector;
 vector<vector<double>> two2DVector;

 //Obviously can't do that but this is what I want to do
 vector<vector<vector<double> > > dataBase; //HERES THE EDIT

 dataBase.push_back(one2DVector);
 dataBase.push_back(one2DVector);

IT should work fine

however if what you want to do is concatenate the vector then you can do.

vector<vector<double>>  database(one2DVector);
database.insert(database.end(), two2DVector.begin(), two2DVector.end());

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249183

You need to declare it like this:

vector<vector<vector<double>>> dataBase;

Note that if you using C++98 and not C++11, you need to put spaces between the > brackets.

Upvotes: 5

Related Questions