-
Notifications
You must be signed in to change notification settings - Fork 246
/
9.28.cpp
41 lines (33 loc) · 821 Bytes
/
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
#include <forward_list>
#include <string>
#include <iostream>
void insertAfterStr(std::forward_list<std::string> &fls,
const std::string &s1,
const std::string &s2) {
auto pre = fls.before_begin();
auto cur = fls.begin();
while (cur != fls.end()) {
if (*cur == s1) {
fls.insert_after(cur, s2);
return;
} else {
pre = cur++;
}
}
fls.insert_after(pre, s2);
}
int main() {
std::forward_list<std::string> fls{"s1", "s2", "s3"};
for (const auto &s : fls)
std::cout << s << " ";
std::cout << std::endl;
insertAfterStr(fls, "s2", "s4");
for (const auto &s : fls)
std::cout << s << " ";
std::cout << std::endl;
insertAfterStr(fls, "s5", "s6");
for (const auto &s : fls)
std::cout << s << " ";
std::cout << std::endl;
return 0;
}