gpt4 book ai didi

c++ - 如何用特定数量的元素初始化集合?

转载 作者:行者123 更新时间:2023-11-30 03:48:55 26 4
gpt4 key购买 nike

如何初始化 std::set<int>有五个元素??像数组我可以说int x[5];或 vector 我也可以使用 vector<int> v(5);我应该如何在 std::set<int> 中做同样的事情? ?

最佳答案

A std::set应该包含唯一值,因此对于初始化,您必须指定这些值。

您可以使用带有initializer_list 的构造函数,但您必须指定一组唯一的数字:

std::set<int> S { 1, 2, 3, 4, 5 };

关于c++ - 如何用特定数量的元素初始化集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32925653/

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