Const静态集合对const静态映射的引用

时间:2013-07-24 23:43:44

标签: c++ reference static set const

#include <iostream>
#include <string>
#include <map>
#include <set>
#include <initializer_list>

typedef std::map<std::string, bool> M1;
typedef std::set<int, const M1&> S1;

const static M1 m_1 = {{"USA", 0}, {"Africa", 1}, {"Netherlands", 0}};
const static M1 m_2 = {{"apple", 1}, {"oranges", 0}, {"pineapple", 0}};
const static M1 m_3 = {{"desk", 0}, {"chair", 1}, {"lamp", 0}};

const static S1 s_1 = {{33, &m_1}, {42, &m_2}, {77, &m_3}};

int main() { return (0); }

当我尝试编译这段代码时,set的初始化只得到1个错误,我不知道为什么我的编译器表现得那样(clang 3.4和gcc 4.8.1)因为它是set的{​​{1}}和int的{​​{1}},这就是我向构造函数提供的内容。

我在这里缺少什么?

2 个答案:

答案 0 :(得分:3)

课程集的模板是http://www.cplusplus.com/reference/set/set/?kw=set

template < class T,                        // set::key_type/value_type
           class Compare = less<T>,        // set::key_compare/value_compare
           class Alloc = allocator<T> >    // set::allocator_type
           > class set;

所以当你说

typedef std::set<int, const M1&> S1;

你正在使用M1&amp;作为比较标准,没有意义。

答案 1 :(得分:2)

  • 你在地图中给出指针而不是引用 声明
  • 您可以将S1设为地图,也可以只将一个参数传递给 模板

使用地图编译(Qt Creator 5.1):

#include <iostream>
#include <string>
#include <map>
#include <set>
#include <initializer_list>

typedef std::map<std::string, bool> M1;
typedef std::map<int, const M1&> S1;
typedef std::set<M1> S2;

const static M1 m_1 = {{"USA", false}, {"Africa", true}, {"Netherlands", false}};
const static M1 m_2 = {{"apple", true}, {"oranges", false}, {"pineapple", false}};
const static M1 m_3 = {{"desk", false}, {"chair", true}, {"lamp", false}};

const static S1 s_1 = {{33, m_1}, {42, m_2}, {77, m_3}};
const static S2 s_2 = {m_1, m_2, m_3};

int main() { return (0); }

编辑:为std::set

添加了typedef S2
相关问题