How to initialize a list with array values 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;
    int arr[]= { 5, 2, 3, 7, 6, 9 };
    
    l.assign(arr, arr + 6);      

    printList(l);
    
    return 0;
}



/*
run:

5 2 3 7 6 9 

*/

 



answered Apr 11, 2020 by avibootz

Related questions

1 answer 136 views
1 answer 154 views
1 answer 177 views
1 answer 146 views
1 answer 129 views
...