我正在尝试绘制 LCS 表。我的这部分代码在fill_the_table()
函数内部 while 循环的第一次迭代后停止。我找不到我的代码有什么问题。顺便说一句,我是 C++ 新手...
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
#define U 1
#define L 2
#define UL 3
void fill_the_table(ifstream& file1, ifstream& file2, int **table, int **p){
table[0][0]=0;
p[0][0]=UL;
string line1, line2;
int i=1, j=1;
file1.clear();
file1.seekg(0, ios::beg);
file2.clear();
file2.seekg(0, ios::beg);
while(getline(file1, line1)){
table[0][j]=0;
p[0][j]=L;
while(getline(file2, line2)){
table[i][0]=0;
p[i][0]=L;
if(line1==line2){
table[i][j]=table[i-1][j-1]+1;
p[i][j]=UL;
i++;
}
else{
if(table[i-1][j]>table[i][j-1]){
table[i][j]=table[i-1][j];
p[i][j]=U;
}
else{
table[i][j]=table[i][j-1];
p[i][j]=L;
}
i++;
}
j++;
}
}
cout<<table[i][j];
return;
}
void table(ifstream& file1, ifstream& file2){
int i=0, j=0;
string line1, line2;
while(getline(file1, line1)){
i++;
}
while(getline(file2, line2)){
j++;
}
int** table = new int* [i];
for(int a=0; a<i; a++){
table[a] = new int [j];
}
int** p = new int* [i];
for(int a=0; a<i; a++){
p[a] = new int [j];
}
fill_the_table(file1, file2, table, p);
for(int a=0; a<i; a++){
delete[] table[j];
}
delete table;
for(int a=0; a<i; a++){
delete[] p[j];
}
delete p;
return;
}
int main(int argc, char* argv[]){
ifstream file1(argv[1]);
ifstream file2(argv[2]);
table(file1, file2);
return 0;
}
- 我可以通过从到的方式
int** table
和方式吗?int** p
table()
fill_the_table()
- 有人建议我使用
vector<string>
而不是自己阅读文件,哪个更可取?