用string读文件内容时,为了把读取到的string类型内容转换为整型数字,我用了stoi,但是在运行时却显示abort已被调用,这是为什么呢?
代码:
//利用Fleury算法求欧拉回路
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
using namespace std;
#define INFINITY INT_MAX // 定义最大值
#define MAX_V 30 // 最大顶点数目
int i, j, k;
typedef struct
{
string code; // 顶点编号
}VertexType; // 顶点类型定义
typedef struct
{
int arcs[MAX_V + 1][MAX_V + 1]; // 邻接矩阵
int vexnum, arcnum; // 图包含的顶点数与边的个数
VertexType vexs[MAX_V + 1]; // 存放顶点信息
} MGraph;
struct
{
int vexcode;
int lowcost;
bool tag;
} closest[MAX_V + 1]; // 定义辅助数组 closest 的结构
//利用邻接矩阵打印无向图
void PrintGraph(MGraph G)
{
cout << "----------矩阵输出----------" << endl;
for (i = 1; i <= G.vexnum; i++)
cout << "\t" << G.vexs[i].code;
cout << endl;
for (i = 1; i <= G.vexnum; i++) {
cout << G.vexs[i].code << "\t";
for (j = 1; j <= G.vexnum; j++) {
if (G.arcs[i][j] == INFINITY)
cout << "∞" << "\t";
else cout << G.arcs[i][j] << "\t";
}
cout << endl;
}
}
//创建图
void CreateGraph(MGraph& G)
{
ifstream ifs;
ifs.open("D:\\欧拉回路.txt", ios::in);
if (!ifs.is_open()) {
cout << "读取文件失败!" << endl;
exit(-1);
}
string tmp;
int count = 0;
while (getline(ifs, tmp, '\n')) {
//读取顶点数、边数、顶点编号
if (count == 0)
G.vexnum = stoi(tmp);
else if (count == 1) {
G.arcnum = stoi(tmp);
for (i = 1; i <= G.vexnum; i++)
for (j = 1; j <= G.vexnum; j++)
G.arcs[i][j] = INFINITY; // 邻接矩阵初始化,所有元素初始值为 ∞
}
else if (count == 2) {
istringstream string_to_string(tmp);
for (i = 1; i <= G.vexnum; i++) {
string str_tmp;
string_to_string >> str_tmp;
G.vexs[i].code = str_tmp;
}
}
//读取带权边
else {
istringstream string_to_num(tmp);
int arr_tmp[2];
for (int i = 0; i < 2; i++) {
string str_tmp;
string_to_num >> str_tmp;
arr_tmp[i] = stoi(str_tmp);
}
G.arcs[arr_tmp[0]][arr_tmp[1]] = 1;
G.arcs[arr_tmp[1]][arr_tmp[0]] = 1;
}
count++;
}
PrintGraph(G);
ifs.close();
}
int main()
{
MGraph G;
CreateGraph(G);
return 0;
}
错误:
恳请解惑!万分感激!