编程语言
首页 > 编程语言> > C++——forward_list的插入操作

C++——forward_list的插入操作

作者:互联网

#include <iostream>
#include <vector>
#include <algorithm>
#include <list>
#include <forward_list>
using namespace std;
//查找s1,并在其后面添s2,没有找到在后面添加
//别忘了fls传递引用
void func(forward_list<string> & fls,const string & s1, const string & s2)
{
    auto prve = fls.before_begin();
    auto now = fls.begin();
    bool flag = false;
    while(now!=fls.end())
    {
        if(*now == s1)
        {
            flag = true;
            now = fls.insert_after(now,s2);
        }
        prve = now;
        ++now;
    }
    if(!flag)
    {
        fls.insert_after(prve,s2);
    }
}
int main()
{
    const string s1 = "hello";
    const string s2 = "world";
    forward_list<string> fls = {"helloq","nihao","ward","shijie","helloq","helloq"};
    func(fls,s1,s2);
    for(auto i : fls) cout << i << endl;
    return 0;
}

标签:include,const,s2,fls,list,C++,forward,now,s1
来源: https://blog.csdn.net/JAN6055/article/details/122877504