gpt4 book ai didi

c++ - 如何使用显式元素初始化程序初始化 vector ?

转载 作者:太空狗 更新时间:2023-10-29 19:37:36 24 4
gpt4 key购买 nike

我们可以使用下面的语法来初始化一个 vector 。

// assume that UserType has a default constructor
vector<UserType> vecCollections;

现在,如果 UserType 没有为 UserType 提供默认构造函数,而只提供如下构造函数:

explicit UserType::UserType(int i) { ... }.

我应该如何使用 vector 构造函数调用这个显式元素初始值设定项?

最佳答案

vector<UserType> vecCollections(10, UserType(2));

关于c++ - 如何使用显式元素初始化程序初始化 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5291291/

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