Ich möchte den folgenden C++ Code kompilieren. Ich gebe diesen Fehler zurück: "ungültige Typen 'int [unsigned int]' für Array-Index". Wie könnte ich es lösen? Es scheint, dass es mit dem Zuweisen von Array und Vektoren zusammenhängt. Kann mir jemand helfen?Ungültige Typen 'int [unsigned int]' für Array-Index C++
#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;
}
Warum verwenden Sie nicht einen 'std :: vector' für alle Array-Anforderungen? und 'int rGraph [Ng] [Ng];' Soweit ich weiß, Arrays variabler Länge werden in Standard C++ nicht unterstützt. –
Die beiden aufrufenden Funktionen werden von woanders kopiert. Ich habe die Eingabe-Arrays von ihnen in Vektoren geändert. Das Problem besteht in der Zuordnung von Vektoren zu Arrays und umgekehrt. Wie könnte ich es tun? –
anstatt 'int rGraph [Ng] [Ng]' zu verwenden, warum nicht 'std :: vector> rGraph' verwenden und die äußeren und inneren Vektoren um die Größe von' Ng' verändern. Danach können Sie auf den Vektor wie ein 2D-Array zugreifen, wenn Sie möchten. –