Sign in to follow this  
m0ng00se

copy vector into vector [Resolved]

Recommended Posts

This is just a code snippet. I'm trying to copy one vector into another. Reason, the first vector is defined in a class and goes out of scope and gets destroyed so I want to copy it into a global vector before it is destructed.

typedef std::vector<std::string> vstring;


typedef struct {
	vstring inputs;
	vstring branches;
	vstring responses;
	vstring repeats;
} data;


typedef struct {
	vstring inputs2;
	vstring branches2;
	vstring responses2;
	vstring repeats2;
} data2;


std::vector<data> mybase1;
std::vector<data2> mybase2;


std::vector<data2> mybase2(mybase1); //doesn't work, should copy?

//next attempt

	int dbase_size = mybase1.size();
	for(int index = 0; index < dbase_size; ++index) {
	data2  temp = mybase1.at(index);  //doesn't work
	mybase2.push_back(temp);          //doesn't work
}


//Reason: cannot convert from 'std::vector<_Ty>' to 'const //std::allocator<_Ty>'






This is just two of my many attempts in the last few hours just to demonstrate that I have been trying to solve it myself. I think it's an issue with the first vector being in a class definition which makes it constant? Is that right? I can post all the referenced code if this isn't enough. Basically the only difference is that the first "struct" (data1) and first vector (mybase1) are declared in a class while the second is global at the top of the *.cpp file. m0ng00se [Edited by - m0ng00se on March 5, 2008 9:45:36 PM]

Share this post


Link to post
Share on other sites
1) std::vector::assign, one of the functions in <algorithm> or the constructor taking iterators will work.

2) Your bigger problem is that one vector is a vector<T>and the other a vector<U>. These are not related types. Why do you have two of essentially the same type? One of them is not required.

Share this post


Link to post
Share on other sites
Why do you have two different structs that are in effect, exactly the same (data and data2)?
This is the root of your problem...


If both vectors were using the same type of struct you could simply do:
std::vector<data> database2(database);
// or
std::vector<data> database2;
database2 = database;
// or even better:
std::vector<data> database2;
database2.swap( database );//this is a super-fast copy, as it just swaps the contents of the two vectors by exchanging root-pointers.

Share this post


Link to post
Share on other sites
While structurally identical, data and data2 are not the same type type. Thus vector<data> and vector<data2> are not the same type and cannot be copied. Provide a sane overloaded set of operators for data <-> data2 and something akin to the second method should work (only using assignment rather than push_back).

...or just use one type.

Share this post


Link to post
Share on other sites
whoa.... that is the problem alright...

but I thought they were identical? What am I missing?
Sorry but I thought they were the same. Can somebody please explain?

I just want to grab all the data in the first one before it goes out of scope and gets destructed, because I need to use it later elsewhere.

Does the fact that it's declared inside a class make it a different type? Sorry I'm a C programmer just learning C++ and I still get confused by classes.

Or is it something else entirely that I'm missing? I thought I'd created an identical vector to copy into. I don't need two different types. Do you mean data is a different type to data2 because they're two different user defined types? Should I have just used data in both?

Sorry guys, I'd really like to understand what I'm doing wrong so I don't do it again.

m0ng00se

Share this post


Link to post
Share on other sites
Quote:
Does the fact that it's declared inside a class make it a different type?

No, the place where the variable is declared has no effect on it's type.

Quote:
I thought I'd created an identical vector to copy into.

No, one is a vector of 'data's and one is a vector of 'data2's.

They look the same to a human, but all C++'s type system sees is that one is called 'data' and the other is called 'data2', hence, they're different types.

Quote:
Do you mean data is a different type to data2 because they're two different user defined types? Should I have just used data in both?

Yes.

Share this post


Link to post
Share on other sites
oops duh!!!

Thanx Hodgman, all fixed now with your help.
I took your advice and did the fast swap thing.
Grrr I was making it way more complicated than it needed to be.

m0ng00se

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this