stl sort算法之研究
来源:互联网 发布:欧洲美国知乎 编辑:程序博客网 时间:2024/06/02 10:54
sudaobo sudaobo@21cn.com
2004-8-4
sort模板有两种:
---------------------------------------------------------------------
template<class RanIt>
void sort(RanIt fist, RanIt last);
template<class RanIt, class Pred>
void sort(RanIt fist, RanIt last, Pred pr);
---------------------------------------------------------------------
第一种模板,sort重排[first,last)之间的元素,产生一个按operate<排列的序列。sort将序列中的元素以升序方式排列。
第二种模板和前一个的行为相似,不过它用pr(X,Y)代替了operate<(x,y)。
例子:
------------------------------
// tmp1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <vector>
#include <algorithm> // Include algorithms
#include <iostream>
using namespace std;
bool pr(int s1, int s2)
{
return s1>s2;
}
int main(int argc, char* argv[])
{
vector<int> vec;
vector<int>::iterator i;
vec.push_back (10);
vec.push_back (3);
vec.push_back (7);
sort(vec.begin(), vec.end(),pr); // Sort the vector
for (i = vec.begin(); i != vec.end(); i++)
{
cout<<*i<<endl;
}
return 0;
}
例子2:
--------------------------------------------------------------------------------------
// tmp1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <vector>
#include <algorithm> // Include algorithms
#include <iostream>
#include <string>
using namespace std;
class myless
{
public:
bool operator()( const int &a, const int &b) {
return a < b;
}
};
int main(int argc, char* argv[])
{
vector<int> vec;
vector<int>::iterator i;
vec.push_back (10);
vec.push_back (3);
vec.push_back (7);
sort(vec.begin(), vec.end(), myless()); // Sort the vector
for (i = vec.begin(); i != vec.end(); i++)
{
cout<<*i<<endl;
}
return 0;
}
例子3:
------------------------------------------------------------------------
// tmp1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <vector>
#include <algorithm> // Include algorithms
#include <iostream>
#include <string>
using namespace std;
typedef struct
{
string first;
string last;
}NAME;
bool sortbyfirst(const NAME& n1, const NAME& n2)
{
return (n1.first<n2.first);
}
bool sortbylast(const NAME& n1, const NAME& n2)
{
return (n1.last<n2.last);
}
int main(int argc, char* argv[])
{
vector<NAME> contacts;
vector<NAME>::iterator j;
NAME tmp;
tmp.first = "liu";
tmp.last = "bei";
contacts.push_back(tmp);
tmp.first = "zhao";
tmp.last = "yun";
contacts.push_back(tmp);
tmp.first = "gun";
tmp.last = "yu";
contacts.push_back(tmp);
tmp.first = "zhang";
tmp.last = "fei";
contacts.push_back(tmp);
cout<<"by first:"<<endl;
sort(contacts.begin(), contacts.end(), sortbyfirst);
for(j=contacts.begin(); j!= contacts.end(); j++)
{
cout<<j->first<<" "<<j->last<<endl;
}
cout<<"by last:"<<endl;
sort(contacts.begin(), contacts.end(), sortbylast);
for(j=contacts.begin(); j!= contacts.end(); j++)
{
cout<<j->first<<" "<<j->last<<endl;
}
return 0;
}
- stl sort算法之研究
- stl sort算法之研究
- stl sort算法之研究[转载]
- STL算法之sort
- STL排序算法之sort()
- STL学习笔记之sort算法
- STL源码分析之 sort算法
- STL之排序算法SORT 详细介绍
- STL算法之sort和stable_sort
- STL Sort算法
- STL - sort排序算法
- STL中的Sort 算法
- 【STL】算法 — sort
- STL算法-sort
- STL list::sort算法
- STL源码:算法sort
- STL中的Sort算法
- STL的sort算法
- 驯服 Tiger: 线程中的默认异常处理
- 在ASP中操作数据库的方法
- IIS6中上传文件尺寸太小的解决办法
- 8088 汇编速查手册
- 利用汇编语言开发盗QQ密码程序
- stl sort算法之研究
- linux手机媒体播放器软件
- rman入门实践!
- 客户端配置文件
- 浅谈数据库设计技巧(下).txt
- Java常见问题集锦
- 在Win2003下寻找“失踪”的移动硬盘
- postgreSQL
- vim