问题描述
我对对象编程非常陌生。我正在尝试为每个顶点输入边缘,但似乎没有任何效果。
我想在控制台上一行写两个数字。例如,如果n = 4且m = 3,我可以在控制台中写类似的内容:
1 1
2 4
3 4
#include <iostream>
#include <vector>
using namespace std;
class Vertices
{
public:
int color;
vector<Vertices*> neighbours;
};
int main()
{
int m,n;
cin >> m; // edges
cin >> n; // vertices
cout << endl;
Vertices* vertices = new Vertices[n];
for (int i = 0; i < m; i++)
{
// cin edges to each vertex
}
return 0;
}
解决方法
首先需要从cin
中读取两个数字:
int src,dst;
cin >> src >> dst;
然后更新相应对象的邻居:
auto srcP = &vertices[src-1];
auto dstP = &vertices[dst-1];
srcP->neighbours.push_back(dstP);
if (src != dst) dstP->neighbours.push_back(srcP);