向量中的push_问题 我在C++中创建了一个图形类,但是在插入元素时有一些问题。该图使用模拟链表的向量实现

向量中的push_问题 我在C++中创建了一个图形类,但是在插入元素时有一些问题。该图使用模拟链表的向量实现,c++,vector,push-back,C++,Vector,Push Back,这是我的main.cc: 和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; }

这是我的main.cc:

和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;
}
cout
总是给我0

提前谢谢你能试试这个吗

Vertex& Graph::operator [] (int pos){
  return this->graph[pos];
}
并添加另一个仅用于检索

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

这只是一个提醒,与问题无关,但对于所有get方法,最好使用
const

您所说的“元素从向量中删除”是什么意思?你的意思是调用析构函数?就在插入之后,向量的大小恢复为0。好像我没有插入任何东西似的是,边()的析构函数被调用。创建一个显示问题的最小示例。二,。在您的问题中输入这个最小示例的代码,而不是强迫人们离开现场。
Graph::operator[]
按值返回向量-也就是说,它返回临时值。正是在这种临时状态下,您才
向后推
。当然,临时文件很快就会被销毁。对
g[i]
的第二次调用生成一个新的、空的临时向量。
#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
#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];
}
#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
#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;
}
  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;
  }
Vertex& Graph::operator [] (int pos){
  return this->graph[pos];
}
const Vertex& Graph::operator [] (int pos)const{
  return this->graph[pos];
}