gpt4 book ai didi

c++具有多个的Push_back类型

转载 作者:行者123 更新时间:2023-11-30 05:35:16 24 4
gpt4 key购买 nike

我有以下 C++ 代码...

struct myType
{
int SID;
int IID;
std::string myString;
};

std::vector<myType[2]> _type;

问题是当我这样做时...

for (int x = 1; x < 82432; x++)
{
_type.push_back(myType());
}

它给我以下错误

Error   441 error C2664: 'void std::vector<myType [2],std::allocator<_Ty>>::push_back(const myType (&)[2])' : cannot convert argument 1 from 'myType' to 'myType (&&)[2]'

我们的想法是创建一个像这样的 vector 数组:_type[82431][2].SID <<= type

感谢任何帮助!

最佳答案

您不能创建数组类型的 vector ,原因之一是您不能将一个数组分配给另一个数组,您可以获得的结果是:

std::vector<std::array<myType, 2>> _type;

这也不对

for (int x = 1; x < 82432; x++)
{
_type.push_back(myType());
}

即使您可以创建数组 vector ,在您的循环中您正在推送 myType 类型的数据,而您的 vector 是 myType[2] 类型。因此,要使其与 std::array 一起使用,请更改为 _type.push_back({myType(), myType()});

看这里http://coliru.stacked-crooked.com/a/b77015b21524de77用于其他一些用途

关于c++具有多个的Push_back类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33952556/

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