STL之仿函数排序(1)
来源:互联网 发布:人力资源涉及的数据 编辑:程序博客网 时间:2024/04/29 14:31
在逐步深入对STL库的研究过程中,接触到很多参数是函数指针形式。例如在for_each,sort等算法中,对内置类型可以直接使用。当我们想要以自己的方式去实现时,又改如何去做呢?
首先,来看看其内部的源码与说明。
1.for_each内部等效于
template
// for_each example#include <iostream> // std::cout#include <algorithm> // std::for_each#include <vector> // std::vectorvoid myfunction (int i) { // function: std::cout << ' ' << i;}struct myclass { // function object type: void operator() (int i) {std::cout << ' ' << i;}} myobject;int main () { std::vector<int> myvector; myvector.push_back(10); myvector.push_back(20); myvector.push_back(30); std::cout << "myvector contains:"; for_each (myvector.begin(), myvector.end(), myfunction); std::cout << '\n'; // or: std::cout << "myvector contains:"; for_each (myvector.begin(), myvector.end(), myobject); std::cout << '\n'; return 0;}Output:myvector contains: 10 20 30myvector contains: 10 20 30
再重复一次for_each()关键点:
对于函数,要保持返回值和传参类型一致。
对于类,要对操作符()进行重载。
说完了for_each,来进行下一个挑战,sort()函数。sort()函数是STL提供给我们使用的,对于关系型容器,底层是基于红黑树实现的,不需要使用sort,但对于序列式容器。对于内置类型,sort的使用方法原理也差不多,不同的参数的个数,比较需要两个参数。先给出一个内置类型的例子。
// sort algorithm example#include <iostream> // std::cout#include <algorithm> // std::sort#include <vector> // std::vectorbool 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;}
有读者就可能会想,如果要实现自定义的比较方式,比如以类或结构体的某个成员作为比较对象,又该如何做呢?聪明的读者应该可能已经想到,对于类,只需要对<操作符进行重载;对于函数对象,重新实现比较方式就可以了。
#include <iostream>#include <algorithm>#include <functional>#include <vector>using namespace std;class myclass { public: myclass(int a, int b):first(a), second(b){} int first; int second; bool operator < (const myclass &m)const { return first < m.first; }};bool less_second(const myclass & m1, const myclass & m2) { return m1.second < m2.second;}int main() { vector< myclass > vect; for(int i = 0 ; i < 10 ; i ++){ myclass my(10-i, i*3); vect.push_back(my); } for(int i = 0 ; i < vect.size(); i ++) cout<<"("<<vect[i].first<<","<<vect[i].second<<")\n"; sort(vect.begin(), vect.end()); cout<<"after sorted by first:"<<endl; for(int i = 0 ; i < vect.size(); i ++) cout<<"("<<vect[i].first<<","<<vect[i].second<<")\n"; cout<<"after sorted by second:"<<endl; sort(vect.begin(), vect.end(), less_second); for(int i = 0 ; i < vect.size(); i ++) cout<<"("<<vect[i].first<<","<<vect[i].second<<")\n"; return 0 ;}
参考文献:
1. http://www.cppblog.com/changshoumeng/articles/114379.html
2. http://www.cplusplus.com/
0 0
- STL之仿函数排序(1)
- STL之仿函数
- STL之仿函数
- STL之仿函数
- STL之仿函数
- STL学习之路之仿函数
- STL之仿函数,适配器简介
- STL源码剖析之仿函数
- STL之仿函数,配接器实现
- C++ STL之仿函数
- STL 源码剖析笔记之仿函数
- 第七章STL之仿函数
- STL---函数对象(仿函数)概述
- 8 STL仿函数(函数对象)
- stl排序之sort函数
- stl排序之sort函数
- stl排序之sort函数
- stl排序之sort函数
- 80端口复用:利用haproxy把http流量和ssh流量分别转发到web服务器和ssh服务器
- Android SDK Manager无法下载问题
- struts2开篇(一)
- 技术博客分店开张
- 动物王国的面向对象
- STL之仿函数排序(1)
- Android常见问题总结(二)
- gradle for android 自动找当前环境当前最高版本的 sdk 和 build tools
- 注册表文件的位置
- 快速排序
- Android应用的界面编程(一)
- TCP处理第一次连接断开,再次连接处理
- Android之Notification的多种用法
- 糊涂的教授 计算几何+拓扑排序