我的关键结构比如

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. Exams

    Exams time limit per test 1 second memory limit per test 256 megabytes input standard input output s ...

  2. iOS 开发之照片框架详解

    转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework.html 一. 概要 在 iOS 设备中,照片和视频是相当重 ...

  3. 项目总结SpringMVC相关

    流程文字概述1.用户发送请求至前端控制器DispatcherServlet2.DispatcherServlet收到请求调用HandlerMapping处理器映射器.3.处理器映射器找到具体的处理器, ...

  4. 扫盲: JAVA基本常识

    http://java-mzd.iteye.com/blog/838514

  5. sites

    search http://213.240.44.13/ http://1.179.252.95/

  6. IndentationError: unexpected indent python

    都知道python是对格式要求很严格的,写了一些python但是也没发现他严格在哪里,今天遇到了IndentationError: unexpected indent错误我才知道他是多么的严格. 以后 ...

  7. HDU 1890 Robotic Sort | Splay

    Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) [Pr ...

  8. python中uuid来生成机器唯一标识

    摘要: 我们可以使用uuid1的后16位来标识一个机器.  # use machine specific uuid, last 16 char will be the same if machine ...

  9. XML 从基础到精通

    1.简介 XML(可扩展标记语言)语言是一种数据交换标准,用于存储数据:关键词是标记: XML具有以下优点: (1) 方便的穿过防火墙,在不同的操作系统之间通信,跨语言,跨平台.数据共享非常方便.(J ...

  10. mysql查询数据库约束

    SELECT * FROM information_schema.`TABLE_CONSTRAINTS` where TABLE_SCHEMA='mold' and TABLE_NAME='tplmi ...