zoukankan      html  css  js  c++  java
  • c++ list sort方法

       1:  #include <list>
       2:  #include <string>
       3:  #include <functional>
       4:  #include <iostream>
       5:   
       6:  struct S {
       7:           std::string firstname;
       8:           std::string secondname;
       9:           int ID;
      10:           // 重新定义小于,因为默认的sort函数调用的操作符是<,所以我们只需要重载 < 就好了    
      11:          bool operator < (S & b) { 
      12:              return ID < b.ID;
      13:           }
      14:   
      15:  };
      16:  int main(int argc, char* argv[])
      17:  { 
      18:             std::list<S> mylist; 
      19:             std::list<S>::iterator iter; 
      20:             S a; 
      21:             a.firstname ="dfadf"; 
      22:             a.ID = 5; 
      23:             mylist.push_back (a);    
      24:            a.firstname ="得到"; 
      25:             a.ID = 9; 
      26:             mylist.push_back (a);   
      27:             a.firstname ="xxx"; 
      28:             a.ID = 7;  
      29:            mylist.push_back (a);  
      30:            a.firstname ="gggg"; 
      31:            a.ID = 25; 
      32:             mylist.push_back (a); 
      33:              mylist.sort();
      34:            // 现在默认的operator已经被我们重载了,就不用管,直接调用sort就好了 
      35:             for (iter = mylist.begin(); iter != mylist.end();++iter)  
      36:             {     
      37:                       std::cout <<static_cast<S>(*iter).ID << "\t"; 
      38:              } 
      39:             std::cout <<std::endl;      
      40:             return 0;
      41:   }
  • 相关阅读:
    SpringCloud----分布式事务
    ps----基础
    DVWA靶场之XSS(Stored)通关
    DVWA靶场之XSS(Reflected)通关
    DVWA靶场之SQL injection(blind)通关
    CentOS6与CentOS7的几点区别
    DVWA靶场之SQL Injection通关
    DVWA靶场之File Upload(文件上传)通关
    DVWA靶场之File Inclusion(文件包含)通关
    DVWA靶场之CSRF(跨站请求伪造)通关
  • 原文地址:https://www.cnblogs.com/dorothychai/p/2549373.html
Copyright © 2011-2022 走看看