Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace an element into a specific position of a vector

I want to replace an element into a specific position of a vector, can I just use an assignment:

// vec1 and 2 have the same length & filled in somehow
vec1;
vec2;

vec1[i] = vec2[i] // insert vec2[i] at position i of vec1

or I have to use insert():

vector<sometype>::iterator iterator = vec1.begin();

vec1.insert(iterator+(i+1), vec2[i]);
like image 334
daiyue Avatar asked Jul 17 '11 21:07

daiyue


People also ask

How do you change the position of an element in a vector?

You can do that using at. You can try out the following simple example: const size_t N = 20; std::vector<int> vec(N); try { vec.at(N - 1) = 7; } catch (std::out_of_range ex) { std::cout << ex. what() << std::endl; } assert(vec.at(N - 1) == 7);

How do you change an element of a vector in C++?

The best option to conditionally replace values in a vector in C++ is using the std::replace_if function. It assigns a new value to all the elements in the specified range for which the provided predicate holds true .

Which method is used to add elements at vector specific location?

Which of these methods is used to add elements in vector at specific location? Explanation: addElement() is used to add data in the vector, to obtain the data we use elementAt() and to first and last element we use firstElement() and lastElement() respectively.

How do you remove an element from a position in a vector?

vector::erase() erase() function is used to remove elements from a container from the specified position or range.


3 Answers

vec1[i] = vec2[i] 

will set the value of vec1[i] to the value of vec2[i]. Nothing is inserted. Your second approach is almost correct. Instead of +i+1 you need just +i

v1.insert(v1.begin()+i, v2[i]) 
like image 79
Armen Tsirunyan Avatar answered Oct 13 '22 07:10

Armen Tsirunyan


You can do that using at. You can try out the following simple example:

const size_t N = 20; std::vector<int> vec(N); try {     vec.at(N - 1) = 7; } catch (std::out_of_range ex) {     std::cout << ex.what() << std::endl; } assert(vec.at(N - 1) == 7); 

Notice that method at returns an allocator_type::reference, which is that case is a int&. Using at is equivalent to assigning values like vec[i]=....


There is a difference between at and insert as it can be understood with the following example:

const size_t N = 8; std::vector<int> vec(N); for (size_t i = 0; i<5; i++){     vec[i] = i + 1; }  vec.insert(vec.begin()+2, 10); 

If we now print out vec we will get:

1 2 10 3 4 5 0 0 0 

If, instead, we did vec.at(2) = 10, or vec[2]=10, we would get

1 2 10 4 5 0 0 0 
like image 29
Pantelis Sopasakis Avatar answered Oct 13 '22 08:10

Pantelis Sopasakis


See an example here: http://www.cplusplus.com/reference/stl/vector/insert/ eg.:



...
vector::iterator iterator1;

  iterator1= vec1.begin();
  vec1.insert ( iterator1+i , vec2[i] );

// This means that at position "i" from the beginning it will insert the value from vec2 from position i

Your first approach was replacing the values from vec1[i] with the values from vec2[i]

like image 37
virlan2004 Avatar answered Oct 13 '22 07:10

virlan2004