How to iterate through forward_list in C++

1 Answer

0 votes
#include <iostream>
#include <forward_list>

void print( std::forward_list<int> fl) {
    for (auto it = fl.begin(); it != fl.end(); ++it )
        std::cout << *it << ", ";
    
    std::cout << "\n";
}

int main() {
    std::forward_list<int> fl = {2, 5, 32, 9, 8, 7};

    print(fl);
}



/*
run:

2, 5, 32, 9, 8, 7, 

*/

 



answered Aug 2, 2020 by avibootz

Related questions

1 answer 79 views
79 views asked Dec 25, 2024 by avibootz
2 answers 158 views
158 views asked May 9, 2021 by avibootz
1 answer 151 views
1 answer 139 views
1 answer 112 views
112 views asked Jun 18, 2022 by avibootz
2 answers 224 views
1 answer 138 views
...