vector.push_back don't work

I am trying to push back elements to a vector, but it doesn't seem to work.

I am trying to create a graph. The graph is stored as a vector of edges. Each edge is defined as such

1
2
3
4
5
6
7
struct Edge
{
    char Element;
    int weight;
    pair<int,int> label;
    vector<Arcs> neighbours;
};



Edges = nodes, each arc = connection between nodes, arcs is defined as such

1
2
3
4
5
6
struct Arcs
{
   Edge *me;
   Edge *neighbour;

};


The graph is creates based from on an 2d char array, in which each element in this 2d array is an edge.

This is how i create the graph.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
struct find_neigbour {
    const std::pair<int,int> label;
    find_neigbour(const std::pair<int,int> label) : label(label) {}
    bool operator()(const Edge& j) const {
        return j.label == label;
    }
};

void createGraph()
{
  cout << "I was called, graph size:  " << graph.size() << endl;
//  Creating Each node
//  By looking at each entry of the 2d arrat
//--------------------------------------------------------------//
  for (int col = 0; col < getCol(); col++) {
    for (int row = 0; row < getRow(); row++) {
        if (!isspace(getChar(row,col)))
        {
            Edge edge;
            edge.Element = getChar(row,col);
            edge.label = make_pair(row,col);
            //cout << edge.Element << " at: " << edge.label.first << edge.label.second << endl;
            graph.push_back(edge);
            //cout << edge.Element << endl;
            //cout << graph.size() << endl;
        }
    }
  }
  cout << "I end with graph size: " << graph.size() << endl;
  cout << endl;

  //finding NeigbourNodes
  //Looking at each node neigbour. 
  //-------------------------------------------------------//
  for(auto it : graph)
  {
    double  beforeCount = 0;
    double  nextCount = 0;
    double  upCount  = 0;
    double  downCount = 0;

    if (it.label.first > 0 && it.label.first < getRow())
    {
      //cout << "doing something" << endl;
      auto before = std::find_if(graph.begin(), graph.end(),find_neigbour(make_pair(it.label.first-1,it.label.second)));
      auto next = std::find_if(graph.begin(), graph.end(),find_neigbour(make_pair(it.label.first+1,it.label.second)));
      if(before != graph.end() && !isspace(graph[before-graph.begin()].Element))
      {
           Arcs arc;
           arc.me = &it;
           arc.neighbour = &graph[before - graph.begin()];
           cout <<"before is: "<< arc.neighbour->Element << endl;
           cout << "at: " << "(" <<  arc.neighbour->label.first << "," <<arc.neighbour->label.second<< ")" << endl;
           cout << "now: " << "(" << arc.me->label.first << "," << arc.me->label.second << ")" << endl;
           beforeCount++;
           it.neighbours.push_back(arc);
      }
      if(next != graph.end()&& !isspace(graph[next-graph.begin()].Element))
      {
             Arcs arc;
             arc.me = &it;
             arc.neighbour = &graph[next - graph.begin()];
             cout <<"next is: "<< arc.neighbour->Element << endl;
             cout << "at: " << "(" <<  arc.neighbour->label.first << "," <<arc.neighbour->label.second<< ")" << endl;
             cout << "now: " << "(" << arc.me->label.first << "," << arc.me->label.second << ")" << endl;
             nextCount++;
             it.neighbours.push_back(arc);
             cout << "-next arc - added" << endl;
             cout <<"size: "<< it.neighbours.size() << endl;
      }
   }
    if (it.label.second >= 0 && it.label.second <= getCol() )
    {
      auto down = std::find_if(graph.begin(), graph.end(),find_neigbour(make_pair(it.label.first,it.label.second-1)));
      auto up = std::find_if(graph.begin(), graph.end(),find_neigbour(make_pair(it.label.first,it.label.second+1)));
      if(up != graph.end()&& !isspace(graph[up-graph.begin()].Element))
      {
             Arcs arc;
             arc.me = &it;
             arc.neighbour = &graph[up - graph.begin()];
             cout <<"up is: "<< arc.neighbour->Element << endl;
             cout << "at: " << "(" <<  arc.neighbour->label.first << "," <<arc.neighbour->label.second<< ")" << endl;
             cout << "now: " << "(" << arc.me->label.first << "," << arc.me->label.second << ")" << endl;
             upCount++;
             it.neighbours.push_back(arc);
             //cout << "up - arc - added" << endl;
      }
      if(down != graph.end() && !isspace(graph[down-graph.begin()].Element))
      {
               Arcs arc;
               arc.me = &it;
               arc.neighbour = &graph[down - graph.begin()];
               cout <<"down is: "<< arc.neighbour->Element << endl;
               cout << "at: " << "(" <<  arc.neighbour->label.first << "," <<arc.neighbour->label.second<< ")" << endl;
               cout << "now: " << "(" << arc.me->label.first << "," << arc.me->label.second << ")" << endl;
               downCount++;
               it.neighbours.push_back(arc);
               //cout << " down - arc - added" << endl;
      }
    }



      //down = 0;
      //up = 0;
      //before = 0;
      //next = 0;
      cout << "beforeCount: " << beforeCount << endl;
      cout << "nextCount: " << nextCount << endl;
      cout << "downCount: " << downCount << endl;
      cout << "upCount: " << upCount << endl;
      cout << "Size of neig: "<<it.neighbours.size() << endl;
      cout << endl;
      if(it.neighbours.size() == 0 )
      {
        cout << "______________________________________________Hey something is wrong here ___________________________________ " << endl;
        cout <<"("<<it.label.first << "," << it.label.second << ")" << endl;
      }

  }
  cout << "I know who is next to me " << endl;
  cout << graph.begin()->Element << graph.begin()->neighbours.size() << graph.begin()->label.first << graph.begin()->label.second << endl;  
}


It might seem a bit intimidating, but i am first looking at each entry of the 2d array, and if its not a space, I create node base in the char, and push it into the vector. when all the nodes has been created, I iterate through the vector of edges (=nodes) and check whether there is a edge which has a edge which is next , before, above or below it,

If one is found, an arc will be created between those two and pushed, to the vector named neighbors.

The problem is when I at the bottom check the size of the first node, it says that it is empty, even though it should have a size of two..

It seems like that the element doesn't get push_back properly.. if that makes sense...

Maybe is this incorrect
cout << graph.begin()->Element << graph.begin()->neighbours.size() << graph.begin()->label.first << graph.begin()->label.second << endl;
Last edited on
I found my error.

I was using range based for loop to manipulate the vector elements... which wasn't possible.. so I changed the loop and it works.
Topic archived. No new replies allowed.