map key 使用自定义结构,这个应该怎么实现比较函数?
struct Info
{
int A;
int B;
int c;
};
Info src;
Info des;
key相当的条件是
src.A==des.B&& src.B==des.A || src.A==des.B&& src.B==des.A
这个比较函数该怎么写?
map<Info,int>mapInfo;//要使用这样的map。 map?自定义?结构体?key?c++
[解决办法]
你要的是一個比較函數,參看下面的sample,然後按你自己的需要寫一個。
#include <iostream>
#include <map>
using namespace std;
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 ()
{
map<char,int> first;
first['a']=10;
first['b']=30;
first['c']=50;
first['d']=70;
map<char,int> second (first.begin(),first.end());
map<char,int> third (second);
map<char,int,classcomp> fourth; // class as Compare
bool(*fn_pt)(char,char) = fncomp;
map<char,int,bool(*)(char,char)> fifth (fn_pt); // function pointer as Compare
return 0;
}