l练习9.28
#include
#include
#include
using namespace std;
void opps_func(forward_list
& ,string, string);//我们要使用的函数
int main(void)
{
forward_list
flist = { "Andrew Wiggins", "Jabari Parker", "Joel Embiid", "Aaron Gerdon", "Dante Exum" }; for (auto ss : flist) { cout << ss << endl; } cout << "*****************************" << endl;//作为分界线 opps_func(flist,"Jabari Parker","Julius Randle"); for (auto ss : flist) cout << ss << endl; cout << "******************************" << endl; opps_func(flist, "Marcus Smart", "Jordan Clacson"); for (auto ss : flist) cout << ss << endl; } void opps_func(forward_list
& flist, string s1, string s2) { auto cerr = flist.begin(); int detect = 0;//检测标志位(用惯了单片机,习惯了标志位) auto prev = flist.before_begin(); while (cerr != flist.end()) { if (*cerr == s1) // 如果有匹配的string { flist.insert_after(cerr, s2); detect = 1; } prev = cerr; ++cerr; } if (detect==0)//如果没有匹配的strinig { flist.insert_after(prev,s2); } }