分割故障:11无转换中止陷阱:6

发布于 2025-01-22 04:54:04 字数 3764 浏览 2 评论 0原文

我是C ++ 11的新手,我正在尝试使用IMDB的大数据集(在Mac上工作,这是我希望这个问题)。

我想实现三个函数:read_graph,它从txt文件读取行(来自tsv.gz文件并使用.sh脚本修改),build_graph,它使用 vector 构建图形在读取引用的txt文件和打印图形的print_graph时。我遇到的错误(在OS壳上编译)是分段故障:11 libc ++ abi:终止未被发现的类型std :: invalid_argument,stoi:stoi:no conversionion 中止陷阱:6 (但由于我更改了代码的一点点,所以我并不会对以前生成WRT的生成),我认为问题在build_graph内部,因为如果我从代码中将其删除编译。对可能出了什么问题的想法吗?任何帮助或建议都将不胜感激,这是守则:

   #include <iostream>
    #include <iomanip>
    #include <vector>
    #include <string>
    #include <queue>
    #include <list>
    #include <stack>
    #include <fstream> // getline
    #include <algorithm> // find
    #include <math.h> // ceil
    #include <sys/time.h> // gettimeofday
    
    using namespace std;
    
    constexpr int MAXN=13000000;
    vector<int> adj[MAXN];
    
    struct character{
        int id;
        string name;
    };
    
    struct movie {
        int id;
        string name;
    };
    
    vector<character> all_actors;
    vector<movie> all_movies;
    
    void read_graph()
    {
        ifstream actors_ress("act_s.txt");
        ifstream movie_s("filtered_movies.txt");
    
        string s,t;
        string del=" ";
    
        while(getline(actors_ress,s)) {
                character tmp;
                tmp.id=stoi(s.substr(0, s.find(del)));
                tmp.name=s.substr(s.find(del)+1);
                all_actors.push_back(tmp);
            }
            while(getline(movie_s,t)) {
                movie tmp;
                tmp.id=stoi(t.substr(0, t.find(del)));
                tmp.name=t.substr(t.find(del)+1);
                all_movies.push_back(tmp);
            }
        }
    
        void build_graph()
        {
            ifstream relations("connections.txt");
            string s,t,r;
    
            int u,v;
            string del=" ";
            vector<int> troupe;
    
            getline(relations,s);
            int current_movie=stoi(s.substr(0, s.find(del)));
    
            while(getline(relations,t)) {
                int next_movie=stoi(t.substr(0, t.find(del)));
    
                if(next_movie == current_movie) {
                    u=stoi(s.substr(s.find(del)+1));
                    troupe.push_back(u);
                    while((next_movie == current_movie)&&(getline(relations,r))) {
                        v=stoi(t.substr(t.find(del)+1));
                        troupe.push_back(v);
                        t=r;
                        next_movie=stoi(t.substr(0, t.find(del)));
                    }
    
                    for(auto first=troupe.begin(); first!=troupe.end(); ++first) {
                        for(auto second=first+1; second!=troupe.end(); ++second) {
                            u=*first;
                            v=*second;
                            if(find(adj[u].begin(), adj[u].end(), v) == end(adj[u])) {
                                adj[u].push_back(v);
                                adj[v].push_back(u);
                            }
                        }
                    }
                }
                current_movie=next_movie;
                s=t;
                troupe.clear();
            }
        }
    
        void print_graph()
        {
            for(int u=1; u<MAXN; ++u) {
                if(!adj[u].empty()) {
                    cout << u << ": ";
                    for(auto v:adj[u]) {
                        cout << v << "\t";
                    }
                    cout << endl;
                }
            }
        }
    
    int main() {
    
    read_graph();
    build_graph();
    print_graph();
    }

I'm new to C++11, I'm trying to use a large dataset from IMDb (working on mac, that's I hope the issue).

I'd like to implement three functions: read_graph, which reads lines from a txt file (coming from a tsv.gz file and modified with a .sh script), build_graph, which build the graph using vector while reading the txt files cited, and print_graph, which print the graph. The error I get (compiling on OS shell) is Segmentation Fault: 11 or libc++abi: terminating with uncaught exception of type std::invalid_argument: stoi: no conversion
Abort trap: 6
(but I don't exaclty how this was generated wrt the previous since I changed a little bit of the code) and I think the problem is inside build_graph, since if I remove it from the code it compiles. Any idea on what might have went wrong? Any help or advice would be appreciated, here's the code:

   #include <iostream>
    #include <iomanip>
    #include <vector>
    #include <string>
    #include <queue>
    #include <list>
    #include <stack>
    #include <fstream> // getline
    #include <algorithm> // find
    #include <math.h> // ceil
    #include <sys/time.h> // gettimeofday
    
    using namespace std;
    
    constexpr int MAXN=13000000;
    vector<int> adj[MAXN];
    
    struct character{
        int id;
        string name;
    };
    
    struct movie {
        int id;
        string name;
    };
    
    vector<character> all_actors;
    vector<movie> all_movies;
    
    void read_graph()
    {
        ifstream actors_ress("act_s.txt");
        ifstream movie_s("filtered_movies.txt");
    
        string s,t;
        string del=" ";
    
        while(getline(actors_ress,s)) {
                character tmp;
                tmp.id=stoi(s.substr(0, s.find(del)));
                tmp.name=s.substr(s.find(del)+1);
                all_actors.push_back(tmp);
            }
            while(getline(movie_s,t)) {
                movie tmp;
                tmp.id=stoi(t.substr(0, t.find(del)));
                tmp.name=t.substr(t.find(del)+1);
                all_movies.push_back(tmp);
            }
        }
    
        void build_graph()
        {
            ifstream relations("connections.txt");
            string s,t,r;
    
            int u,v;
            string del=" ";
            vector<int> troupe;
    
            getline(relations,s);
            int current_movie=stoi(s.substr(0, s.find(del)));
    
            while(getline(relations,t)) {
                int next_movie=stoi(t.substr(0, t.find(del)));
    
                if(next_movie == current_movie) {
                    u=stoi(s.substr(s.find(del)+1));
                    troupe.push_back(u);
                    while((next_movie == current_movie)&&(getline(relations,r))) {
                        v=stoi(t.substr(t.find(del)+1));
                        troupe.push_back(v);
                        t=r;
                        next_movie=stoi(t.substr(0, t.find(del)));
                    }
    
                    for(auto first=troupe.begin(); first!=troupe.end(); ++first) {
                        for(auto second=first+1; second!=troupe.end(); ++second) {
                            u=*first;
                            v=*second;
                            if(find(adj[u].begin(), adj[u].end(), v) == end(adj[u])) {
                                adj[u].push_back(v);
                                adj[v].push_back(u);
                            }
                        }
                    }
                }
                current_movie=next_movie;
                s=t;
                troupe.clear();
            }
        }
    
        void print_graph()
        {
            for(int u=1; u<MAXN; ++u) {
                if(!adj[u].empty()) {
                    cout << u << ": ";
                    for(auto v:adj[u]) {
                        cout << v << "\t";
                    }
                    cout << endl;
                }
            }
        }
    
    int main() {
    
    read_graph();
    build_graph();
    print_graph();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文