不同大小的向量数组的大小 cpp

Size of array of vectors of different sizes cpp

本文关键字:cpp 数组 向量      更新时间:2023-10-16

我有一个接受不同大小的向量数组的函数,我想知道数组中存在多少个向量

int dfs(vector<int> g[])
{
int n=?;//(no. of vector in array)
}

在C++中没有办法做到这一点:函数参数中的数组类型衰减到没有大小信息的指针。

最好的选择是将参数作为std::vector<std::vector<int>>,但您在评论中提到这是不可能的。

如果您确定将传递数组(与指向数组的指针相比(,另一种选择是将定义更改为如下所示的内容:

template <std::size_t n>
int dfs(std::vector<int>(&g)[n]) {
// n is the number of elements in the array
}