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();
}