changaney 2017-05-04 09:58 采纳率: 50%
浏览 835
已采纳

怎么用c++vectors实现查找排序

c++实现查找排序图片

  • 写回答

2条回答 默认 最新

  • SHA-ZI 2017-05-05 05:27
    关注

    最简单的方法其实是使用头下的算法

     // 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 << '\n';
    
      return 0;
    }
    

    algorithm

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件