简体   繁体   中英

Problems with push_back in a vector

I'm creating a graph class in c++ but having some problems to insert elements into it. The graph is implemented using a vector<vector<Edge> > simulating a linked_list.

Here is my main.cc: http://pastebin.com/xkamm7Jq

#include <iostream>
#include <vector>
#include <set>
#include <string>
#include "edge.h"
#include "graph.h"
#include "vertex.h"

using namespace std;

int main (){

  int n, m;
  cin >> n >> m;

  Graph g (n);    
  for (int i=0; i<m; i++){
    int node, cost;
    cin >> node >> cost;    
    g[i].push_back (Edge (node, cost));
    cout << g[i].size() << endl;
  }

  cout << g.size() << endl;
  for (int i=0; i<g.size(); i++){
    cout << g[i].size() << endl;
  }    
  return 0;
}

Graph.h

#include <iostream>
#include <vector>
#include "vertex.h"

#ifndef _graph_h
#define _graph_h

class Graph {
 private:
  std::vector<Vertex> graph;

 public:
  Graph ();
  Graph (int size);
  Graph (int size, Vertex vertices);

  ~Graph();

  int size ();
  void resize (int size);
  void push_back (Vertex vertex);

  Vertex operator [] (int pos);


};

#endif

Here is graph.cc:

#include "graph.h"

Graph::Graph (){
  //
}

Graph::Graph (int size){
  this->graph.resize (size);
}

Graph::Graph (int size, Vertex vertices){
  this->graph.resize (size);
  for (int i=0; i<size; i++)
    this->graph[i] = vertices;
}

Graph::~Graph (){
  std::cout << "destruiu grafo\n";
}

int Graph::size (){
  return this->graph.size();
}

void Graph::resize (int size){
  this->graph.resize (size);
}

void Graph::push_back (Vertex vertex){
  this->graph.push_back (vertex);
}

Vertex Graph::operator [] (int pos){
  return this->graph[pos];
}

Vertex.h:

#include <vector>
#include "edge.h"

#ifndef _vertex_h
#define _vertex_h

class Vertex {
 private:
  int node;
  std::vector<Edge> edges;

 public:
  Vertex ();
  Vertex (int node);
  Vertex (int node, std::vector<Edge> edges);
  Vertex (const Vertex& other);
  ~Vertex ();

  int getNode ();
  void push_back (Edge edge);
  std::vector<Edge> getEdges ();

  int size();

  Vertex operator = (const Vertex& other);

  Edge operator [] (int pos);


};

#endif

Vertex.cc

#include "vertex.h"
#include <iostream>

Vertex::Vertex (){

}

Vertex::Vertex (int node){
  this->node = node;
}

Vertex::Vertex (int node, std::vector<Edge> edges){
  this->node = node;
  this->edges = edges;
}

Vertex::Vertex (const Vertex& other){
  this->node = other.node;
  this->edges = other.edges;
}

Vertex::~Vertex (){

}

Vertex Vertex::operator = (const Vertex& other){
  this->node = other.node;
  this->edges = other.edges;
  return *this;
}

int Vertex::getNode (){
  return this->node;
}

std::vector<Edge> Vertex::getEdges (){
  return this->edges;
}

void Vertex::push_back (Edge edge){
  this->edges.push_back (edge);
}

int Vertex::size (){
  return this->edges.size();
}

Edge Vertex::operator [] (int pos){
  return this->edges[pos];
}

Edge.h:

#ifndef _edge_h
#define _edge_h

class Edge {
 private:
  int node;
  int cost;    
 public:
  Edge (int node, int cost);
  Edge (const Edge& other);
  ~Edge ();

  Edge operator = (const Edge& other);
  void setNode (int node);
  int getNode ();
  int getCost ();
};

#endif

and Edge.cc:

#include <iostream>
#include "edge.h"

Edge::Edge (int node, int cost){
  this->node = node;
  this->cost = cost;
}

Edge::Edge (const Edge& other){
  this->node = other.node;
  this->cost = other.cost;
}

Edge::~Edge (){
  std::cout << "edge deleted\n";
}

Edge Edge::operator = (const Edge& other){
  this->node = other.node;
  this->cost = other.cost;    
  return *this;
}

int Edge::getNode (){
  return this->node;
}

int Edge::getCost (){
  return this->cost;
}

void Edge::setNode (int node){
  this->node = node;
}

My problem is related to the g.push_back() . Just after the push, the element (in this case, Edge ) is deleted from the vector<Edge> .

I know that a vector copies the element during the insertion but apparently, he is holding a reference to the object declared on main ().

Here is an example:

  for (int i=0; i<m; i++){
    int node, cost;
    cin >> node >> cost;

    g[i].push_back (Edge (node, cost));
    cout << g[i].size() << endl;
  }

The cout will always give me 0.

Thanks in advance

Can you try this?

Vertex& Graph::operator [] (int pos){
  return this->graph[pos];
}

And add another just for retrieval

const Vertex& Graph::operator [] (int pos)const{
  return this->graph[pos];
}

And just a reminder, not related to the question, but it is good to make use of const for all get methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM