stl map的一个排序问题

三分球很准真的 2012-12-12 02:34:53
各位大侠,求教:
对map<string,int> 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;
如何写仿函数实现如下的排序?
a$1 1
b$2 1
c$1 1
a$2 1
b&1 1
b$3 1
...全文
230 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
AlgoThinking 2013-01-10
  • 打赏
  • 举报
回复
引用 10 楼 jackzhhuang 的回复:
什么叫仿函数?
说简单点仿函数就是重载了operator()是对象,仿函数只是一个对象。
Saingel 2013-01-10
  • 打赏
  • 举报
回复

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;
没有规律的话就自己造个规律吧
老王爱上猫 2013-01-08
  • 打赏
  • 举报
回复
智慧的老鸟,不给力啊
hackbuteer1 2013-01-08
  • 打赏
  • 举报
回复
不知道这个具体的排序规则是什么啊?
swordtan 2013-01-08
  • 打赏
  • 举报
回复
4楼正解,仿函数好实现,问题是这个看不出排序准则啊
mymtom 2013-01-08
  • 打赏
  • 举报
回复
引用 4 楼 smallnat 的回复:
表示没看出规律。 到底是想根据什么排序。 楼主提到仿函数,知道按什么排序的话,写代码应该不存在问题的吧
++
jackzhhuang 2013-01-08
  • 打赏
  • 举报
回复
什么叫仿函数?
ForestDB 2013-01-08
  • 打赏
  • 举报
回复
key的排序规律是什么? 以下是按自己的想的规律给出的例子。

# 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
hznat 2013-01-07
  • 打赏
  • 举报
回复
表示没看出规律。 到底是想根据什么排序。 楼主提到仿函数,知道按什么排序的话,写代码应该不存在问题的吧
Kaile 2013-01-07
  • 打赏
  • 举报
回复
a$1 1 b$2 1 c$1 1 a$2 1 b&1 1 b$3 1 找出规律就好写了
wangeen 2013-01-07
  • 打赏
  • 举报
回复
自定义map的 < 比较函数.

#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;
}
brikehuang 2013-01-07
  • 打赏
  • 举报
回复
struct map_sort_pred: public binary_function<..> { bool operator()(const map<string,int> &_node1, const map<string,int> &_node2) { } }; sort(mymap.begin(), mymap.end(), map_sort_pred);

64,651

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧