C++ forward_list的简单使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/A13155283231/article/details/81737187
#include <iostream>
#include <forward_list>
using namespace std;

/*******************
std单链表的间隔插入
因为链表的判断结束逻辑是判断next是否为空
所以末尾也会插入一个0
*******************/

void main() {
    forward_list<int> l = { 1, 2, 3, 4, 5 };

    for (auto i = l.begin(); i != l.end(); i++)
    {
        i = l.insert_after(i, 0);
    }

    for (auto i = l.begin(); i != l.end(); i++)
    {
        cout << *i << endl;
    }

    system("pause");
}

输出

1
0
2
0
3
0
4
0
5
0
请按任意键继续. . .

猜你喜欢

转载自blog.csdn.net/A13155283231/article/details/81737187