gpt4 book ai didi

initialization - 如何使用统一初始值设定项 "reduce typing to create C++ types"?

转载 作者:行者123 更新时间:2023-12-03 02:19:06 25 4
gpt4 key购买 nike

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

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

毫无疑问,这个初始化可能会改变我们的 C++ 编程。但我想知道在阅读 Herb Sutter FAQsAlfonses 的问题时,我是否错过了一些神奇的可能性。 。

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, 2, 3, 4};
std::map<int, std::string> y{{1, "hello"}, {2, "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");

关于initialization - 如何使用统一初始值设定项 "reduce typing to create C++ types"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7446499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com