How to define a map with key as a enum and value as an integer in c++11/c++14?

蹲街弑〆低调 提交于 2019-12-13 02:39:06

问题


enum Symbols
{
    BAR,
    BELL,
    PLUM,
    ORANGE,
    CHERRY,
    DOESNOTMATTER,
}wheel1, wheel2, wheel3;

map<Symbols[3], int> symb = {{{BAR, BAR, BAR}, 250},
                              {{BELL, BELL, BELL}, 20},
                              {{BELL, BELL, BAR}, 20},
                              {{PLUM, PLUM, BAR}, 14},
                              {{PLUM, PLUM, PLUM}, 14},
                              {{ORANGE, ORANGE, BAR}, 10},
                              {{ORANGE, ORANGE, ORANGE}, 10},
                              {{CHERRY, CHERRY, CHERRY}, 7},
                              {{CHERRY, CHERRY, DOESNOTMATTER}, 5},
                              {{CHERRY, DOESNOTMATTER, DOESNOTMATTER}, 2}};

I have defined an enumerator named Symbols. I am trying to create a map, who's key is a Symbols enum array and value is an integer.

When I try to run this program, I get an error "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/utility:268:11: Array initializer must be an initialiser list".

What am I doing wrong here? I have tried inserting the values too. The error is the same.


回答1:


The raw array Symbols[3] doesn't match the requirement of the containers as a key type of std::map.

std::map meets the requirements of Container, AllocatorAwareContainer, AssociativeContainer and ReversibleContainer.

See C++ concepts: Container

CopyInsertable
EqualityComparable
Destructible 

You can use std::array or std::vector. Such as:

std::map<std::array<Symbols, 3>, int> symb = ...

LIVE




回答2:


The reason you cannot do this is because the raw arrays are not copyable or assignable, and copy operation is required for map, vector, set, etc. Use this instead:

std:array<Symbol,3>



回答3:


For map, Key need at least to be :

  • Copiable
  • less Comparable

To achieve this, I will pass by a structure :

struct key
{
    Symbol k1;
    Symbol k2;
    Symbol k3;

    key(Symbol _k1, Symbol _k2, Symbol _k3):
        k1(_k1), k2(_k2), k3(_k3)
    {}
    key(key const &) = default;
    key(key &&) = default;
    key &operator=(key const &) = default;
    key &operator=(key &&) = default;

    bool operator<(key const & rhs)
    {
       // perform less operation between this and rhs
    }
};



回答4:


This would work:

#include <iostream>
#include <map>
#include <array>

enum Symbols
{
  BAR,
  BELL,
  PLUM,
  ORANGE,
  CHERRY,
  DOESNOTMATTER,
};

void main()
{
  std::map<std::array<Symbols,3>, int> symb ( { 
    { { Symbols::BAR, Symbols::BAR, Symbols::BAR }, 250 }
  } );
  std::array<Symbols, 3> bar_sym = { Symbols::BAR, Symbols::BAR, Symbols::BAR };
  std::cout << symb[bar_sym] << std::endl;
  }


来源:https://stackoverflow.com/questions/35452739/how-to-define-a-map-with-key-as-a-enum-and-value-as-an-integer-in-c11-c14

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!