Skip to main content
gamedev.net
🔒 Locked

Adding std vectors

Started by daniel_i_l 2 replies 350+ views
Original Post
daniel_i_l
daniel_i_l
If I have 2 std vectors - v1 and v2. How do i combine them into ine vector - v3? Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
ToohrVyk
ToohrVyk
std::vector v3(v1);v3.insert(v3.end(), v2.begin(), v2.end());
daniel_i_l
daniel_i_l
Thanks, and if I wanted instead to put v2 in the begining of v1 (for example if v1 was (3,4,5) and v2 was (0,1,2) and i wanted v1 to be (0,1,2,3,4,5) )
then would I do:
v1.insert(v1.begin(), v2.begin(), v2.end());
?
Thanks.
EDIT: and i have another question, how do i add something to the begining of a vector (instead of push_back that adds to the end)?
Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
ToohrVyk
ToohrVyk
Yes. Although, depending on the relative sizes of v1 and v2, it might be better to append v1 to v2 and then swap them (that is, if you don't care about what becomes of v2).

If you need to do this operation often, you might want to look at std::list.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.