Yoyo_adrian 2023-02-27 15:19 采纳率: 94.7%
浏览 14
已结题

为什么用stoi后显示abort已被调用错误

用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;
}

错误:

img

img

恳请解惑!万分感激!

  • 写回答

1条回答 默认 最新

  • 小陈程序 2023-02-27 18:35
    关注

    这可能是由于文件内容不是有效的字符串,stoi函数无法将其转换为可用的整数而导致的。为了避免出现这种情况,应该使用有效的字符串来读取文件内容,以确保stoi函数可以正常工作。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月7日
  • 已采纳回答 2月27日
  • 创建了问题 2月27日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效