How to insert an array into a specific position of a list in C++

1 Answer

0 votes
#include <iostream>
#include <list>

void printList(std::list<int> const &l) {
    for (auto const &n: l) {
        std::cout << n << " ";
    }
}

int main() {
    std::list<int> l = { 5, 2, 7, 1, 9, 3, 6, 4 };
    std::list<int>::iterator it;
    int arr[]= { 100, 200, 300, 400 };
    int N = 3;
    
    it = l.begin();
    for (int i = 0; i < N; i++) it++;
    

    l.insert(it, arr, arr + 6);      
    
    printList(l);
    
    return 0;
}



/*
run:

5 2 7 100 200 300 400 9 3 1 9 3 6 4 

*/

 



answered Apr 11, 2020 by avibootz

Related questions

2 answers 251 views
1 answer 193 views
1 answer 176 views
1 answer 182 views
1 answer 233 views
1 answer 165 views
...