STL sort使用
来源:互联网 发布:保定广电网络客服电话 编辑:程序博客网 时间:2024/06/06 16:04
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
typedef struct _NODE
{
int id;
int val;
_NODE(int id, int val): id(id), val(val){};
friend ostream& operator <<(ostream &os, const _NODE &node)
{
return os << node.val << " ";
}
}NODE;
typedef struct _ANY
{
int id;
int val;
_ANY(int id, int val): id(id), val(val){};
friend bool operator <(const _ANY &an, const _ANY &any)
{
return an.val < any.val;
}
friend ostream& operator <<(ostream &os, const _ANY &an)
{
return os << an.val << " ";
}
}ANY;
bool cmp(const NODE &nod, const NODE &node)
{
return nod.val < node.val;
}
class valCmp
{
public:
bool operator()(const NODE &nod, const NODE &node)
{
return nod.val < node.val;
}
};
int main(int argc, char *argv[])
{
vector<NODE> node;
vector<ANY> any;
for(int i=0; i<10; i++)
{
NODE temp(i, rand() % 100);
ANY tempA(i, rand() % 100);
node.push_back(temp);
any.push_back(tempA);
}
//sort(node.begin(), node.end(), cmp);
sort(node.begin(), node.end(), valCmp());
sort(any.begin(), any.end());
ostream_iterator<NODE> output(cout, " ");
copy(node.begin(), node.end(), output);
cout << endl;
ostream_iterator<ANY> output1(cout, " ");
copy(any.begin(), any.end(), output1);
cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
利用sort对自定义的结构进行排序的三种方式:
1.重载自定义结构的<操作符, 这种适合于按照特定的字段进行比较的情况...
2.写一个单独的cmp()函数...
3.写一个单独的类, 重载()操作符...
- STL 之sort使用
- STL 之sort使用
- STL sort使用
- STL sort的使用
- stl sort使用经验
- STL sort的使用
- STL sort使用及重载
- 使用STL中的泛型算法sort
- 使用stl sort函数的例子
- STL中sort()函数的使用
- STL中sort的使用 _Updated 150721
- list不能使用STL算法sort()
- STL Sort的五种使用示例
- STL SORT
- stl - sort
- STL sort
- STL:sort
- sort stl
- struts2中的OGNL
- 很久没有更新blog
- window移动应用开发
- MVC入门学习笔记(六)
- 解决 Notes Local Mail Replica Scanner v1.0 重复安装的问题
- STL sort使用
- 在VC++下和VS2005下建立openGL开发环境
- 清除记住密码的方法
- org.hibernate.exception.LockAcquisitionException
- CREATEMUTEX AND CREATEEVENT
- 寻求会员收银管理系统
- Windows核心编程学习笔记 第一部分 第一章 错误处理
- sql优化
- 一个WEB页面中代码执行次序