kjohansen
September 20th, 2008, 02:46 AM
I am attempting to do some graph algorithms and store the nodes in a map.
I am reading in data from a file and filling the nodes, my program is not reading in the number of nodes it should.
string line;
vector<string> singMovie;
vector<vector<string> > allMovies;
vector<string> actorsInCurrentMovie;
map<string,int> actors;
map<string,int>::const_iterator mlMap;
int actorCount=0;
typedef pair<string,int> Map_Pair;
ifstream myfile("movie-top-grossing.txt");
if(myfile.is_open()){
while(!myfile.eof()){ //while not empty
singMovie.clear(); //clear temp holder
getline(myfile,line); //read line from file
if(line==""){ //if line is empty
break; //get out of loop
}
Tokenize(line,singMovie,"/"); //split the string on the delim
allMovies.push_back(singMovie); //put the current movie into all movies
for(int i=1;i<singMovie.size();i++){ //loop through all actors (movie title is at "0" so start at 1
mlMap=actors.find(singMovie[i]); //search for actor
if(mlMap==actors.end()){ //if actor does not exist
actors.insert(Map_Pair(singMovie[i],actorCount)); //put the actor and the count into the map
actorCount++; //increase actor count
}
}
}
}
cout<<allMovies.size()<<endl<<actorCount;
The number of movies I expect is 187 which I get correctly, but I do not the number of actors I expect. The number of actors I expect is off by 13. I know the file is right.
What I think is happening is that I am using the find function wrong. Since the find function returns an iterator, the iterator might return a result but not the the correct record I am looking for. By that I mean I think the hash function is not unique for these values and the iterator is not equal to the end so it does not add the actor but it should.
How do I check to make sure the find function is returning the right result and not just one with the same hash? Or am I way off base...
I am reading in data from a file and filling the nodes, my program is not reading in the number of nodes it should.
string line;
vector<string> singMovie;
vector<vector<string> > allMovies;
vector<string> actorsInCurrentMovie;
map<string,int> actors;
map<string,int>::const_iterator mlMap;
int actorCount=0;
typedef pair<string,int> Map_Pair;
ifstream myfile("movie-top-grossing.txt");
if(myfile.is_open()){
while(!myfile.eof()){ //while not empty
singMovie.clear(); //clear temp holder
getline(myfile,line); //read line from file
if(line==""){ //if line is empty
break; //get out of loop
}
Tokenize(line,singMovie,"/"); //split the string on the delim
allMovies.push_back(singMovie); //put the current movie into all movies
for(int i=1;i<singMovie.size();i++){ //loop through all actors (movie title is at "0" so start at 1
mlMap=actors.find(singMovie[i]); //search for actor
if(mlMap==actors.end()){ //if actor does not exist
actors.insert(Map_Pair(singMovie[i],actorCount)); //put the actor and the count into the map
actorCount++; //increase actor count
}
}
}
}
cout<<allMovies.size()<<endl<<actorCount;
The number of movies I expect is 187 which I get correctly, but I do not the number of actors I expect. The number of actors I expect is off by 13. I know the file is right.
What I think is happening is that I am using the find function wrong. Since the find function returns an iterator, the iterator might return a result but not the the correct record I am looking for. By that I mean I think the hash function is not unique for these values and the iterator is not equal to the end so it does not add the actor but it should.
How do I check to make sure the find function is returning the right result and not just one with the same hash? Or am I way off base...