gpt4 book ai didi

c++ - 分配兼容 vector ?

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

假设您有一个类型的 vector ,想要将其分配给另一种不同类型的 vector ?

#include <vector>

int main() {
std::vector ints = {0,1,2,3};
std::vector<long long int> llints = ints;//compile error here
return 0;
}

Try it yourself

如何使用单行(一个表达式)而不使用 lambda,仅使用 stdboost

最佳答案

如果要构建,可以使用two iterator constructor :

std::vector<long long int> llints(ints.begin(), ints.end());

如果要赋值给现有的 vector ,可以使用两个迭代器std::vector::assign成员函数:

std::vector<long long int> llints;
...
llints.assign(ints.begin(), ints.end());

关于c++ - 分配兼容 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59841316/

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