0
点赞
收藏
分享

微信扫一扫

C++——forward_list的插入操作

若如初梘 2022-02-11 阅读 50
c++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;
}
举报

相关推荐

0 条评论