-1

基本上我需要模板函数,它将容器的大小与一些常数进行比较。所以我需要制作容器的 std::vector 并检查谓词compSize是否为真。

template < int x, class Container >
bool compSize(Container cont)
{
    return x == cont.size();
}
    
template < int x, class ...Container >
bool compSizeMult(Container ...conts)
{
    std::vector< Container > cvector{ conts... };
    return std::all_of(cvector.cbegin(), cvector.cend(), compSize< x, Container >);
}

但是编译器说,该参数包没有在

cvector{ 续... };

我也想使用compSizeMult作为 std::sort 的比较器并制作类似的东西

int main()
{
    std::vector< std::vector< int > > vec{{1}, {2}, {3}, {0}, {0}};
    std::sort(vec.begin(), vec.end(), compSizeMult< 0, std::vector< int > >);
}

这就是为什么函数应该接受多个参数的原因。我不能在这个任务中使用循环和 lambda,只能使用标准算法

4

1 回答 1

0

你有 2 个可变参数

std::vector<Container> cvector{ conts... };
  • conts有它的变量...
  • Container没有它的类型...(不在 中compSize<x, Container>)。

你可能想要std::common_type_t<Container...>.

在 C++17 中,您可以使用折叠表达式

template < int x, class ... Containers >
bool compSizeMult(Containers&&... conts)
{
    return (compSize<x>(conts) && ...);
    // or directly: return (conts.size == x && ...);
}

我想compSizeMult用作比较器

它不是一个有效的比较器。它打破了严格的弱排序

于 2021-06-02T07:43:23.987 回答