自定义函数使用map排序
2007-11-15 14:40
357 查看
#pragma warning(disable:4786)
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
/* 从小到大排序 */
struct ltstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};
typedef map<const char*, int,ltstr> MYDATAMAP;
typedef MYDATAMAP::iterator Itermydata;
int main(int argc, char* argv[])
{
MYDATAMAP mydata;
mydata.insert(Itermydata::value_type("3",9));
mydata.insert(Itermydata::value_type("4",9));
mydata.insert(Itermydata::value_type("1",9));
mydata.insert(Itermydata::value_type("2",9));
Itermydata begin = mydata.begin();
for(; begin != mydata.end(); ++begin)
{
cout<<"Key: " << begin->first
<<" Value: " << begin->second <<endl;
}
mydata.clear();
return 0;
}
结果输出为:
Key: 1 Value: 9
Key: 2 Value: 9
Key: 3 Value: 9
Key: 4 Value: 9
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
/* 从小到大排序 */
struct ltstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};
typedef map<const char*, int,ltstr> MYDATAMAP;
typedef MYDATAMAP::iterator Itermydata;
int main(int argc, char* argv[])
{
MYDATAMAP mydata;
mydata.insert(Itermydata::value_type("3",9));
mydata.insert(Itermydata::value_type("4",9));
mydata.insert(Itermydata::value_type("1",9));
mydata.insert(Itermydata::value_type("2",9));
Itermydata begin = mydata.begin();
for(; begin != mydata.end(); ++begin)
{
cout<<"Key: " << begin->first
<<" Value: " << begin->second <<endl;
}
mydata.clear();
return 0;
}
结果输出为:
Key: 1 Value: 9
Key: 2 Value: 9
Key: 3 Value: 9
Key: 4 Value: 9
相关文章推荐
- c++ 自定义排序函数的 map
- 自定义函数模板和泛型排序函数的使用
- python中map、reduce、filter、自定义排序函数、装饰器函数和偏函数
- PHP usort 使用用户自定义的比较函数对数组中的值进行排序
- 使用sort 和自定义比较函数对任意成员变量排序
- c++中优先队列的使用(自定义排序函数)
- PHP 使用用户自定义的比较函数对数组中的值进行排序
- usort — 使用用户自定义的比较函数对数组中的值进行排序
- <STL初探>list节点为自定义类型,如何访问节点中变量和函数,如何使用list自带的排序
- PHP 使用用户自定义的比较函数对数组元素排序
- php array_map使用自定义的函数处理数组中的每个值
- c++ 自定义排序函数的 map
- php usort 使用用户自定义的比较函数对二维数组中的值进行排序
- usort( )函数—使用用户自定义的比较函数对数组中的值进行排序
- Python 函数式编程(高阶函数、把函数作为参数、map()函数、reduce()函数、filter()函数、自定义排序函数、函数返回函数、闭包、匿名函数、装饰器decorator)
- 密码 stl函数的应用 注意map的使用 2012-10-7
- React study(四)Mixin编写使用、函数复用、自定义表单组件
- STL中sort、priority_queue、map、set的自定义比较函数
- 使用 {$INCLUDE} 或 {$I} 指令管理和调用自定义函数
- 【转】在c++中qsort()排序函数的使用qsort函数应用大全