From: https://www.walletfox.com/course/mapwithcustomclasskey.php

If you have ever tried to use a custom class as a key of std::map, most probably you got a compilation error. This article explains why this happens and shows how to make custom classes work as keys of std::map by providing a sorting rule. The article demonstrates three different ways in which you can provide the sorting rule, namely via operator overloading, a comparator (C++98) or with help of a lambda expression (C++11 only).

The reason why we have to provide a sorting rule for user-defined classes as keys is because std::map is a binary search tree data structure. This videoshows how binary search trees work in general . Binary search trees store their data in sorted order, thus we have to provide a sorting rule. Once we provided the sorting rule, the map can make use of binary search for all its operations. A binary tree structure for a map of size 6 can be seen below:

A basic example of a binary search can be a guessing game in which one of the players thinks of a number in a certain range (e.g. 0 - 24) and the other player tries to guess it. In binary search the second player always guesses the middle element (i.e. 12) and the first player informs him whether this is larger, smaller or equal to the value that he thought of. Every time, the player makes a guess, one half of the array gets eliminated. The search is repeated until the second player guesses the correct number. The number of times we need to do this is log2(N) because each comparison throws out half of the possibilities.

Problem definition

Imagine we have a class called Color represented by three private attributes red, green and blue and we would like to use this class as a key of std::map. The class can be seen below. Notice that I also overloaded operator<<, this has nothing to do with our problem, it is there just so that we can print the Color directly with std::cout.

class Color{
public:
Color();
Color(int r, int g, int b);
int red() const {return m_red;}
int green() const {return m_green;}
int blue() const {return m_blue;}
bool areValid() const;
private:
int m_red;
int m_green;
int m_blue;
}; std::ostream& operator<<(std::ostream &output, const Color &c);

Let's look at the main.cpp below. We construct a map in which we use our class Color as a key and an int as a value. We insert a couple of pairs (Color, int) and try to print the elements of the map.

int main()
{
std::map<Color, int> myMap;
myMap[Color(0, 0, 0)] = 0;
myMap[Color(230, 159, 0)] = 1;
myMap[Color(86, 180, 233)] = 2;
myMap[Color(128, 128, 0)] = 3; std::map<Color, int>::iterator it;
for (it = myMap.begin(); it != myMap.end(); ++it)
std::cout << it->first << " " << it->second << '\n'; return 0;
}

If we try to compile the code above, it won't compile and produces the following error: no match for 'operator<' in '__x < __y'. This is because we did not provide a rule to sort the elements which are needed by the binary search tree. How do we solve this? The following lines show how to solve the problem.

Overloading the operator< (C++98)

The first possible solution to our problem is overloading operator<. This is highlighted in the code below. The overloaded operator< takes references to two color instances as arguments and returns true or false based on our sorting rule which will appear in the body of the method. Notice that the only thing we need to do is to provide a rule according to which to compare two color objects. This is sufficient for the std::map to achieve sorted order.

class Color{
public:
Color();
Color(int r, int g, int b);
int red() const {return m_red;}
int green() const {return m_green;}
int blue() const {return m_blue;}
bool areValid() const;
private:
int m_red;
int m_green;
int m_blue;
}; std::ostream& operator<<(std::ostream &output, const Color &c);
bool operator<(const Color &c1, const Color &c2);

Now let's look at the body of the overloaded operator from color.cpp (the entire source files can be found above). How do we decide which color is "smaller"? This depends on our intentions. If we only require that our custom class works with std::map, we should provide something simple and logical. We might choose to sort colors according to their lightness which is in its simplest case an average of red, green and blue. Thus, the operator< will return true if the average of R,G and B of the left-hand color is smaller than the average of R, G and B of the right-hand color. This can be seen below

bool operator<(const Color &c1, const Color &c2){
return c1.red() + c1.green() + c1.blue() <
c2.red() + c2.green() + c2.blue();
}

Note: The basic formula for color lightness is an average of R, G and B, i.e. (R + G + B) / 3. Notice that we omitted the division by 3 in the code above, that is because when comparing two colors the division by 3 simply disappears from the equation.

Note: The code compiles and produces the following output: You can see that the colors are sorted in ascending order according to the sum of their R,G and B values.

(0, 0, 0) 0
(128, 128, 0) 3
(230, 159, 0) 1
(86, 180, 233) 2

Note: Notice that we could call

std::cout << it->first << " " << it->second << '\n';

only because we also overloaded the output operator<<. The details of this can be found in the source files above.

User-defined comparator (C++98)

Another way of providing the sorting rule is with the help of a custom comparator. A custom comparator is a function object and a function object is simply a class that defines operator() and as a result can be called as if it was a function. You can see our version of the class below. Notice that the parameters, return type as well as the body of the function are the same as in the first solution.

class Color{
public:
Color();
Color(int r, int g, int b);
int red() const {return m_red;}
int green() const {return m_green;}
int blue() const {return m_blue;}
bool areValid() const;
private:
int m_red;
int m_green;
int m_blue;
}; std::ostream& operator<<(std::ostream &output, const Color &c); class Comparator {
public:
bool operator()(const Color& c1, const Color& c2){
return c1.red() + c1.green() + c1.blue() <
c2.red() + c2.green() + c2.blue();
}
};

To use the comparator, notice that we passed an extra argument to the map. The rest of the code remains the same.

int main()
{
std::map<Color, int, Comparator> myMap;
myMap[Color(0, 0, 0)] = 0;
myMap[Color(230, 159, 0)] = 1;
myMap[Color(86, 180, 233)] = 2;
myMap[Color(128, 128, 0)] = 3; std::map<Color, int>::iterator it;
for (it = myMap.begin(); it != myMap.end(); ++it)
std::cout << it->first << " " << it->second << '\n'; return 0;
}

Lambda expression (C++11)

C++11 provides another solution to the same problem, namely a lambda expression.

A lambda expression is a syntactic shortcut for a function object, i.e. an object that can be called as if it was a function. The basic syntax of the lambda expression can be seen below:

[captures] (parameter list) -> return-type
{
lambda body;
}

The lambda expression for our problem can be seen below. Our lambda expression has no captures, takes two parameters and returns a bool. The body of the expression also remains the same as in the previous solutions.

auto comparator = [](const Color& c1, const Color& c2) -> bool
{
return c1.red() + c1.green() + c1.blue() <
c2.red() + c2.green() + c2.blue();
};

Notice that to use the lambda expression with our map we have to use decltype. The decltype() is here because we cannot use lambda in unevaluated context. We firstly have to define lambda with 'auto' elsewhere and then only use it in the map's parameters with decltype(). If we did not do this, we would get a compilation error that would look like this: type/value mismatch at argument 3 in template parameter list for 'template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map'.

int main()
{
auto comparator = [](const Color& c1, const Color& c2) -> bool
{
return c1.red() + c1.green() + c1.blue() <
c2.red() + c2.green() + c2.blue();
}; std::map<Color, int, decltype(comparator)> myMap(comparator);
myMap = {
{Color(0, 0, 0), 0},
{Color(230, 159, 0), 1},
{Color(86, 180, 233), 2},
{Color(128, 128, 0), 3}
}; for (auto& it : myMap)
std::cout << it.first << " " << it.second << '\n'; return 0;
}

That's it. Now the std::map will work with our user-defined class.

Using std::map with a custom class key的更多相关文章

  1. std::map插入已存在的key时,key对应的内容不会被更新

    std::map插入已存在的key时,key对应的内容不会被更新,如果不知道这一点,可能会造成运行结果与预期的不一致 “Because element keys in a map are unique ...

  2. c++ how to make your own class a valid key type for std::map?

    In Java, if you want your own class to be a valid key type of the container, you just need to make i ...

  3. std::map自定义类型key

    故事背景:最近的需求需要把一个结构体struct作为map的key,时间time作为value,定义:std::map<struct, time> _mapTest; 技术调研:众所周知, ...

  4. C++ std::map

    std::map template < class Key, // map::key_type class T, // map::mapped_type class Compare = less ...

  5. std::map用法

    STL是标准C++系统的一组模板类,使用STL模板类最大的好处就是在各种C++编译器上都通用.    在STL模板类中,用于线性数据存储管理的类主要有vector, list, map 等等.本文主要 ...

  6. C++ std::map::erase用法及其陷阱

    1.引入: STL的map中有一个erase方法用来从一个map中删除制定的节点 eg: map<string,string> mapTest; typedef map<string ...

  7. std::map的操作:插入、修改、删除和遍历

    using namespace std; std::map<int,int> m_map; 1.添加 for(int i=0;i<10;i++) { m_map.insert(mak ...

  8. map以自定义类型当Key

    关于map的定义: template < class Key, class T, class Compare = less<Key>, class Allocator = alloc ...

  9. 对std::map进行排序

    1.对Key排序. std::map的第三个参数即为对key进行排序的比较函数.默认为less,表示升序.如果要降序,可以改为greater. 2.对Value排序 不支持,因为map不是一个序列的容 ...

随机推荐

  1. 步步为营-73-asp.net的简单练习(根据美工提供静态页面,编写后台代码)

    说明:实际企业中开发分工是很明确,往往程序员根据美工提供的UI界面进行后台代码的编写. 1.1 原始HTML页面 1.2 使用aspx进行修改 这里使用到了三层架构 using System; usi ...

  2. The last packet sent successfully to the server was 0 milliseconds ago.[nutch---mysql ]

    今天在使用JDBC操作mysql时遇到下面的异常信息: 引用 The last packet sent successfully to the server was 0 milliseconds ag ...

  3. 【开源小软件 】Bing每日壁纸 让桌面壁纸保持更新

    发布一个开源小软件,Bing每日壁纸. 该小软件可以自动获取Bing的精美图片设置为壁纸,并且支持随机切换历史壁纸,查看壁纸故事. 欢迎大家下载使用,点star!有问题请留言或者提issue. 开源地 ...

  4. 第六章|网络编程-socket开发

    1.计算机基础 作为应用开发程序员,我们开发的软件都是应用软件,而应用软件必须运行于操作系统之上,操作系统则运行于硬件之上,应用软件是无法直接操作硬件的,应用软件对硬件的操作必须调用操作系统的接口,由 ...

  5. 【Java】 剑指offer(35) 复杂链表的复制

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 请实现函数ComplexListNode* Clone(Compl ...

  6. 定制库到maven库

    有一些jar不支持maven,这个时候就可以使用下面的处理方式. kaptcha,它是一个流行的第三方Java库,它被用来生成 “验证码” 的图片,以阻止垃圾邮件,但它不在 Maven 的中央仓库中. ...

  7. 自适应阈值二值化之最大类间方差法(大津法,OTSU)

    最大类间方差法是由日本学者大津(Nobuyuki Otsu)于1979年提出的,是一种自适应的阈值确定的方法,又叫大津法,简称OTSU.它是按图像的灰度特性,将图像分成背景和目标2部分.背景和目标之间 ...

  8. 【转】一张图解析FastAdmin中的表格列表的功能

     一张图解析FastAdmin中的表格列表的功能 功能描述请根据图片上的数字索引查看对应功能说明. 1.时间筛选器如果想在搜索栏使用时间区间进行搜索,则可以在JS中修改修改字段属性,如 {field: ...

  9. Java内存管理-初始JVM和JVM启动流程(二)

    勿在流沙住高台,出来混迟早要还的. 做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开! 上一篇分享了什么是程序,以及Java程序运行的三个阶段.也顺便提到了Java中比较重要 ...

  10. [洛谷P2123]皇后游戏

    很抱歉,这个题我做的解法不是正解,只是恰巧卡了数据 目前数据已经更新,这个题打算过一段时间再去写. 目前在学习DP,这个会暂时放一放,很抱歉 这个题是一个国王游戏的变形(国王游戏就把我虐了qwq) 题 ...