Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace a portion of a vector with another vector

Tags:

c++

I have a vector of size 20 and a second of size 5. I wish to replace elements 11-15 in the first vector with the second vector. I can do this by deleting those elements from the first vector and inserting the second vector. Is there another way to do this, perhaps by using assign?

like image 221
Richard Johnson Avatar asked Jul 15 '13 05:07

Richard Johnson


People also ask

How do you replace an element in a vector?

To replace an element in Java Vector, set() method of java. util. Vector class can be used.

Can you add a vector to another vector?

Is vector addition applicable to any two vectors? No, vector addition does not apply to any two vectors. Two vectors are added only when they are of the same type and nature. For instance, two velocity vectors can be added, but one velocity vector and one force vector cannot be added.


1 Answers

You can use std::copy:

#include <algorithm> // for std::copy

std::copy(src.begin(), src.end(), dst.begin()+10);

where src is the size 5 vector, and dst is the size 20 vector.

like image 158
juanchopanza Avatar answered Sep 19 '22 15:09

juanchopanza