如何在初始化列表中使用std :: map :: operator = [英] How to use std::map::operator= with initializer lists
问题描述
我在> 前问过有关
I asked the same question before about boost::assign::map_list_of
(which didn't get answered), then I thought maybe using brace initialization would help, but it didn't.
这很完美:
std::map<int, char> m = {{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd'}};
但这不是:
std::map<int, char> m;
m = {{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd'}};
Visual Studio 2013给出错误error C2593: 'operator =' is ambiguous
,可能是operator=(std::initalizer_list)
或operator=(std::map&&)
.
Visual Studio 2013 gives the error error C2593: 'operator =' is ambiguous
, could be either operator=(std::initalizer_list)
or operator=(std::map&&)
.
是否可以使用第二个版本?例如,对于m
是成员变量的情况.
Is it possible to get the second version to work? For cases where m
is a member variable, for example.
推荐答案
您可以构造一个临时文件并将其用于作业.
You could construct a temporary and use it in the assignment.
std::map<int, char> m;
m = std::map<int, char>{{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd'}};
如果不想重复输入,可以使用decltype
.
If you don't want to repeat the type, you can use decltype
.
std::map<int, char> m;
m = decltype(m){{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd'}};
相关的SO帖子:
Related SO posts:
- Initializing map of maps with initializer list in VS 2013
- Using Initializer Lists with std::map
- Is this a compiler bug? Am I doing something wrong?
这篇关于如何在初始化列表中使用std :: map :: operator =的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!