-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExer09_28.cpp
39 lines (39 loc) · 1.04 KB
/
Exer09_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
#include <iostream>
#include <forward_list>
#include <string>
using std::cout;
using std::endl;
using std::forward_list;
using std::string;
forward_list<string>::iterator insert_word(forward_list<string>& flst, const string& str1, const string& str2)
{
auto prev = flst.before_begin();
auto curr = flst.begin();
while(curr != flst.end())
{
if(*curr == str1)
{
curr = flst.insert_after(curr, str2);
return curr;
}
else
{
prev = curr;
++curr;
}
}
curr = flst.insert_after(prev, str2);
return curr;
}
int main()
{
forward_list<string> flst = { "The", "wrinkled", "sea", "beneath", "him", "crawls;\n",
"He", "from", "his", "mountain", "walls,\n",
"And", "like", "a", "thunderbolt", "he", "falls.\n" };
string word1 = "He", word2 = "watches";
insert_word(flst, word1, word2);
for(const auto &s : flst)
cout << s << " ";
cout << endl;
return 0;
}