gpt4 book ai didi

c++ - 在 C++ 中引用 vector

转载 作者:行者123 更新时间:2023-11-30 05:25:14 26 4
gpt4 key购买 nike

首先,请知道我已经对我的问题的解决方案进行了广泛的研究,但似乎没有一个解决方案有效。我正在尝试创建一个图表。长话短说,我似乎无法向我的任何节点添加边,因为这样做的功能:

void pushEdge(node * dest, int weight) {
edges.push_back(edge(this,dest,weight));
}

不引用实际的“边缘” vector 。当我打印出边缘时,没有打印出任何东西,因为尽管我之前添加了它们,但每个节点显然都没有边缘。

如何在类函数中引用类 vector ?

代码:(如果你想运行这个,你会看到有 0 个边。

graph.h - 存储边、节点和图类。

#pragma once
#include <vector>
#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

class node;

class edge {
public:
edge(){}
edge(node * origin, node * dest, int weight) {
this->origin = origin;
this->dest = dest;
this->weight = weight;
}
node * origin;
node * dest;
int weight;

};

class node {
public:
node(){}
node(int x, int y, int t) {
this->x = x;
this->y = y;
this->t = t;
}

vector<edge> edges;
int x; int t; int y;

void pushEdge(node * dest, int weight) {
edges.push_back(edge(this,dest,weight));
}

void printEdges() {
cout << "NODE (" << this->x << "," << this->y << "): " << edges.size() << " edges!" << endl;
for (int i = 0; i < edges.size(); i++) {
edge at = edges.at(i);
cout << "EDGE: Origin(" << at.origin->x << "," << at.origin->y << ") Target(" << at.dest->x << "," << at.dest->y << ") Weight: " << at.weight << endl;
}
}
};

class graph {
public:
graph(){}
node nodes[900] = {};

void printNodes() {
cout << "Node count: " << sizeof(this->nodes) << endl;
for (int i = 0; i < 900; i++) {
this->nodes[i].printEdges();
}
}
};

source.cpp - 生成图表并将其打印出来。

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

using namespace std;

graph genGraph();

int main() {
graph myGraph = genGraph();
myGraph.printNodes();
}

graph genGraph() {
graph myGraph;
int i = 0;
for (int x = 1; x <= 30; x++) {
for (int y = 1; y <= 30; y++) {
node myNode(x, y, 0);
myGraph.nodes[i] = myNode;
i += 1;
}
}
for (int i = 0; i < 900; i++) {
node myNode = myGraph.nodes[i];
int x = myNode.x; int y = myNode.y;
if (x > 1) {
node nodeLeft = myGraph.nodes[i-30];
myNode.pushEdge(&nodeLeft, 2);
if (y > 1) {
node nodeTopLeft = myGraph.nodes[i-31];
myNode.pushEdge(&nodeTopLeft, 2);
}
if (y < 30) {
node nodeBottomLeft = myGraph.nodes[i-29];
myNode.pushEdge(&nodeBottomLeft, 2);
cout << myNode.edges.size() << endl;
}
}
if (x < 30) {
node nodeRight = myGraph.nodes[i+30];
myNode.pushEdge(&nodeRight, 2);
if (y > 1) {
node nodeTopRight = myGraph.nodes[i+29];
myNode.pushEdge(&nodeTopRight, 2);
}
if (y < 30) {
node nodeBottomRight = myGraph.nodes[i + 31];
myNode.pushEdge(&nodeBottomRight, 2);
}
}
if (y > 1) {
node nodeTop = myGraph.nodes[i - 1];
myNode.pushEdge(&nodeTop, 2);
}
if (y < 30) {
node nodeBottom = myGraph.nodes[i + 1];
myNode.pushEdge(&nodeBottom, 2);
}
}
return myGraph;
}

谢谢,最大K

最佳答案

看来问题出在

node myNode = myGraph.nodes[i];

当你在那里创建节点 i 的拷贝时。您可以创建一个引用:

node& myNode = myGraph.nodes[i];

关于c++ - 在 C++ 中引用 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38381781/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com