顺序容器删除元素 vector list deque

#include <iostream>
#include <list>
#include <algorithm>
#include <string>

using namespace std;

int main()
{
  list<string> slist;
  slist.push_back("A");
  slist.push_back("B");
  slist.push_back("C");
  slist.push_back("D");
  slist.push_back("E");
  slist.push_back("F");
  slist.push_back("G");

  list<string>::iterator iter;
  for (iter=slist.begin();iter != slist.end();++iter)
  {
    cout << *iter<< endl;
  }

  cout << "---------------------------------------" << endl;

  string s1("D");
  string s2("F");
  list<string>::iterator iter_start = find(slist.begin(),slist.end(),s1);
  list<string>::iterator iter_end = find(slist.begin(), slist.end(), s2);

  if (iter_start != slist.end() && iter_end != slist.end())
  {
    slist.erase(iter_start,iter_end);
  }

  cout << "---------------------------------------" << endl;
  list<string>::iterator iter2;
  for (iter2 = slist.begin(); iter2 != slist.end(); ++iter2)
  {
    cout << *iter2 << endl;
  }
  cout << "---------------------------------------" << endl;

 


  system("pause");
  return 0;
}

 

=======================================================

A
B
C
D
E
F
G
---------------------------------------
---------------------------------------
A
B
C
F
G
---------------------------------------
请按任意键继续. . .

 

posted @ 2019-06-09 15:49  西北逍遥  阅读(305)  评论(0编辑  收藏  举报