forward_list中查找str1并将str2插到后面
Practice 9.28
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include <iterator>
#include <cmath>
#include <cstring>
#include <forward_list>
using namespace std ;
void print(forward_list<string > flst) {
for (auto i : flst) {
cout << i << " " ;
}
cout << endl;
}
void find_str1_insert_str2(forward_list<string > flst, string str1, string str2) {
auto cur = flst.begin();
while (1 ) {
if (cur == flst.end()) {
cout << "Find nothing!" << endl;
return ;
}
if (*cur == str1) {
cur = flst.insert_after(cur, str2);
break ;
}
cur++;
}
cout << "Done!" << endl;
print(flst);
return ;
}
int main() {
forward_list<string > flst = {"jin" , "hai" , "jia" };
cout << "test1: find hai insert ding" << endl;
find_str1_insert_str2(flst, "hai" , "ding" );
cout << "test2: find hai insert hai" << endl;
find_str1_insert_str2(flst, "hai" , "hai" );
cout << "test3: find wang insert meng" << endl;
find_str1_insert_str2(flst, "wang" , "meng" );
cout << "test4: find jia insert ding" << endl;
find_str1_insert_str2(flst, "jia" , "ding" );
return 0 ;
}
Output
test1: find hai insert ding
Done!
jin hai ding jia
test2: find hai insert hai
Done!
jin hai hai jia
test3: find wang insert meng
Find nothing!
test4: find jia insert ding
Done!
jin hai jia ding
Circumstances? I make circumstances!