forked from demon90s/CppStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise_9_28.cpp
45 lines (37 loc) · 1008 Bytes
/
exercise_9_28.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// 练习9.28:编写函数,接受一个forward_list<string>和两个string共三个参数。
// 函数应在链表中查找第一个string,并将第二个string插入到紧接着第一个string
// 之后的位置。如第一个string未在链表中,则将第二个string插入到链表末尾。
#include <iostream>
#include <forward_list>
#include <string>
using namespace std;
void Insert(forward_list<string> &flst, const string &find_str, const string &insert_str)
{
auto prev = flst.before_begin();
auto it = flst.begin();
while (it != flst.end()) {
if (*it == find_str) {
it = flst.insert_after(it, insert_str);
break;
}
else {
prev = it;
++it;
}
}
if (it == flst.end())
flst.insert_after(prev, insert_str);
}
int main()
{
forward_list<string> flst{"a", "b", "c", "d"};
Insert(flst, "c", "X");
for (const auto &s : flst)
cout << s << " ";
cout << endl;
Insert(flst, "M", "U");
for (const auto &s : flst)
cout << s << " ";
cout << endl;
return 0;
}