#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}},这就是我向构造函数提供的内容。
我在这里缺少什么?
答案 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)
使用地图编译(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