0
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;

vector<int> parent(N);
vector<int> rank(N); // size
void make(int v)
{
   parent[v] = v;
   rank[v] = 1;//"rank" is ambiguous
}
int find(int v)
{
    if (v == parent[v])
    return v;

    return parent[v] = find(parent[v]);
}
void Union(int a, int b)
{
    a = find(a);
    b = find(b);
    if (a != b)
    {
        if (rank[a] < rank[b]) //"rank" is ambiguous
            swap(a, b);
        parent[b] = a;
        rank[a] += rank[b];//"rank" is ambiguous
    }
}

所以我在 C++ 中做不相交联合集 (DSU) 数据结构。我vector<int> parent没有显示任何错误,但vector<int> rank显示出模棱两可的错误。为什么我收到错误:

source>: In function 'void make(int)':
<source>:10:4: error: reference to 'rank' is ambiguous
   10 |    rank[v] = 1;//"rank" is ambiguous
      |    ^~~~
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/move.h:57,
                 from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_pair.h:59,
                 from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_algobase.h:64,
                 from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/specfun.h:45,
                 from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/cmath:1927,
                 from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/x86_64-linux-gnu/bits/stdc++.h:41,
                 from <source>:1:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/type_traits:1319:12: note: candidates are: 'template<class> struct std::rank'
 1319 |     struct rank
      |            ^~~~
<source>:6:13: note:                 'std::vector<int> rank'
    6 | vector<int> rank(N); // size
      |             ^~~~

为什么我会收到此错误?

我如何解决它 ?

4

0 回答 0