初始化 – 如何使用Uniform Initializers“减少创建C类型的类型”?

我用{}玩了很多新的统一初始化.像这样:

vector<int> x = {1,2,3,4};
map<int,string> getMap() {
    return { {1,"hello"},{2,"you"} };
}

无可争议的是,这种初始化可能会改变我们的程序C.
但我想知道在Herb Sutter FAQs阅读Alfonses的问题时我是否错过了一些神奇的可能性.

Alfonse: Uniform initialization (the use of {} to call constructors when the type being constructed can be deduced) has the potential to radically reduce the quantity of typing necessary to create C++ types. It’s the kind of thing,like lambdas,that will change how people write C++ code. […]

有人能给我一个Alfonse在这里设想的例子吗?

解决方法

我认为他的意思是

std::vector<int> x{1,4};
std::map<int,std::string> y{{1,"you"}};

打字明显少于

std::vector<int> x;
x.push_back(1);
x.push_back(2);
x.push_back(3);
x.push_back(4);
std::map<int,std::string> y;
y.emplace(1,"hello");
y.emplace(2,"you");

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...