向量中的push_back问题

时间:2014-10-14 03:45:47

标签: c++ vector push-back

我正在用c ++创建一个图表类,但是在插入元素时遇到了一些问题。该图是使用模拟linked_list的vector<vector<Edge> >实现的。

这是我的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

这是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

和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;
}

我的问题与g.push_back()有关。在推送之后,元素(在本例中为Edge)将从vector<Edge>中删除。

我知道vector在插入过程中复制了元素,但显然,他持有对main()上声明的对象的引用。

以下是一个例子:

  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总是会给我0.

提前致谢

1 个答案:

答案 0 :(得分:1)

你能试试吗?

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

添加另一个仅用于检索

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

只是提醒一下,与问题无关,但对所有获取方法都使用const是好的。