数组下标c ++

时间:2016-05-22 09:42:37

标签: c++ arrays vector compiler-errors

我想编译以下c ++代码。我返回此错误:"数组下标"的无效类型'int [unsigned int]'。我该怎么解决?它似乎与分配数组和向量有关。愿任何人帮助我吗?

#include <iostream>
#include <limits.h>
#include <string.h>
#include <queue>
#include <vector>
using namespace std;

/* Returns true if there is a path from source 's' to sink 't' in
residual graph. Also fills parent[] to store the path */
bool bfs(vector<int> graphvec, unsigned int Ng , int s, int t, int parent[])
{

int rGraph[Ng][Ng];
for (unsigned int ii=0; ii<Ng; ii++){
    for (unsigned int jj=0; jj<Ng; jj++)
        rGraph[ii][jj]=graphvec[ii][jj];
} 

int V=Ng;
// Create a visited array and mark all vertices as not visited
bool visited[V];
memset(visited, 0, sizeof(visited));

// Create a queue, enqueue source vertex and mark source vertex
// as visited
queue <int> q;
q.push(s);
visited[s] = true;
parent[s] = -1;

// Standard BFS Loop
while (!q.empty())
{
    int u = q.front();
    q.pop();

    for (int v=0; v<V; v++)
    {
        if (visited[v]==false && rGraph[u][v] > 0)
        {
            q.push(v);
            parent[v] = u;
            visited[v] = true;
        }
    }
}

// If we reached sink in BFS starting from source, then return
// true, else false
return (visited[t] == true);
}




// Returns tne maximum flow from s to t in the given graph
int fordFulkerson(vector<int> graphvec, unsigned int Ng , int s, int t)
{

int graph[Ng][Ng];
for (unsigned int ii=0; ii<Ng; ii++){
    for (unsigned int jj=0; jj<Ng; jj++)
        graph[ii][jj]=graphvec[ii][jj];
}

int V=Ng;
int u, v;

// Create a residual graph and fill the residual graph with
// given capacities in the original graph as residual capacities
// in residual graph
int rGraph[V][V]; // Residual graph where rGraph[i][j] indicates 
                 // residual capacity of edge from i to j (if there
                 // is an edge. If rGraph[i][j] is 0, then there is not)  
for (u = 0; u < V; u++)
    for (v = 0; v < V; v++)
         rGraph[u][v] = graph[u][v];

int parent[V];  // This array is filled by BFS and to store path

int max_flow = 0;  // There is no flow initially

// Augment the flow while tere is path from source to sink
while (bfs(rGraph, s, t, parent))
{
    // Find minimum residual capacity of the edhes along the
    // path filled by BFS. Or we can say find the maximum flow
    // through the path found.
    int path_flow = INT_MAX;
    for (v=t; v!=s; v=parent[v])
    {
        u = parent[v];
        path_flow = min(path_flow, rGraph[u][v]);
    }

    // update residual capacities of the edges and reverse edges
    // along the path
    for (v=t; v != s; v=parent[v])
    {
        u = parent[v];
        rGraph[u][v] -= path_flow;
        rGraph[v][u] += path_flow;
    }

    // Add path flow to overall flow
    max_flow += path_flow;
}

// Return the overall flow
return max_flow;
}




int main()
{
unsigned int NP,Ng;   // Number of paired numbers
unsigned int ii,kk,jj;    

cout << "Enter number of paired numbers(<100): \n";
cin  >> NP;
int PNset[NP][2];
int Nequ[NP*3];
vector<int> Neq;
vector<int> graphvec;
cout << "\n";
cout << "Now, enter the paired numbers: \n";

kk=0;
for (ii=0; ii<NP; ii++) {
cout << ii+1 << ": \n";
cin  >> PNset[ii][0];
cin  >> PNset[ii][1];
Nequ[kk]=PNset[ii][0]+PNset[ii][1];  kk++;
Nequ[kk]=PNset[ii][0]-PNset[ii][1];  kk++;
Nequ[kk]=PNset[ii][0]*PNset[ii][1];  kk++;

}

cout << "\n";
for (ii=0; ii<NP*3; ii++) {
cout << Nequ[ii] << " ";
}

cout << "\n";
Neq.push_back(Nequ[0]);

for (int i = 1; i < NP*3 ; i++){
bool matching = false;
for (int j = 0; (j < i) && (matching == false); j++)
        if (Nequ[i] == Nequ[j]) 
            matching = true;
if (!matching){
       Neq.push_back(Nequ[i]); 
    }
}

for (ii=0; ii<Neq.size(); ii++) {
cout << Neq[ii] << " ";
}

cout << "\n";

Ng=2+NP+Neq.size();
int graph[Ng][Ng];

for (ii=0; ii<Ng; ii++){

  if (ii==0){
     for (jj=0; jj<Ng; jj++){      
         if (jj>NP)
              graph[ii][jj]=1;
         else 
              graph[ii][jj]=0;
         graph[ii][Ng-1]=0;
       }
   }     

   if (ii>0 && ii<=NP){
      for (jj=0; jj<Ng; jj++){
          if (jj==Ng-1)
              graph[ii][jj]=1;
          else 
              graph[ii][jj]=0;          
      }
   }   

   if (ii>NP && ii<Ng-1){
      for (jj=0; jj<Ng; jj++){
          if (jj>0 && jj<=NP){
             if (Neq[ii-NP-1]==(PNset[jj-1][0]+PNset[jj-1][1]) || Neq[ii-NP-1]==(PNset[jj-1][0]-PNset[jj-1][1]) || Neq[ii-NP-1]==(PNset[jj-1][0]*PNset[jj-1][1]))
                graph[ii][jj]=1;           
             else 
                graph[ii][jj]=0;          
          }
          else
             graph[ii][jj]=0;
      } 
   }

   if (ii==Ng-1){
      for (jj=0; jj<Ng; jj++)
          graph[ii][jj]=0;                        
   } 
}  



for (ii=0; ii<Ng; ii++){
 for (jj=0; jj<Ng; jj++)
     cout << graph[ii][jj] << " ";
 cout << "\n";                  
}  

graphvec.resize(Ng,Ng);
for (ii=0; ii<Ng; ii++){
 for (jj=0; jj<Ng; jj++)
     graphvec[ii][jj]=graph[ii][jj];
} 

cout << "The maximum possible flow is " << fordFulkerson(graphvec, Ng , 0 , Ng-1);

cout << "\n"; 

return 0;
}

1 个答案:

答案 0 :(得分:1)

问题在于bfs()你写的

rGraph[ii][jj]=graphvec[ii][jj];

因此graphvec是一个二维数组,您将graphvec作为vector<int> graphvec传递,因此是一维向量。要匹配维度,应为vector<vector<int> >

fordFulkerson()中的同一问题(graphvec是一维向量)

graph[ii][jj]=graphvec[ii][jj];

并在main()中(graphvec是一维向量)

graphvec[ii][jj]=graph[ii][jj];

不仅:您致电bfs

while (bfs(rGraph, s, t, parent))

其中rGraph是2D C风格的数组。

不仅:考虑标准C ++禁止变长数组;所以

int rGraph[Ng][Ng];
bool visited[V];
int graph[Ng][Ng];
int rGraph[V][V];
int parent[V]; 
int PNset[NP][2];
int Nequ[NP*3];
int graph[Ng][Ng];

是非法的ISO C ++声明。

建议:用C ++向量替换所有C风格的数组;所以[警告:未经测试]

std::vector<std::vector<int> > rGraph(Ng, std::vector<int>(Ng));
std::vector<bool> visited(V);
std::vector<std::vector<int> > graph(Ng, std::vector<int>(Ng));
std::vector<std::vector<int> > rGraph(V, std::vector<int>(V));
std::vector<int> parent(V); 
std::vector<std::vector<int> > PNset(NP, std::vector<int>(2));
std::vector<int> Nequ(NP*3);
std::vector<std::vector<int> > graph(Ng, std::vector<int>(Ng));

p.s:抱歉我的英语不好。