zoukankan      html  css  js  c++  java
  • c/c++ 标准顺序容器 容器的访问,删除 操作

    c/c++ 标准顺序容器 容器的访问,删除 操作

    pop_front:vector,string不支持

    pop_back:forward_list不支持

    知识点

    1,front, back, at 成员函数的使用,对应代码里的test1

    2,删除最后一个元素pop_back, 删除第一个元素pop_front,对应代码里的test2

    3,删除指定位置的元素erase,并返回下一位置的迭代器 ,对应代码里的test3

    #include <vector>
    #include <string>
    #include <list>
    #include <forward_list>
    #include <deque>
    
    using namespace std;
    
    int main(){
      //test1 顺序容器的front, back(forward_list没有back),at成员函数                
      //front返回容器里的头元素;back返回容器里的最后一个元素                       
      /*                                                                            
      //在解引用一个迭代器或调用front,back之前要检查容器里是否有元素               
      deque<int> li{1,2,3,4};                                                       
      if(!li.empty()){                                                              
        auto val = *li.begin();                                                     
        val = 9;//不会改变容器里头元素的值                                          
        cout << li.front() << endl;                                                 
        auto val2 = li.front();                                                     
        val2 = 8;//不会改变容器里头元素的值                                         
        cout << li.front() << endl;                                                 
        auto& val3 = *li.begin();                                                   
        val3 = 9;//会改变容器里头元素的值                                           
        cout << li.front() << endl;                                                 
        auto& val4 = li.front();                                                    
        val4 = 8;//会改变容器里头元素的值                                           
        cout << li.front() << endl;                                                 
                                                                                    
        auto val5 = li.end();                                                        
        //得到容器的最后一个元素                                                    
        auto last = *(--val5);                                                      
        cout << last << endl;                                                       
                                                                                    
        auto v = li.at(1);                                                          
        auto v1 = li[2];                                                            
        cout << v << " " << v1 << endl;                                             
      }                                                                             
      */
    
      //test2 pop_back, pop_front                                                   
      //pop_back:删除容器的最后一个元素;pop_front:删除容器的第一个元素             
      /*                                                                            
      list<int> li{1,2,3,4};                                                        
      if(!li.empty()){                                                              
        li.pop_back();                                                              
        cout << li.back() << endl;                                                  
        li.pop_front();                                                             
        cout << li.front() << endl;                                                 
      }                                                                             
      */
    
      //test3 erase                                                                 
      //删除指定位置的元素,并放回指向删除元素的下一个元素的迭代器                  
      vector<int> li{0,1,2,3,4,5,6,7,8};
      //删除奇数                                                                    
      auto b = li.begin();
      while(b != li.end()){
        if(*b % 2){
          b = li.erase(b);
        }
        else{
          ++b;
        }
      }
      for(auto const &s : li){
        cout << s << " ";
      }
      cout << endl;
    
      auto it = li.begin();
      li.erase(it, it + 2);
      for(auto const &s : li){
        cout << s << " ";
      }
      cout << endl;
    
    }
    

    c/c++ 学习互助QQ群:877684253

    本人微信:xiaoshitou5854

  • 相关阅读:
    Do You See Me? Ethical Considerations of the Homeless
    ELDER HOMELESSNESS WHY IS THIS AN ISSUE?
    Endoflife support is lacking for homeless people
    html内联框架
    html字体
    html块 div span
    html列表
    html表格
    SQL Server管理员专用连接的使用   作为一名DBA,经常会处理一些比较棘手的服务无响应问题,鉴于事态的严重性,多数DBA可能直接用“重启”大法,以便尽快的恢复生产环境的正常运转,但是多数情况
    如何配置最大工作线程数 (SQL Server Management Studio)
  • 原文地址:https://www.cnblogs.com/xiaoshiwang/p/9643807.html
Copyright © 2011-2022 走看看