I have a function which accepts array of vectors of different sizes I want to know how many vectors are present in array
int dfs(vector<int> g[])
{
int n=?;//(no. of vector in array)
}
I have a function which accepts array of vectors of different sizes I want to know how many vectors are present in array
int dfs(vector<int> g[])
{
int n=?;//(no. of vector in array)
}
There is no way to do this in C++: an array type in a function parameter decays to a pointer, which has no size information.
The best option is to take the parameter as a std::vector<std::vector<int>>
, but you mention in a comment that that is not possible.
One other option, if you are sure you will be passed an array (in comparison to a pointer to an array) is to change the definition to something like this:
template <std::size_t n>
int dfs(std::vector<int>(&g)[n]) {
// n is the number of elements in the array
}