zoukankan      html  css  js  c++  java
  • C++小知识之Vector排序

    // sort algorithm example
    #include <iostream>     // std::cout
    #include <algorithm>    // std::sort
    #include <vector>       // std::vector
     
    bool  myfunction ( int  i, int  j)
    {
         return  (i<j);
    }
     
    struct  myclass
    {
         bool  operator() ( int  i, int  j)
         {
             return  (i<j);
         }
    } myobject;
     
    int  main ()
    {
         int  myints[] = {32,71,12,45,26,80,53,33};
         std::vector< int > myvector (myints, myints+8);               // 32 71 12 45 26 80 53 33
     
         // using default comparison (operator <):
         std::sort (myvector.begin(), myvector.begin()+4);           //(12 32 45 71)26 80 53 33
     
         // using function as comp
         std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
     
         // using object as comp
         std::sort (myvector.begin(), myvector.end(), myobject);     //(12 26 32 33 45 53 71 80)
     
         // print out content:
         std::cout << "myvector contains:" ;
         for  (std::vector< int >::iterator it=myvector.begin(); it!=myvector.end(); ++it)
         {
             std::cout << ' '  << *it;
         }
         std::cout << ' ' ;
     
         return  0;
    }
  • 相关阅读:
    python爬虫慕课基础2
    python爬虫慕课基础1
    Xcode6中如何对scrollview进行自动布局(autolayout)
    iOS 图形编程总结
    iOS开发之数据存取
    自定义下拉刷新控件-CBStoreHouseRefreshControl
    'libxml/tree.h' file not found
    iPhone,iPad如何获取WIFI名称即SSID
    获得手机的ip
    一步一步教你做ios推送
  • 原文地址:https://www.cnblogs.com/riasky/p/3508787.html
Copyright © 2011-2022 走看看