Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用折叠表达式合并多个向量_C++_C++17 - Fatal编程技术网

C++ 使用折叠表达式合并多个向量

C++ 使用折叠表达式合并多个向量,c++,c++17,C++,C++17,在尝试学习c++17的一些新特性时,遇到了折叠表达式 使用折叠表达式,我可以对元素进行多次推回。我只是尝试使用折叠表达式将多个向量合并为单个向量。我知道还有其他方法可以合并向量,但我想使用折叠表达式 #include <iostream> #include <vector> template<typename T, typename... Args> void push_back_vec(std::vector<T>& v, Args&a

在尝试学习c++17的一些新特性时,遇到了折叠表达式

使用折叠表达式,我可以对元素进行多次推回。我只是尝试使用折叠表达式将多个向量合并为单个向量。我知道还有其他方法可以合并向量,但我想使用折叠表达式

#include <iostream>
#include <vector>

template<typename T, typename... Args>
void push_back_vec(std::vector<T>& v, Args&&... args)
{
    (v.push_back(args), ...);
}
int main()
{

    std::vector<int> v;
    std::vector<int> m ={1,2,3};
    std::vector<int> x ={4,5,6};
    std::vector<int> y ={7,8,9};

    //push_back_vec(v,m,x,y);

    push_back_vec(v, 66, 67, 68);
    for (int i : v) std::cout << i << ' ';

}
试图使下面的语句起作用,将m,x,y向量添加到v

 push_back_vec(v,m,x,y);
目前,对于下面的“无匹配参数”//行,出现错误

push_back_vec(v,m,x,y);
这里需要改变什么

void push_back_vec(std::vector<T>& v, Args&&... args)
void push_-back_-vec(std::vector&v,Args&…Args)

要连接2个向量,可以执行以下操作

template <typename T>
void concatenate(std::vector<T>& v, const std::vector<T>& v2)
{
    v.insert(v.end(), v2.begin(), v2.end());
}
template <typename T, typename ... Ts>
void concatenate(std::vector<T>& v, const Ts&... ts)
{
    (v.insert(v.end(), ts.begin(), ts.end()), ...);
}
模板
无效连接(std::vector&v,const std::vector&v2)
{
v、 插入(v.end(),v2.begin(),v2.end());
}
所以要连接N个向量,您可以

template <typename T>
void concatenate(std::vector<T>& v, const std::vector<T>& v2)
{
    v.insert(v.end(), v2.begin(), v2.end());
}
template <typename T, typename ... Ts>
void concatenate(std::vector<T>& v, const Ts&... ts)
{
    (v.insert(v.end(), ts.begin(), ts.end()), ...);
}
模板
无效连接(std::vector&v,const Ts&…Ts)
{
(v.insert(v.end(),ts.begin(),ts.end()),…);
}
如果希望同一函数附加值或向量,可以添加多个重载:

template <typename T>
void append(std::vector<T>& v, const std::vector<T>& v2)
{
    v.insert(v.end(), v2.begin(), v2.end());
}

template <typename T>
void append(std::vector<T>& v, const T& value)
{
    v.push_back(value);
}
模板
void追加(std::vector&v,const std::vector&v2)
{
v、 插入(v.end(),v2.begin(),v2.end());
}
模板
无效附加(标准::向量和向量、常量和值)
{
v、 推回(值);
}
然后

template<typename T, typename... Args>
void push_back_vec(std::vector<T>& v, Args&&... args)
{
    (Concat(v, args), ...);
}
模板
void push_back_vec(std::vector&v,Args&&…Args)
{
(Concat(v,args),…);
}

要连接两个向量,可以执行以下操作

template <typename T>
void concatenate(std::vector<T>& v, const std::vector<T>& v2)
{
    v.insert(v.end(), v2.begin(), v2.end());
}
template <typename T, typename ... Ts>
void concatenate(std::vector<T>& v, const Ts&... ts)
{
    (v.insert(v.end(), ts.begin(), ts.end()), ...);
}
模板
无效连接(std::vector&v,const std::vector&v2)
{
v、 插入(v.end(),v2.begin(),v2.end());
}
所以要连接N个向量,您可以

template <typename T>
void concatenate(std::vector<T>& v, const std::vector<T>& v2)
{
    v.insert(v.end(), v2.begin(), v2.end());
}
template <typename T, typename ... Ts>
void concatenate(std::vector<T>& v, const Ts&... ts)
{
    (v.insert(v.end(), ts.begin(), ts.end()), ...);
}
模板
无效连接(std::vector&v,const Ts&…Ts)
{
(v.insert(v.end(),ts.begin(),ts.end()),…);
}
如果希望同一函数附加值或向量,可以添加多个重载:

template <typename T>
void append(std::vector<T>& v, const std::vector<T>& v2)
{
    v.insert(v.end(), v2.begin(), v2.end());
}

template <typename T>
void append(std::vector<T>& v, const T& value)
{
    v.push_back(value);
}
模板
void追加(std::vector&v,const std::vector&v2)
{
v、 插入(v.end(),v2.begin(),v2.end());
}
模板
无效附加(标准::向量和向量、常量和值)
{
v、 推回(值);
}
然后

template<typename T, typename... Args>
void push_back_vec(std::vector<T>& v, Args&&... args)
{
    (Concat(v, args), ...);
}
模板
void push_back_vec(std::vector&v,Args&&…Args)
{
(Concat(v,args),…);
}

您发布了一些代码。它有用吗?为什么?你发了一些代码。它有用吗?为什么或者为什么不呢?我不明白为什么“Concat”在应该是“append”的时候用。我不明白为什么“Concat”在应该是“append”的时候用。