#include <iostream>
#include <forward_list>
#include <string>
#include <vector>
using namespace std;
void f(forward_list<string> &, string &, string &);
int main(int argc, char** argv)
{
forward_list<string> Myflst{ "C" ,"C","is","an","student"};
string s1 = "an", s2 = "excellent";
f(Myflst, s1, s2);
forward_list<string>::iterator it1 = Myflst.begin();
for (it1; it1 != Myflst.end(); ++it1)
{
cout << *it1 << " ";
}
return 0;
cout << endl;
}
void f(forward_list<string> &flst, string &s1, string &s2)
{
forward_list<string>::iterator prev = flst.before_begin();
forward_list<string>::iterator curr = flst.begin();
if (!flst.empty())
{
//查找第一个string是否在链表中
while (curr != flst.end())
{
if (*curr == s1)
{
flst.insert_after(curr, s2);
return;
}
else
{
prev = curr;
++curr;
}
}
flst.insert_after(prev, s2);
}
else
{
cout << "该链表为空" << endl;
}
}
《C++ Prime》9.28
最新推荐文章于 2023-10-24 19:10:57 发布