您的位置:首页 > 其它

例题:在链表中查找一个字符串,并插入另一个字符串

2017-03-17 11:40 197 查看
题目为C++ primer 9.28

#include <iostream> 

#include <string>

#include <forward_list>

using namespace std;

void Insert(forward_list<string> &lst, const string &a, const string &b);

int main()

{
forward_list<string> lst{ "nayeon","tzuyu","momo","taeyeon" };
string a = "nayeon";
string b = "2yeon";
Insert(lst, a, b);
for (auto it : lst)
cout << it << endl;
system("pause");
return 0;

}

void Insert(forward_list<string> &lst, const string &a, const string &b)

{
auto it = lst.begin();
auto it_begin = lst.before_begin();
while (it != lst.end())
{
if (*it == a)
{
lst.insert_after(it, b);
break;
}
it_begin++;
it++;
}
if (it == lst.end())
{

lst.insert_after(it_begin, b);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐