0
#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); }

当我尝试编译这段代码时,我只收到 1 个初始化错误set,而且我不知道为什么我的编译器会这样(clang 3.4 和 gcc 4.8.1),因为它是一个简单set的这就是我提供给构造函数的内容。intconstant referencesmap

我在这里缺少什么?

4

2 回答 2

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& 作为比较标准,这是没有意义的。

于 2013-07-24T23:56:25.753 回答
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

于 2013-07-24T23:51:58.830 回答