Interestingly, what would be the right way to replace (rewrite) part of a given std :: vector "input" with another smaller std :: vector? I try to keep the rest of the original vector unchanged. Also I donβt need to worry about what was in the original vector and I donβt need to keep the smaller vector later.
Say I have this:
std::vector<int> input = { 0, 0, 1, 1, 2, 22, 3, 33, 99 }; std::vector<int> a = { 1, 2, 3 }; std::vector<int> b = { 4, 5, 6, 7, 8 };
And I want to achieve this:
input = { 1, 2, 3, 4, 5, 6, 7, 8, 99}
What is the right way to do this? I thought of something like
input.replace(input.beginn(), input.beginn()+a.size(), a);
// intermediate input will look like this: input = {1, 2, 3, 1, 2, 22, 3, 33, 99};
input.replace(input.beginn()+a.size(), input.beginn()+a.size()+b.size(), b);
There must be a standard way to do this, right? My thoughts on this are still the following:
- I can not use std :: vector :: assign for it destroys all input elements
std :: vector :: push_back will not replace, but increase input -> not what I want
std :: vector :: insert also creates new elements and updates the input vector, but I know for sure that the vectors a.size () + b.size () <= input.size ()
- std :: vector :: swap will not work, since there is some input content that should remain there (in the example, the last element) will also not work, to add b this way
- std :: vector :: emplace also increments input.size β as well
I would also prefer that the solution does not lose performance due to unnecessary cleanups or writing values ββto vectors a or b. My vectors will be very large for real, and this is ultimately about performance.
Any competent help would be greatly appreciated.