题目为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);
}
}