C++ 函数不打印任何解决方案
所以,我需要做一个函数,返回图的色数。该图通过邻接矩阵给出,函数使用文件名查找邻接矩阵。我有一个函数,理论上应该可以工作,编译器不会对它抛出任何问题,但当我运行它时,它只是打印出一个空行并结束程序C++ 函数不打印任何解决方案,c++,C++,所以,我需要做一个函数,返回图的色数。该图通过邻接矩阵给出,函数使用文件名查找邻接矩阵。我有一个函数,理论上应该可以工作,编译器不会对它抛出任何问题,但当我运行它时,它只是打印出一个空行并结束程序 #include <iostream> #include <string> #include <fstream> #include <vector> using namespace std; int Find_Chromatic_Number (v
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
int Find_Chromatic_Number (vector <vector <int>> matg, int matc[], int n) {
if (n == 0) {
return 0;
}
int result, i, j;
result = 0;
for (i = 0; i < n; i++) {
for (j = i; j < n; j++) {
if (matg[i][j] == 1) {
if (matc[i] == matc[j]) {
matc[j]++;
}
}
}
}
for (i = 0; i < n; i++) {
if (result < matc[i]) {
result = matc[i];
}
}
return result;
}
int main() {
string file;
int n, i, j, m;
cout << "unesite ime datoteke: " << endl;
cin >> file;
ifstream reader;
reader.open(file.c_str());
reader >> n;
vector<vector<int>> matg(n, vector<int>(0));
int matc[n];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
reader >> matg[i][j];
}
matc[i] = 1;
}
int result = Find_Chromatic_Number(matg, matc, n);
cout << result << endl;
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int Find_color_数(向量matg,int matc[],int n){
如果(n==0){
返回0;
}
int结果,i,j;
结果=0;
对于(i=0;i>n;
向量matg(n,向量(0));
int-matc[n];
对于(i=0;i>matg[i][j];
}
matc[i]=1;
}
int结果=查找色数(matg、matc、n);
试着这样做吧。
不要为向量选择大小
vectormatg;
而不是使用reader>>matg[i][j];
使用:
<代码> int Matc[n];不是有效的C++,请参阅问题为什么。我不知道这是否是所有问题,但是<代码>向量Matg(n,vector(0));< /Cord>定义了0大小向量的向量。随后访问“代码> Matg[i] [j] < /代码>导致未定义行为。<代码>向量Matg(n,vector(n))
会更好。然后将它通过const&
传递到Find_color\u Number
以避免复制。谢谢,这就澄清了为什么它不返回任何内容,但我现在遇到了编译器的问题,因为它无法识别运算符int tmp;
reader >> tmp;
matg[i].push_back(tmp);