Basically I am making a graph that represents movie data. I have a class called Vertice and a class called Actor. Vertice contains a vector of Actors and in my main function I have a vector of Vertices which is the graph.
Basically, as I parse through the movie data file my code is working properly and storing neighborhood size. However, once I exit the initial while loop that goes through the data file, all the information about neighbors and neighborhood size is getting lost. I am not resetting anything and I am not sure why this is happening.
I have a function called addToNeighborhood that adds a new actor to a neighborhood and this is where neighborhood size gets increased. As a test, I called addToNeighborhood on a random neighborhood once I was outside of the while loop. Now, this particular actor's neighborhood size was 2 inside the while loop. Then when I inserted a random name to it's neighborhood outside the while loop it returned that it's size was 1, showing that it reverted back to zero for some reason outside the while loop.
I wouldn't normally ask about something so specific as this but I've been trying to find the problem all day and I think maybe I am just understanding vectors incorrectly or something.
#include <iostream>
#include <fstream>
#include "ctype.h"
#include <string>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <stack>
#include <queue>
#include <unordered_map>
using namespace std;
class Actor
{
public:
int name;
string nameStr;
string title;
};
class Vertice
{
public:
string name;
int neighborhood_size;
vector<class Actor> neighborhood;
void printNeighborhood();
void addToNeighborhood(int, string, string);
};
void Vertice::addToNeighborhood(int actor_name, string actor_string, string movie_title)
{
Actor to_add;
to_add.name = actor_name;
to_add.title = movie_title;
to_add.nameStr = actor_string;
neighborhood.push_back(to_add);
neighborhood_size++;
Actor print = neighborhood.back();
cout << "Just added " << print.nameStr << " to " << name << "'s neighborhood.\n";
cout << "The size of the neighborhood is now: " << neighborhood_size << ".\n\n";
}
void Vertice::printNeighborhood()
{
cout << "The neighborhood of " << name << " is: ";
for(int j = 0; j < neighborhood_size; j++)
{
cout << (neighborhood[j]).nameStr << " ";
}
}
int main(int argc, char** argv)
{
ifstream data_dump;
data_dump.open("cleaned.txt");
string line;
char *buffer, *piece;
string actor;
vector<class Vertice> myGraph;
vector<string> nameArray;
queue<int> Q;
unordered_map<string, int> actorMap;
int actor_count = 0;
int loop_count = 0;
string movie_title;
while(getline(data_dump, line)) { // each line of movie file
buffer = strdup(line.c_str());
piece = strtok(buffer, " ");
movie_title = piece; // get the first token, that is the movie title
piece = strtok(NULL, " "); // get the next token, that will be first actor
while (piece != NULL) { // while more actors to get
actor = piece; //string name of actor
if (actorMap.find(actor) == actorMap.end()) { // we haven't seen this actor before
// add to the map
// add to the graph
// add to nameArray
// increment total actors
}
int to_queue = actorMap[actor];
Q.push(to_queue);
loop_count++; // actors in just this movie
piece = strtok(NULL, " "); // grab the next actor
}
//second pass through updates neighborhoods
for(int i=0; i < loop_count; i++) {
int top = Q.front();
Q.pop();
Vertice to_update = myGraph[top];
for (int j = 0; j < loop_count-1; j++) {
int next = Q.front();
// add next to top's neighborhood
// enqueue next back into Q
}
Q.push(top);
}
while (!Q.empty()) Q.pop(); // make Q empty
loop_count = 0;
} // go on to the next line of the movie file
// just printing a random neighborhood to test
Vertice temp = myGraph[0];
temp.printNeighborhood();
}