C++ remove remove_if erase

#include <iostream>
#include <algorithm>
#include <list>
#include <vector>
#include <functional>


using namespace std;

int main()
{
  list<int> list1;
  for (int k=0;k<10;k++)
  {
    list1.push_back(k);
    list1.push_front(k);
  }

  list<int>::iterator list_iter1;
  for (list_iter1 = list1.begin();list_iter1 != list1.end();++list_iter1)
  {
    cout << *list_iter1 << " ";
  }
  cout << endl;
  cout << "list1 size: " << list1.size() << endl;
  cout << "----------------------------------------" << endl;

  list<int>::iterator list_iter_end;
  list_iter_end = remove(list1.begin(),list1.end(),3);
  for (list_iter1 = list1.begin(); list_iter1 != list1.end(); ++list_iter1)
  {
    cout << *list_iter1 << " ";
  }
  cout << endl;
  cout << "list1 size: " << list1.size() << endl;
  cout << "----------------------------------------" << endl;

  for (list_iter1 = list1.begin(); list_iter1 != list_iter_end; ++list_iter1)
  {
    cout << *list_iter1 << " ";
  }
  cout << endl;
  cout << "list1 size: " << list1.size() << endl;
  cout << "----------------------------------------" << endl;

  cout << "删除元素的个数:" <<distance(list_iter_end,list1.end())<< endl;

  //真正的删除
  list<int>::iterator list_iter3 = list1.begin();
  ++list_iter3;
  ++list_iter3;
  ++list_iter3;
  list1.erase(list1.begin(), list_iter3);

  for (list_iter1 = list1.begin(); list_iter1 != list1.end(); ++list_iter1)
  {
    cout << *list_iter1 << " ";
  }
  cout << endl;
  cout << "list1 size: " << list1.size() << endl;
  cout << "----------------------------------------" << endl;

  vector<int> vec1;
  for(int k=0;k<10;k++)
  {
    vec1.push_back(k);
  }

  for (int k =6; k<16; k++)
  {
    vec1.push_back(k);
  }

  vector<int>::iterator vec_iter1;
  for (vec_iter1 = vec1.begin();vec_iter1 != vec1.end();++vec_iter1)
  {
    cout << *vec_iter1 << " ";
  }
  cout << endl;
  cout << "vec1 size: " << vec1.size() << endl;
  cout << "----------------------------------------" << endl;

  vector<int>::iterator vec_iter2 = remove(vec1.begin(),vec1.end(),11);
  vec1.erase(vec_iter2, vec1.end());

  for (vec_iter1 = vec1.begin(); vec_iter1 != vec1.end(); ++vec_iter1)
  {
    cout << *vec_iter1 << " ";
  }
  cout << endl;
  cout << "vec1 size: " << vec1.size() << endl;
  cout << "----------------------------------------" << endl;

  vector<int>::iterator vec_iter4 = remove_if(vec1.begin(),vec1.end(),bind2nd(less<int>(),6));
  vec1.erase(vec_iter4,vec1.end());

  for (vec_iter1 = vec1.begin(); vec_iter1 != vec1.end(); ++vec_iter1)
  {
    cout << *vec_iter1 << " ";
  }
  cout << endl;
  cout << "vec1 size: " << vec1.size() << endl;
  cout << "----------------------------------------" << endl;


  system("pause");
  return 0;
}

 

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

9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9
list1 size: 20
----------------------------------------
9 8 7 6 5 4 2 1 0 0 1 2 4 5 6 7 8 9 8 9
list1 size: 20
----------------------------------------
9 8 7 6 5 4 2 1 0 0 1 2 4 5 6 7 8 9
list1 size: 20
----------------------------------------
删除元素的个数:2
6 5 4 2 1 0 0 1 2 4 5 6 7 8 9 8 9
list1 size: 17
----------------------------------------
0 1 2 3 4 5 6 7 8 9 6 7 8 9 10 11 12 13 14 15
vec1 size: 20
----------------------------------------
0 1 2 3 4 5 6 7 8 9 6 7 8 9 10 12 13 14 15
vec1 size: 19
----------------------------------------
6 7 8 9 6 7 8 9 10 12 13 14 15
vec1 size: 13
----------------------------------------
请按任意键继续. . .

 

 

posted @ 2019-06-12 17:14  西北逍遥  阅读(364)  评论(0编辑  收藏  举报