【发布时间】:2015-11-15 10:38:36
【问题描述】:
我想使用带有可变参数的模板函数将许多向量组合为一个。 在我当前的代码下方,我遇到了三个或更多向量的问题:
#include <iostream>
#include <vector>
#include <numeric>
template<typename _Ty, typename ..._Args>
std::vector<_Ty> combine(const std::vector<_Ty> &a, const _Args &...args) {
// Determine size of new vector
std::vector<std::size_t> sizes = { a.size(), args.size()... };
std::size_t size = std::accumulate(sizes.begin(), sizes.end(), 0);
// Create vector with new size
std::vector<_Ty> result(size);
// Insert all vectors into this one
result.insert(a.begin(), a.end(), result.end());
result.insert(result.end(), args.begin()..., args.end()...);
return result;
}
int main(int argc, char *argv[], char *envp[]) {
std::vector<int> a = { 0, 1, 2, 3, 4 };
std::vector<int> b = { 4, 3, 2, 1, 0 };
std::vector<int> c = { 1, 1, 1, 1 };
std::cout << combine(a, b).size() << std::endl;
std::cout << combine(a, b, c).size() << std::endl; // <-- Does not compile
std::cin.ignore();
return 0;
}
所以确切的问题是 combine(a, b, c) 无法编译。我知道为什么。因为这一行:
result.insert(result.end(), args.begin()..., args.end()...);
编译成:
result.insert(result.end(), b.begin(), c.begin(), b.end(), c.end());
但我不知道如何使用可变参数调用 result.insert,因此它会编译为:
result.insert(result.end(), b.begin(), b.end());
result.insert(result.end(), c.begin(), c.end());
一种可能性是:
std::vector<std::vector<_Ty> all = { a, args...};
for (const auto &vec : all) {
result.insert(vec.begin(), vec.end());
}
但这需要所有向量的第二个副本....有什么想法吗?谢谢!
【问题讨论】:
标签: c++ templates c++11 vector arguments