#include <iostream> #include <forward_list> #include <iterator> template <class T> void display(T it, T end) { while(it != end) { std::cout<<*it<<" "; std::advance(it, 1); } std::cout<<std::endl; } int main(int argc, char *argv[]) { std::forward_list<int> l = {1, 2, 3, 4}; std::forward_list<int> l2(10, 1); std::forward_list<int> l3(l.begin(), l.end()); std::forward_list<int> l4(l3); display(l4.begin(), l4.end()); //auto it = l.end(); //std::advance(it, -1);//error, forward_list has no backward link return 0; }
#include <iostream> #include <forward_list> #include <iterator> template <class T> void display(T it, T end) { while(it != end) { std::cout<<*it<<" "; std::advance(it, 1); } std::cout<<std::endl; } int main(int argc, char *argv[]) { std::forward_list<int> l = {1, 2, 3, 4}; //push_front l.push_front(5);//5 1 2 3 4 //pop_front l.pop_front();//1 2 3 4 //insert_after auto it = l.begin(); std::advance(it, 1); l.insert_after(it, 10);//1 2 10 3 4 //erase_after it = l.begin(); std::advance(it, 2);//1 2 10 4 l.erase_after(it); //remove l.remove(10);//1 2 4 //remove_if l.remove_if([](int n){if(n%2 == 1) return n;});//2 4 //reverse l.reverse();//4 2 display(l.begin(), l.end()); return 0; }