我的关键结构比如

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. event小解

    首先是一个小例子: <input type="text" onclick="a(event)"/> function a(event){   con ...

  2. UIKit控件直接显示网页文字内容

    NSString *html = @"<bold>Hello</bold> Now<br> <em>iOS</em> can cr ...

  3. A convenient way of installing(compiling) VIM with YCM

    Ah, while I am still downloading LLVM from github(very slow.. and very large in size). I come with m ...

  4. Viewpager实现网络图片的轮播

    //主意:里面用到了第三方的Xutils.jar包和Imageloader.jar包还用到了访问网络,所以要加网络权限 <uses-permission android:name="a ...

  5. css 7.30

    1.外提到内联元素,我们会想到有个display的属性是display:inline;这个属性能够修复著名的IE双倍浮动边界(float时margin)问题 2.一般来说,可以为所有块级元素应用 te ...

  6. java中list的使用方法

    LIST是个容器接口,可以理解为动态数组,传统数组必须定义好数组的个数才可以使用,而容器对象无须定义好数组下标总数, 用add()方法即可添加新的成员对象,他可以添加的仅仅只能为对象,不能添加基本数据 ...

  7. Linux学习 -- 软件包管理

    1 软件包类型 源码包 脚本安装包  install.sh  不常用 二进制包(rpm包.系统默认包) RedHat -- rpm包 Debian,Ubuntu -- beb包 2 RPM包命令管理 ...

  8. JSch - Java实现的SFTP

    JSch - Java实现的SFTP(文件上传详解篇)     JSch是Java Secure Channel的缩写.JSch是一个SSH2的纯Java实现.它允许你连接到一个SSH服务器,并且可以 ...

  9. 报错:“不是有效的Win32应用程序”的解决办法

    Win7.Win8下用VS2013编译完的程序,拿到32位WindowsXP虚拟机下运行有时候会报错:

  10. Linux下find命令用法小结

    find是个使用频率比较高的命令.常常用它在系统特定目录下,查找具有某种特征的文件. find命令的格式:find [-path……] -options [-print -exec -ok] path ...