64,683
社区成员
发帖
与我相关
我的任务
分享
//from http://www.cplusplus.com/reference/map/map/map/
// constructing maps
#include <iostream>
#include <map>
bool fncomp (char lhs, char rhs) {return lhs<rhs;}
struct classcomp {
bool operator() (const char& lhs, const char& rhs) const
{return lhs<rhs;}
};
int main ()
{
std::map<char,int> first;
first['a']=10;
first['b']=30;
first['c']=50;
first['d']=70;
std::map<char,int> second (first.begin(),first.end());
std::map<char,int> third (second);
std::map<char,int,classcomp> fourth; // class as Compare
bool(*fn_pt)(char,char) = fncomp;
std::map<char,int,bool(*)(char,char)> fifth (fn_pt); // function pointer as Compare
return 0;
}
bool fncomp (char lhs, char rhs) {return lhs>rhs;}
struct classcomp {
bool operator() (const char& lhs, const char& rhs) const
{return lhs>rhs;}
};
set和map中的元素是不可以重复的
set和map中的元素默认是升序排序的,我想知道如何做可以让set和map使用自己定义的方式去排序
template < class T, // set::key_type/value_type
class Compare = less<T>, // set::key_compare/value_compare
class Alloc = allocator<T> // set::allocator_type
> class set;
template < class T, // set::key_type/value_type
class Compare = less<T>, // set::key_compare/value_compare
class Alloc = allocator<T> // set::allocator_type
> class set;
set,map都可以自己提供Compare模板实参的呀。 [quote=引用 2 楼 zhao4zhong1 的回复:] http://www.cplusplus.com/reference/map/map/operators/ http://www.cplusplus.com/reference/set/set/operators/
struct xxtype
{
int num1, num2;
};
bool operator < (const xxtype &a , const xxtype &b)
{
return a.num1 < b.num1;
}
set<xxtype> the_set;
对于你自定义的类/结构体,可以直接自己重载小于号做。 set和map中的元素默认是升序排序的,我想知道如何做可以让set和map使用自己定义的方式去排序template < class T, // set::key_type/value_type class Compare = less<T>, // set::key_compare/value_compare class Alloc = allocator<T> // set::allocator_type > class set;
set,map都可以自己提供Compare模板实参的呀。template < class T, // set::key_type/value_type class Compare = less<T>, // set::key_compare/value_compare class Alloc = allocator<T> // set::allocator_type > class set;
用boost库里的多索引 自定义排序方式