std::sort      对vector成员进行排序;

std::sort(v.begin(),v.end(),compare);

 
std::lower_bound 在排序的vector中进行二分查找,查找第一大于等于;
std::lower_bound(v.begin(),v.end(),v.element_type_obj,compare);
 
std::upper_bound 在排序的vector中进行二分查找,查找第一个大于;
std::upper_bound(v.begin(),v.end(),v.element_type_obj,compare);
 
std::binary_search 在排序的vector中进行二分查找;
std::binary_search(v.begin(),v.end(),v.element_type_obj,compare);
 
std::unique
 将排序的vector
vector<element_type>::iterator iter = std::unique(v.begin(),v.end(),compare);
v.resize(iter-v.begin());
 
std::for_each 将迭代器指向区间的内容调用指定的函数
void show(element_type& obj);
std::for_each(v.begin(),v.end(),show);
这里也可以:
strcut showclass
{
       void  operator ()(element_type& obj){
                 //具体操作......
       }
}showobj;
std::for_each(v.begin(),v.end(),showobj);
 
std::random_shuffle将迭代器指向的内容内容打散
std::random_shuffle(v.begin(),v.end());
或:
// random generator function:
ptrdiff_t myrandom (ptrdiff_t i) { return rand()%i;}
// pointer object to it:
ptrdiff_t (*p_myrandom)(ptrdiff_t) = myrandom;
random_shuffle ( myvector.begin(), myvector.end(), p_myrandom);
 
具体使用:
std::sort使用:
              std::sort(vectorobj.begin(),vectorobj.end(),compare_fun);
std::lower_bound 使用:
     std::lower_bound (vectorobj.begin(),vectorobj.end(),比较对象,compare_fun);
     比较对象的类型和vectorobj的成员类型一样。
示例:
#include <vector>
#include <algorithm>
#include <algorithm>
#include <iostream>
 
struct Student
{
    int age;
};
 
//sort和lower_bound使用比较函数的原型:
//boo function(const vector的成员类型 lit,const vector的成员类型 rit);
bool CompareOperation(const Student& lit,const Student& rit)     
{
    if(lit.age<rit.age){
        return true;
    }else{
        return false;
    }
}
 
int main(int argc,char *argv[])
{
    std::vector<Student> stuvec;
    Student a;
    a.age = 5;
    stuvec.push_back(a);
    Student b;
    b.age = 6;
    stuvec.push_back(b);
    Student c;
    c.age = 4;
    stuvec.push_back(c);
    Student d;
    d.age = 7;
    stuvec.push_back(d);
    std::cout<<"befort sort"<<std::endl;
    for(size_t index=0;index<stuvec.size();index++){
        std::cout<<stuvec[index].age<<std::endl;
    }
    std::sort(stuvec.begin(),stuvec.end(),CompareOperation);
    std::cout<<"after sort"<<std::endl;
    for(size_t index=0;index<stuvec.size();index++){
        std::cout<<stuvec[index].age<<std::endl;
    }
    std::cout<<"binary search"<<std::endl;
    std::vector<Student>::iterator iter = 
        std::lower_bound(stuvec.begin(),stuvec.end(),b,CompareOperation);
    if(iter != stuvec.end()){
        std::cout<<iter->age<<std::endl;
    }
    return 0;
}
运行结果:
stl算法库参看:
示例代码:
// lower_bound/upper_bound example
#include <iostream>     // std::cout
#include <algorithm>    // std::lower_bound, std::upper_bound, std::sort
#include <vector>       // std::vector
 
int main () {
  int myints[] = {10,20,30,30,20,10,10,20};
  std::vector<int> v(myints,myints+8);           // 10 20 30 30 20 10 10 20
 
  std::sort (v.begin(), v.end());                // 10 10 10 20 20 20 30 30
 
  std::vector<int>::iterator low,up;
  low=std::lower_bound (v.begin(), v.end(), 20); // 第一个   >= 20 的元素的迭代器
  up= std::upper_bound (v.begin(), v.end(), 20); // 第一个   > 20  的元素的迭代器                  
 
  std::cout << "lower_bound at position " << (low- v.begin()) << '\n';
  std::cout << "upper_bound at position " << (up - v.begin()) << '\n';
 
  return 0;
}
 
使用std::unique可以将有序的vector中的成员去重:
#include <iostream>     // std::cout
#include <algorithm>    // std::lower_bound, std::upper_bound, std::sort,std::unique, std::for_each
#include <vector>       // std::vector
 
void show(const int& i)
{
    std::cout<<i<<std::endl;
}
 
int main () {
    int myints[] = {10,20,30,30,20,10,10,20};
    std::vector<int> v(myints,myints+8);           // 10 20 30 30 20 10 10 20
 
    std::sort (v.begin(), v.end());                // 10 10 10 20 20 20 30 30
    std::vector<int>::iterator iter = std::unique(v.begin(),v.end());
    v.resize(iter - v.begin());
    std::for_each(v.begin(),v.end(),show);
 
    return 0;
}
使用std::binary_search对vector进行排序;
// binary_search example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; bool myfunction (int i,int j) { return (i<j); } int main () {
int myints[] = {1,2,3,4,5,4,3,2,1};
vector<int> v(myints,myints+9); // 1 2 3 4 5 4 3 2 1 // using default comparison:
sort (v.begin(), v.end()); cout << "looking for a 3... ";
if (binary_search (v.begin(), v.end(), 3))
cout << "found!\n"; else cout << "not found.\n"; // using myfunction as comp:
sort (v.begin(), v.end(), myfunction); cout << "looking for a 6... ";
if (binary_search (v.begin(), v.end(), 6, myfunction))
cout << "found!\n"; else cout << "not found.\n"; return 0;
}
参看:http://www.cplusplus.com/reference/algorithm/binary_search/?kw=binary_search
 

std::random_shuffle将指定的迭代器区间的内容随机打散:

// random_shuffle example
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std; // random generator function:
ptrdiff_t myrandom (ptrdiff_t i) { return rand()%i;} // pointer object to it:
ptrdiff_t (*p_myrandom)(ptrdiff_t) = myrandom; int main () {
srand ( unsigned ( time (NULL) ) );
vector<int> myvector;
vector<int>::iterator it; // set some values:
for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 // using built-in random generator:
random_shuffle ( myvector.begin(), myvector.end() ); // using myrandom:
random_shuffle ( myvector.begin(), myvector.end(), p_myrandom); // print out content:
cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it; cout << endl; return 0;
}

C++算法库学习__std::sort__对 vector进行排序_排序后就可以进行使用std::lower_bound进行二分查找(查找第一个大于等于指定值的迭代器的位置)__std::unique的更多相关文章

  1. mahout算法库(四)

    mahout算法库 分为三大块 1.聚类算法 2.协同过滤算法(一般用于推荐) 协同过滤算法也可以称为推荐算法!!! 3.分类算法 算法类 算法名 中文名 分类算法               Log ...

  2. C++ algorithm算法库

    C++ algorithm算法库 Xun 标准模板库(STL)中定义了很多的常用算法,这些算法主要定义在<algorithm>中.编程时,只需要在文件中加入#include<algo ...

  3. 安装Python算法库

    安装Python算法库 主要包括用NumPy和SciPy来处理数据,用Matplotlib来实现数据可视化.为了适应处理大规模数据的需求,python在此基础上开发了Scikit-Learn机器学习算 ...

  4. scikit-learn 线性回归算法库小结

    scikit-learn对于线性回归提供了比较多的类库,这些类库都可以用来做线性回归分析,本文就对这些类库的使用做一个总结,重点讲述这些线性回归算法库的不同和各自的使用场景. 线性回归的目的是要得到输 ...

  5. dlib库学习之一

    dlib库学习之一 1.介绍 跨平台 C++ 通用库 Dlib 发布 ,带来了一些新特性,包括概率 CKY 解析器,使用批量同步并行计算模型来创建应用的工具,新增两个聚合算法:中国低语 (Chines ...

  6. muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制

    目录 muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制 eventfd的使用 eventfd系统函数 使用示例 EventLoop对eventfd的封装 工作时序 runInLoo ...

  7. muduo网络库学习笔记(三)TimerQueue定时器队列

    目录 muduo网络库学习笔记(三)TimerQueue定时器队列 Linux中的时间函数 timerfd简单使用介绍 timerfd示例 muduo中对timerfd的封装 TimerQueue的结 ...

  8. Effective STL 学习笔记: 多用 vector & string

    Effective STL 学习笔记: 多用 vector & string 如果可能的话, 尽量避免自己去写动态分配的数组,转而使用 vector 和 string . 原书作者唯一想到的一 ...

  9. GDI+学习笔记(九)带插件的排序算法演示器(MFC中的GDI+实例)

    带插件的排序算法演示器 请尊重本人的工作成果,转载请留言.并说明转载地址,谢谢. 地址例如以下: http://blog.csdn.net/fukainankai/article/details/27 ...

随机推荐

  1. Windows 环境下运用Python制作网络爬虫

    import webbrowser as web import time import os i = 0 MAXNUM = 1 while i <= MAXNUM: web.open_new_t ...

  2. Android处理日期

    近期做一个项目,后台返回的日期是RFC3339格式的.之前没有看到过,当中遇到了几个问题以及解决 1.2015-11-18T14:49:55Z转换 在SimpleDateFormat中给出了几种格式 ...

  3. YTU 2899: D-险恶逃生 I

    2899: D-险恶逃生 I 时间限制: 1 Sec  内存限制: 128 MB 提交: 130  解决: 55 题目描述 Koha被邪恶的巫师困在一个m*n的矩阵当中,他被放在了矩阵的最左上角坐标( ...

  4. 【Poj 1330】Nearest Common Ancestors

    http://poj.org/problem?id=1330 题目意思就是T组树求两点LCA. 这个可以离线DFS(Tarjan)-----具体参考 O(Tn) 0ms 还有其他在线O(Tnlogn) ...

  5. bzoj4870

    http://www.lydsy.com/JudgeOnline/problem.php?id=4870 矩阵快速幂... 人话题意:从nk个物品里选模k余r个物品,问方案数模P 那么我们有方程 f[ ...

  6. Springboot拦截器线上代码失效

    今天想测试下线上代码,能否正常的执行未登录的拦截.所以把拦截器的代码给开放出来,但是没想到线上代码addInerceptors(InterceptorRegistry registry) 这个方法一直 ...

  7. 学习http协议的三次握手和四次挥手 ~~笔记

    http协议是基于tcp协议的  所以应该说是tcp协议的三次握手和四次挥手 SYN:请求建立连接,并在其序列号的字段进行序列号的初始值设定.建立连接,设置为1 FIN:用来释放一个连接.FIN=1表 ...

  8. P2973 [USACO10HOL]赶小猪

    跟那个某省省选题(具体忘了)游走差不多... 把边搞到点上然后按套路Gauss即可 貌似有人说卡精度,$eps≤1e-13$,然而我$1e-12$也可以过... 代码: #include<cst ...

  9. $CF19A\ World\ Football\ Cup$

    炒鸡\(6\)批的模拟题. 注意的是输入 把握好空格 大小写. 根据题目的这句话来排序 积分榜是按照以下原则制作的:胜利一个队得3分,平分1分,失败0分. 首先,球队按积分顺序排在积分榜上,分数相等比 ...

  10. 状态压缩+枚举 UVA 11464 Even Parity

    题目传送门 /* 题意:求最少改变多少个0成1,使得每一个元素四周的和为偶数 状态压缩+枚举:枚举第一行的所有可能(1<<n),下一行完全能够由上一行递推出来,b数组保存该位置需要填什么 ...