[Solved] Replacing a range between two iterators in C++


Using std::copy(), it can be done like this:

#include <iostream>
#include <vector>
#include <algorithm>

void printVector(const std::vector<int>& v) {
    bool first = true;
    std::cout << '{';
    for (int i : v) {
        if (!first) std::cout << ", ";
        std::cout << i;
        first = false;
    }
    std::cout << "}\n";
}

int main(void) {
    std::vector<int> v1 = {1, 2, 3, 4, 5, 6, 7};
    std::vector<int> v2 = {6, 6, 3, 5, 4, 13, 99};

    printVector(v1);
    printVector(v2);

    std::vector<int>::iterator dest_begin = v1.begin();
    std::vector<int>::iterator src_begin = std::next(v2.begin(), 1);
    std::vector<int>::iterator src_end = std::next(v2.begin(), 5);

    std::copy(src_begin, src_end, dest_begin);

    printVector(v1);

    return 0;
}

Output:

{1, 2, 3, 4, 5, 6, 7}
{6, 6, 3, 5, 4, 13, 99}
{6, 3, 5, 4, 5, 6, 7}

solved Replacing a range between two iterators in C++