Sign in to follow this  
riruilo

How can I join two std::vectors?

Recommended Posts

Hi dudes! Let's say I have std::vector<CWhatever*> v1; and std::vector<CWhatever*> v2; How can I join them? I mean, is there any easy way or a method that do that? (I already can do that using 2 fors) Thanks for replies.

Share this post


Link to post
Share on other sites
Using a container of raw pointers suggests a design problem. Consider using one of the boost ptr_containers (boost::ptr_vector in this case), or a container of smart pointers (e.g. std::vector<boost::shared_ptr> - note that std::auto_ptr is *not* suitable for storing in a standard library container; if you have a good implementation, it should produce a compiler error instead of undefined behaviour). The exact choice depends on why you are using pointers currently. (It may even be the case that an ordinary vector of instances is the correct solution.)

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
If by join, you mean insert the contents of one to the end of the other you can use: v1.insert(v1.end(), v2.begin(), v2.end()).


OK, I'll use that. I guess I can use something like that using a third vector.

Thanks.

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