map以自定义类型当Key

关于map的定义:

template < class Key, class T, class Compare = less<Key>,
           class Allocator = allocator<pair<const Key,T> > > class map;

第一个template参数被当做元素的key,第二个template参数被当作元素的value。Map的元素型别Key和T,必须满足以下两个条件:
1.key/value必须具备assignable(可赋值的)和copyable(可复制的)性质。
2.对排序准则而言,key必须是comparable(可比较的)。
第三个template参数可有可无,用它来定义排序准则。这个排序准则必须定义为strict weak ordering。元素的次序由它们的key决定,和value无关。排序准则也可以用来检查相等性:如果两个元素的key彼此的都不小于对方,则两个元素被视为相等。如果使用未传入特定排序准则,就使用缺省的less排序准则——以operator<来进行比较。

所谓“排序准则”,必须定义strict weak ordering,其意义如下:
1.必须是“反对称性的”。
2.必须是“可传递的”。
3.必须是“非自反的”。

按照定义的要求:
我们有两种方法以自定义类型当key:
1.为自定义类型重载operator<,map的第三个参数为默认仿函数less<key>。

  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. using namespace std;
  5. class test
  6. {
  7. public:
  8. bool operator<(const test& a)const;
  9. //private:
  10. int nA;
  11. int nB;
  12. };
  13. bool test::operator<(const test& a)const
  14. {
  15. if(this->nA < a.nA)
  16. return true;
  17. else
  18. {
  19. if(this->nA == a.nA && this->nB < a.nB)
  20. return true;
  21. else
  22. return false;
  23. }
  24. }
  25. int main()
  26. {
  27. map<test, string> myTestDemo;
  28. test tA;
  29. tA.nA = 1;
  30. tA.nB = 1;
  31. test tB;
  32. tB.nA = 1;
  33. tB.nB = 2;
  34. myTestDemo.insert(pair<test, string>(tA, "first!"));
  35. myTestDemo.insert(pair<test, string>(tB, "second!"));
  36. map<test, string>::iterator myItr = myTestDemo.begin();
  37. cout << "itr begin test nA:" << myItr->first.nA << endl;
  38. cout << "itr begin test nB:" << myItr->first.nB << endl;
  39. cout << "itr begin test string:" << myItr->second << endl;
  40. return 1;
  41. }

2. 不使用map的第三个参数为默认仿函数less<key>,自己编写一个比较仿函数。

    1. #include <iostream>
    2. #include <map>
    3. using namespace std;
    4. struct keyOfMap
    5. {
    6. int firstOfKey;
    7. int secondOfKey;
    8. };
    9. struct myMapFunctor
    10. {
    11. bool operator()(const keyOfMap& k1, const keyOfMap& k2) const
    12. {
    13. if(k1.firstOfKey < k2.firstOfKey)
    14. return true;
    15. else
    16. return false;
    17. }
    18. };
    19. int main()
    20. {
    21. map<keyOfMap, string, myMapFunctor> test;
    22. keyOfMap temp1;
    23. keyOfMap temp2;
    24. temp1.firstOfKey = 1;
    25. temp1.secondOfKey = 1;
    26. temp2.firstOfKey = 2;
    27. temp2.secondOfKey = 2;
    28. test.insert(make_pair<keyOfMap, string>(temp1, "first"));
    29. test.insert(make_pair<keyOfMap, string>(temp2, "second"));
    30. map<keyOfMap, string, myMapFunctor>::iterator begin = test.begin();
    31. cout << begin->first.firstOfKey << begin->first.secondOfKey << begin->second << endl;
    32. return 1;
时间: 2024-07-30 08:32:22

map以自定义类型当Key的相关文章

cxf处理一些Map等复杂类型

前面讲的一些都是简单类型,cxf都支持.但是有些复杂类型,cxf是不支持,比如常用的Map类型: 下面我们在前面的实例基础上在加一个方法,比如我们现在有个需求,获取所有用用户以及对应的每个用户所有角色信息: 服务器端: HelloWorld接口加方法: /**  * 获取所有用户以及对应的角色  * @return  */ public Map<String,List<Role>> getRoles(); HelloWorldImpl实现类加方法实现: public Map<

一个关于自定义类型作为HashMap的key的问题

在之前的项目需要用到以自定义类型作为HashMap的key,遇到一个问题:如果修改了已经存储在HashMap中的实例,会发生什么情况呢?用一段代码来试验: import java.util.HashMap; import java.util.Map; public class TestHashMap { public static void main(String[] args) { testObjAsKey(); } private static void testObjAsKey() { c

java自定义类型 作为HashMap中的Key值 (Pair&lt;V,K&gt;为例)

由于是自定义类型,所以HashMap中的equals()函数和hashCode()函数都需要自定义覆盖. 不然内容相同的对象对应的hashCode会不同,无法发挥算法的正常功能,覆盖equals函数,应该就相当于c++重载==运算符来保证能判断是否相等.只不过java没有自定义重载运算符这个功能的,需要进行函数覆盖. equals的函数原型是 boolean equals(Object o);注意括号内.hashCode的函数原型就是int hashCode(); 先看一段代码: import

【map】【unordered_map】map和unordered_map中键类型为自定义类型的操作

STL中map的底层为红黑树,所以查找的时间复杂度为O(logn). unordered_map是根据哈希值(遇到哈希值相同时用==号比较)寻找键,所以时间复杂度为O(1). 键类型为自定义类型时,map需要重载键类型的<符号,unordered_map需要定义键类型的哈希函数(在类外定义),以及重载键类型的==符号. class person1 { public: string name; int age; person1(string s,int i):name(s),age(i){} //

Hadoop日记Day13---使用hadoop自定义类型处理手机上网日志

测试数据的下载地址为:http://pan.baidu.com/s/1gdgSn6r 一.文件分析 首先可以用文本编辑器打开一个HTTP_20130313143750.dat的二进制文件,这个文件的内容是我们的手机日志,文件的内容已经经过了优化,格式比较规整,便于学习研究,感兴趣的读者可以尝试一下. 我从中截取文件中的一行记录内容进行分析: 1363157985066     13726230503    00-FD-07-A4-72-B8:CMCC    120.196.100.82    i

【Spring】利用spring的JdbcTemplate查询返回结果映射到自定义类型

// org.springframework.jdbc.core.JdbcTemplate 中的查询方法基本都有支持参数RowMapper<T> rowMapper的重载方法.下面只是随便举例2个,还有很多 public <T> List<T> query(String sql, Object[] args, RowMapper<T> rowMapper) throws DataAccessException { ... }; public <T>

struts2自定义类型转换器

首先,何为struts2的类型转换器? 类型转换器的作用是将请求中的字符串或字符串数组参数与action中的对象进行相互转换. 一.大部分时候,使用struts2提供的类型转换器以及OGNL类型转换机制即可满足大部分类型转换需求.如: 类User.java package models; public class User { private String username; private String password; public String getUsername() { retur

java.lang.Comparable, java.util.Compartor区别以及Hadoop中关于自定义类型中的compare方法

public interface Comparable<T> { public int compareTo(T o); } 规定了对象内部比较的方法 public interface Comparator<T> { int compare(T o1, T o2); boolean equals(Object obj); } 定义外部比较器的基本方法,其中equals是用来确定两个比较器是否相等. 关于对象内部比较和外部比较这两个接口的区别和使用场景如下: 个人总结: Compara

oracle 自定义类型 type / create type

一:Oracle中的类型有很多种,主要可以分为以下几类: 1.字符串类型.如:char.nchar.varchar2.nvarchar2. 2.数值类型.如:int.number(p,s).integer.smallint. 3.日期类型.如:date.interval.timestamp. 4.PL/SQL类型.如:pls_integer.binary_integer.binary_double(10g).binary_float(10g).boolean.plsql类型是不能在sql环境中使