当我使用第一个输入文件运行代码时,代码工作正常,但是当我使用第二个输入文件Dev-C ++ 4.9.9.2编译器时,向我显示警告:“在您的中引发了访问冲突(分段错误)程序“指向该行:
__test = __grouping_tmp[__i] == __grouping[__min];
编译器的文件locale_facets.tcc。 我一直在寻找可能的解决方案,但没有任何问题,有人可以帮助我吗?
这是我的代码:
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
vector< vector<int> > adj;
vector <string> towns;
vector <int> w;//weights
vector <int> v;//visited
string town1,town2;
int n,i,j,k,l;
void dfs(int start){
int c,size;
v[start]=1;
size=adj[start].size();
for (c=0;c<size;c++)
//if ((adj[start][c]==1) && (v[c]==0))
//if (v[c]==0)
dfs(adj[start][c]);
}
int findi(string name){ //Finds the I of the town
for(i=0;i<towns.size();i++){
if(name==towns[i]){
return i;
}
}
return 101;//Dead END
}
int main(){
ifstream fin ("B.in");
ofstream fout ("B.out");
towns.push_back("Start");
w.push_back(0);
bool found;
while(!fin.eof()){
fin>>town1>>k>>town2>>l;
found=false;
for(i=0;i<towns.size();i++){//search an ksanabike i poli
if(towns[i]==town1){
found=true;
}
}
if(!found){
towns.push_back(town1);
w.push_back(k);
}
}
towns.push_back("Finish");
w.push_back(10001);
for(i=1;i<towns.size()-1;i++){//sorting by weight
for(j=i+1;j<towns.size();j++){
if(w[i]>w[j]){
k=w[i];
w[i]=w[j];
w[j]=k;
town1=towns[i];
towns[i]=towns[j];
towns[j]=town1;
}
}
}
int c;
string prev;
c=0;
prev=towns[0];
for(i=0;i<towns.size();i++){//sort towns with same weight
if(towns[i]==prev){
c++;
}
if(towns[i]!=prev&&c!=0){
sort(towns.begin()+i-c-1,towns.begin()+i-1);
}
//Arxikopiisi visited je grafou
v.push_back(0);
adj.push_back(vector<int> ());
}
fin.clear();//reset file read!!
fin.seekg(0, std::ios::beg);
while(!fin.eof()){//Dimiourgia grafou
fin>>town1>>k>>town2>>l;
//cout<<town1<<" "<<town2<<endl;
adj[findi(town1)].push_back(findi(town2));
}
dfs(0);
//cout<<"v size:"<<v.size();
for(i=1;i<towns.size()-1;i++){
if(v[i]==1){
fout<<towns[i]<<endl;
}
}
fin.close();
fout.close();
return 0;
}
有效的输入文件 - &gt;
开始0拉纳卡40
Paphos 70 Finish 10001
尼科西亚90完成10001
Larnaca 40 Paphos 70
Limasol 50 Paphos 70
Limasol 50 Larnaca 40
Nicosia 90 Limasol 50
提供错误的输入文件 - &gt;
帕福斯40完成10001
尼科西亚90完成10001
开始0拉纳卡40
Larnaca 40 Paphos 40
Limasol 40 Larnaca 40
Larnaca 40 Limasol 40
Nicosia 90 Limasol 40