• 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
    ----------------------------------------
    请按任意键继续. . .

  • 相关阅读:
    nios sgdma(Scatter-Gather dma)示例
    关于nios 中printf 的问题
    Nios II 系统时钟timestamp的应用
    DMA在FPGA的应用之我见
    PIO Core
    VGA接口时序约束
    时序分析,重中之重,柳暗花明又一村 搞定美女了问题
    深入浅出VGA和DVI接口
    基于FPGA的VGA可移植模块终极设计
    理解FPGA中的RAM、ROM和CAM;ROM、RAM、DRAM、SRAM、FLASH
  • 原文地址:https://www.cnblogs.com/herd/p/11010878.html
Copyright © 2020-2023  润新知