首页 > 解决方案 > "Operator mismatch" error in c++

问题描述

I'm creating a transpose of the graph v and saving it in g2. Both v and g2 are same type, so g2 will hold the transpose of the graph.

Below, v is of vector<int>v[1000]. It consists a adjacency list representation of a graph.

vector<int> v[10000] = ...; // input: some matrix
vector<int> g2[10000]; // output: transposed matrix

for(int u=0;u<N;u++) //N is the number of vertices
    for(vector<int>::iterator it=v[u].begin();it!=v[u].end();it++) 
      g2[v[*it]].push_back(u);

I'm getting this error:

prog.cpp:74:8: error: no match for 'operator[]' (operand types are 'std::vector<int> [10001]' and 'std::vector<int>')
   g2[v[*it]].push_back(u);
    ^    

Can you guys please help me?

标签: c++c++11graph

解决方案


vector<int> g2[10000];
for(int u=0;u<N;u++) //N is the number of vertices
{ 
      for(vector<int>::iterator it=v[u].begin();it!=v[u].end();it++) 
      {
         g2[u].push_back(*it);
      }
}

我希望这将有所帮助。


推荐阅读