How to insert multiple copies of the same element at a given position in a vector with C++

1 Answer

0 votes
#include <iostream>
#include <vector>

int main() {
    std::vector<int> v = {10, 20, 30, 40};

    // Insert three copies of 86 at position 2
    v.insert(v.begin() + 2, 3, 86);

    for (int x : v) {
        std::cout << x << " ";
    }
}



/*
run:

10 20 86 86 86 30 40 

*/

 



answered Dec 13, 2025 by avibootz
...