如何在初始化列表中使用std :: map :: operator = [英] How to use std::map::operator= with initializer lists

查看:128
本文介绍了如何在初始化列表中使用std :: map :: operator =的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在> 前问过有关(没有得到回答),然后我想也许使用括号初始化会有所帮助,但没有帮助.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆