#include<iostream>
#include<string>
#include<list>
#include<deque>
using namespace std;
int main()
{
string tempValue;
string searchValue;
list<string> my_list;
int exist_tap = 0; //表示是否存在要删除的元素,默认为0不存在
cout <<"input string list, Ctrl+z to end" <<endl;
while(cin >>tempValue)
{
my_list.push_back(tempValue);
}
cin.clear();
cout <<"input string for searching and deleting" << endl;
cin>>searchValue;
list<string> ::iterator list_iter = my_list.begin();
while(list_iter != my_list.end())
{
if(searchValue == *list_iter)
{
list_iter = my_list.erase(list_iter);
exist_tap = 1;
if(list_iter != my_list.begin())
{
list_iter--; //迭代器减1,使其指向下一个元素
}
else
{
continue;//如果刚好是begin,则不能减1
}
}
list_iter++;
}
if(0 == exist_tap)
{
cout <<"there is no string "<<searchValue << " in the input"<< endl;
goto Pause;
}
cout <<"the new string is:" << endl;
for(list_iter = my_list.begin(); list_iter != my_list.end(); list_iter++)
{
cout << *list_iter <<" ";
}
cout<<endl;
Pause: system("pause");
return 0;
}