C++ 如何用C++;这需要一个变量的向量数?

C++ 如何用C++;这需要一个变量的向量数?,c++,arguments,stdvector,argument-passing,variadic-functions,C++,Arguments,Stdvector,Argument Passing,Variadic Functions,我试图将不同数量的向量变量组合成一个向量。我尝试使用cstdarg库。它抛出 错误:无法通过“…”接收非平凡可复制类型“class myvectortype”的对象 在哪里 typedef向量myvectortype; typedef-vectordatavectotype 下面是函数的定义 datavectortype ExtractData::GetPixelData(int num, ...) { datavectortype data_temp; va_l

我试图将不同数量的
向量
变量组合成一个
向量
。我尝试使用
cstdarg
库。它抛出

错误:无法通过“…”接收非平凡可复制类型“class myvectortype”的对象

在哪里

typedef向量myvectortype; typedef-vectordatavectotype

下面是函数的定义

datavectortype ExtractData::GetPixelData(int num, ...)
{
        datavectortype data_temp;
        va_list arguments;
        va_start (arguments, num);
        for(int i = 0; i<num; i++)
        {
                data_temp.push_back(va_arg ( arguments, myvectortype));
        }
        va_end ( arguments );
        return data_temp;
}
datavectortype ExtractData::GetPixelData(int num,…) { datavectortype数据_temp; va_列表参数; va_开始(参数,num);
对于(inti=0;i自C++11以来,您已经可以

std::vector<double> v1{1}, v2{2}, v3{3, 4};
std::vector<std::vector<double>> v {v1, v2, v3};
std::向量v1{1},v2{2},v3{3,4}; std::向量v{v1,v2,v3}; 但是,如果您想为此执行函数,可以使用可变模板:

template <typename T, typename ...Ts>
std::vector<T> make_vector(const T& arg, const Ts&... args)
{
    return {arg, args...};
}
模板
std::vector make_vector(常数T和参数、常数T和参数)
{
返回{arg,args…};
}
因此,像这样使用它:

std::vector<double> v1{1}, v2{2}, v3{3, 4};
std::vector<std::vector<double>> v = make_vector(v1, v2, v3);
std::向量v1{1},v2{2},v3{3,4}; std::vector v=make_vector(v1、v2、v3);
可能重复@bariskand:如果您所做的只是将它们推回一个向量向量(顺便说一句,您应该使用
std::move
),那么您不需要函数。只需将它们放在大括号中(即形成一个
初始值设定项列表
)如下:
数据向量类型vov={v0,v1,v2,v3,v4,v5,v6};
.OP,如果要使用std:,请不要忘记包含header实用程序:move@al-Acme,我知道那篇文章,但是我不知道如何用向量结构处理它,初始化列表的例子对我来说很模糊。这似乎是一个XY问题,我更喜欢你的答案:)