Reputation: 351
I want to insert vector b
in a certain position in a vector a
. For Example
std::vector <int> vecta{ 10, 20, 30 ,40 , 50};
std::vector <int> vectb{ 1000, 2000, 3000 };
How can I have a result vector to be like {10,20,1000,2000,3000,40,50} ?
I want to remove 30
and replace its with the vector.
Upvotes: 3
Views: 3337
Reputation: 24738
I want to insert vector
b
in a certain position in a vectora
You may want to consider std::vector::insert()
:
vecta.insert(pos, vectb.begin(), vectb.end());
where pos
above is an iterator pointing to the element (in vecta
) before the content of vectb
will be inserted. pos
may be also the iterator returned by end()
, which would mean appending the content of vectb
to vecta
.
How can I have a result vector to be like {10,20,1000,2000,3000,40,50}
For that, you also need to remove the element 30
from vecta
. You can do this with std::vector::erase()
:
auto main() -> int {
std::vector <int> vecta{ 10, 20, 30, 40 , 50};
/* ^
|-- insert vectb here and replace the 30
*/
std::vector <int> vectb{ 1000, 2000, 3000 };
// what element to erase from vecta?
auto pos = vecta.begin() + 2;
// erase it
pos = vecta.erase(pos);
// insert vectb in vecta
vecta.insert(pos, vectb.begin(), vectb.end());
for (auto& e: vecta)
std::cout << e << " ";
std::cout << std::endl;
}
std::vector::erase()
returns the iterator that follows the removed element. Since you want to erase 30
from vecta
and then insert vectb
at that position, you can simply pass the iterator erase()
returns to insert()
.
Upvotes: 6
Reputation: 23802
You can use vector::insert
vecta.erase(vecta.begin() + 2); //<-- erase 30 element
vecta.insert(vecta.begin() + 2, vectb.begin(), vectb.end());
vecta.begin() + 2
being the point of insertion of vectb
in vecta
and vectb.begin(), vectb.end()
the span of the elements of vectb
you need to insert, In this case, all of them.
Upvotes: 3
Reputation: 117298
You can use erase
to erase an element at a certain iterator position and insert
to insert elements before a certain iterator position. To step an iterator, use std::next
and std::prev
.
#include <iostream>
#include <iterator>
#include <vector>
int main() {
std::vector<int> vecta{10, 20, 30, 40, 50};
std::vector<int> vectb{1000, 2000, 3000};
// erase 30 from the vector
vecta.erase(std::next(vecta.begin(), 2));
// insert vectb before position 2
vecta.insert(std::next(vecta.begin(), 2), vectb.begin(), vectb.end());
for(int v : vecta) {
std::cout << v << ' ';
}
std::cout << '\n';
}
Output:
10 20 1000 2000 3000 40 50
An alternative that doesn't require erase
. This is probably slightly faster:
#include <utility> // added for std::swap
int main() {
std::vector<int> vecta{10, 20, 30, 40, 50};
std::vector<int> vectb{1000, 2000, 3000};
std::vector<int> result;
// reserve space for the number of elements you know will be in the resulting vector
result.reserve(vecta.size() - 1 + vectb.size());
// append the 2 first elements from vecta
result.insert(result.end(), vecta.begin(), std::next(vecta.begin(), 2));
// append vectb
result.insert(result.end(), vectb.begin(), vectb.end());
// append the last two elements from vecta
result.insert(result.end(), std::prev(vecta.end(), 2), vecta.end());
// let vecta take over the data in result and vice-a-versa
std::swap(result, vecta);
}
Upvotes: 3