64,651
社区成员
发帖
与我相关
我的任务
分享
map<string,int> rule_map;
rule_map["a$1"] = 1;
rule_map["b$2"] = 2;
rule_map["c$1"] = 3;
rule_map["a$2"] = 4;
rule_map["b&1"] = 5;
rule_map["b$3"] = 6;
struct my_cpr {
bool operator() (const string& left, const string& right) const
{
return rule_map[left]<rule_map[right];
}
};
map<string,int,my_cpr> mymap;
没有规律的话就自己造个规律吧
# include <iostream>
# include <string>
# include <map>
using namespace std;
struct string_sorter {
bool operator ()(const string & a, const string & b) const
{
if (a[0] != b[0])
return a[0] < b[0];
else
return a[2] < b[2];
}
};
int main()
{
map<string, int, string_sorter> mymap;
mymap["b&1"] = 1;
mymap["a$1"] = 1;
mymap["c$1"] = 1;
mymap["b$2"] = 1;
mymap["a$2"] = 1;
mymap["b$3"] = 1;
for (map<string, int>::iterator it = mymap.begin(); it != mymap.end(); it++)
{
cout << it->first << '\t' << it->second << endl;
}
return 0;
}
a$1 1
a$2 1
b&1 1
b$2 1
b$3 1
c$1 1
#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;
}