我的关键结构比如

struct{

    int a;

    int b;

    int c;

}s;

因为这三个数据是基本信息,可以唯一区别一个设备。拿这样一个数据结构作为索引就能找到每个设备。



我现在想这么用

map<s, string>



因为map是二叉树,好像没法拿结构体比较大小,去索引,所以把结构体s改成类,重载小于号,让他能比较大小。

class s

{

public:

    int a;

    int b;

    int c;

    s(int m, int d, int u){a=m;b=d;c=u;}

    bool operator < (const s &other)

    {

        if ((a<other.a) ||

           ((a==other.a)&&(b<other.b)) ||

           ((a==other.a)&&(b==other.b)&&(c<other.c)))

        {

             return true;

         }

         return false;

    }

};



然后,

map<s, string> w;

s s1;

string s2;

一旦执行w.insert(make_pair(s1, s2));只要有这行就立刻报错。

要想使用一个类似结构体的数据结构作为KEY到底要怎么做呀?

是不是光重载一个小于号不够呀?

我现在好糊涂。有没有简单办法?

1.1

struct s {

    int a;

    int b;

    int c;

    bool operator<(const s&) const {
return true; }

};



map<s,string> m;

m.insert( make_pair(s(),"") );





1.2



struct s {

    int a;

    int b;

    int c;

};



bool operator<(const s&,const s&) { return true; }



map<s,string> m;

m.insert( make_pair(s(),"") );





2.

struct s {

    int a;

    int b;

    int c;

};



struct cmp {

    bool operator()(const s&,const s&) const { return true; }

};



map<s,string,cmp> m;

m.insert(make_pair(s(),"" ) );

转载自:http://bbs.chinaunix.net/thread-1538318-1-1.html

MAP--复杂map结构的构造的更多相关文章

  1. STL——容器(Map & multimap)的拷贝构造与赋值

    1. Map & multimap 的拷贝构造与赋值 map(const map &mp);               //拷贝构造函数 map& operator=(con ...

  2. Map接口----Map中嵌套Map

    package cn.good.com; import java.util.HashMap; import java.util.Iterator; import java.util.Map; impo ...

  3. 加锁的位置 (eq:map<key,map<>> 双集合 怎么 只加锁 在用到的对象位置,而不是把整个集合锁住)

    比如上边的map里套map 定义变量为data,例如组队副本 npc 为1 下有众多房间 即Map<1,<roomId,room>> ,处于多线程下,一个线程在 npc为1的下 ...

  4. 2019/2/20训练日记+map/multi map浅谈

    Most crossword puzzle fans are used to anagrams - groups of words with the same letters in different ...

  5. LinkedHashSet、Map、Map接口HashMap、Hashtable,TreeSet、TreeMap、如何选择使用集合实现类,Collections工具类

    一.Set接口实现类LinkedHashSet 实现继承图: 1.LinkedHashSet的全面说明 1) LinkedHashSet是 HashSet的子类 2) LinkedHashSet底层是 ...

  6. Map生成器 map适配器如今能够使用各种不同的Generator,iterator和常量值的组合来填充Map初始化对象

    Map生成器 map适配器如今能够使用各种不同的Generator,iterator和常量值的组合来填充Map初始化对象 package org.rui.collection2.map; /** * ...

  7. java中遍历MAP,嵌套map的几种方法

    java中遍历MAP的几种方法 Map<String,String> map=new HashMap<String,String>();    map.put("us ...

  8. arm-none-eabi-g++ -Xlinker -T "../LF3Kmonitor.ld" -Xlinker -Map="Bogota_ICT_V.map"-ram-hosted.ld -mc

    1.arm-none-eabi-g++:是编译ARM裸板用的编译器,不依赖于操作系统. 2.-Xlinker -T "../LF3Kmonitor.ld" -Xlinker -Ma ...

  9. 03-封装BeanUtil工具类(javabean转map和map转javabean对象)

    package com.oa.test; import java.beans.BeanInfo; import java.beans.IntrospectionException; import ja ...

  10. map泛型 map不指定泛型 与 Map<Object,Object>的区别

    map泛型 map不指定泛型 与 Map<Object,Object>的区别 private void viewDetail(){ Map map1 = new HashMap(); Ma ...

随机推荐

  1. 第四届河南省省赛 走迷宫 二分+DFS

    题目思路:使用二分查找路径中最大值和最小值之间的差值,从而确定出一组minn和maxn,对此组的minn和maxn经行DFS,如果可以找到一条路径,其中的最大值,最小值在minn~maxn的范围内,则 ...

  2. mysql 修改 添加 删除 表字段

    添加表的字段    alter table 表名  add  字段名  字段的类型 例子:        alter table table1 add transactor varchar(10) n ...

  3. struts2获得提交是get还是post方法提交

    String method=ServletActionContext.getRequest().getMethod(); System.out.println(method); 如果是get  会打印 ...

  4. libmysql.dll 找不到

    在用C#开发的时候,需要连接MySQL  ,系统提示  libmysql.dll 找不到模块. 我们可以找到 MySQL安装文件夹下的 C:\Program Files\MySQL\MySQL Ser ...

  5. debian 安装 android studio 环境

    jdk环境变量配置: ~/.hashrc export JAVA_HOME=/usr/share/jdk1.8.0_92 export PATH=$JAVA_HOME/bin:$PATH export ...

  6. 学习笔记——工厂模式Factory

    Product是我们所需要获得的对象,一般的使用中,我们直接通过new获得新对象. 当需要大量获得对象时,每一次都new会很麻烦. <真菌世界>游戏中,一个星球上有很多树,一棵树会不断生成 ...

  7. HDU - 4994 Revenge of Nim (取石子游戏)

    Problem Description Nim is a mathematical game of strategy in which two players take turns removing ...

  8. 在程序中用new ClassPathXmlApplicationContext()的注意事项

    http://blog.csdn.net/budapest/article/details/38493003

  9. PPTP-VPN日志功能,记录用户登录时间,流量统计,IP地址等信息

    我们先看两个文件 /etc/ppp/ip-up /etc/ppp/ip-down 这两个文件为shell脚本,当PPTP用户连接或者断开时分别执行这两个文件,并且会带相应的参数 这些参数有 $PEER ...

  10. jquery控制audio的播放与暂停

    <audio id="audio" src='music.mp3'></audio> <script type="type/javascri ...