检查STL容器长度相等的变分函数
Variadic function to check equal lengths of STL containers
我正在编写我的第一个可变函数模板。我得到错误error: parameter packs not expanded with ‘...’:
。也许我无法理解这里的一些简单概念。我想将在iterators
中传递的所有内容存储在vector
中。做这件事的正确方法是什么?
template<typename... Iterator>
bool IsEqualLength(Iterator&... its)
{
std::vector<Iterator> vec_its {its...};
int head = vec_its.front().size();
bool is_diff_size = std::any_of(vec_its.begin(), vec_its.end(),
[&head](Iterator& cur){return cur.size() != head;});
if(is_diff_size)
{
return false;
} else {
return true;
}
}
这无法使用(在Ubuntu上的gcc 4.8.4下)进行编译:
../proc.h: In function ‘bool IsEqualLength(Iterator& ...)’:
../proc.h:32:24: error: parameter packs not expanded with ‘...’:
std::vector<Iterator> vec_its {its...};
^
../proc.h:32:24: note: ‘Iterator’
../proc.h:35:87: error: parameter packs not expanded with ‘...’:
您在该语句中使用了两个不同的包:
std::vector<Iterator> vec_its {its...};
its
是扩展的,但Iterator
不是单一的类型。。。它也是一个包,但您无法扩展它。因此出现了错误(特别指出Iterator
)。
如果你只需要容器的大小,你可以对所有传入的容器(容器,而不是迭代器!)调用size()
,并将其放入数组中(不需要动态分配):
template <typename... Container>
bool isEqualLength(Container&&... cs) {
size_t sizes[] = {cs.size()...};
return std::all_of(std::begin(sizes), std::end(sizes),
[&sizes](size_t cur){return cur == sizes[0]; });
}
相关文章:
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 在C应用程序中运行C++(带有STL)函数
- 检查函数返回类型是否与STL容器类型值相同
- STL算法函数在多个一维容器上的使用
- 如果我真的真的想从 STL 容器继承,并且我继承构造函数并删除新运算符,会发生什么?
- 有没有办法捕获 STL 函数未被赋予正确参数的异常?
- 如何在 STL 函数中找到传递给谓词的元素的索引?
- 不能对类型化模板使用 STL 函数
- 如何使用 binary_search STL 函数在嵌套类中搜索整数?
- C++ 如何将参数传递给 STL 函数
- 如何将 STL 队列推送函数绑定到 std::函数?
- STL 函数和函数类型与函数指针类型
- 接受任何 STL 容器的函数
- 重载 STL 函数
- 为什么 stl 字符串C++函数有时出错有时会出错?
- 如何将元素从 STL 队列传递到函数?
- 成员函数不能为集合迭代器和const_iterator的输入重载(但可以为其他 STL 迭代器重载)
- C++如何获取传递给函数(STL 迭代器)的参数的名称
- 为什么 STL 函数使用节点的颜色来计算 std::map 节点前身