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;
}

上一篇:Linux配置仓库可实现挂载


下一篇:PyTorch深度学习入门笔记(八)神经网络的基本骨架 nn.Module的使用