How to insert an array into at the beginning 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 };
    int arr[]= { 100, 200, 300, 400 };

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



/*
run:

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

*/

 



answered Apr 11, 2020 by avibootz

Related questions

1 answer 197 views
1 answer 148 views
1 answer 258 views
2 answers 228 views
1 answer 160 views
1 answer 186 views
...