#include<iostream>
#include<forward_list>
using namespace std;
int main() {
forward_list<int> a{ 1,2,3,4,5,6,7,8 };
a.insert_after(a.before_begin(), 0);
auto prec = a.before_begin();
auto curr = a.begin();
//遍历到最后一项,添加新元素在末尾
while (1) {
prec = curr;
curr++;
if (curr == a.end())
break;
}
a.insert_after(prec, 9);
for (auto it = a.begin(); it != a.end(); it++)
cout << *it << endl;
}
输出:
0
1
2
3
4
5
6
7
8
9