[英]Adjacency list representation of a directed graph using c++ vector
I'm a newcomer. 我是新来者。 I'm facing a problem with c++ vector and its iterator. 我正遇到c ++向量及其迭代器的问题。 I have tried to represent a adjacency list of a directed graph but failed. 我试图代表一个有向图的邻接表,但是失败了。 Here is my code: ` 这是我的代码:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int Node,Edge,node1,node2,cost;
vector<int>nodes[100],costs[100];
vector<int>::iterator it;
cout<<"Enter numbers of nodes: "<<endl;
cin>>Node;
cout<<"Enter numbers of edges: "<<endl;
cin>>Edge;
for(int i=0;i<Edge;i++){
cout<<i+1<<"th edge's Node1: ";
cin>>node1;
cout<<i+1<<"th edge's Node2: ";
cin>>node2;
cout<<"Cost from "<<node1<<" to"<<node2<<": ";
cin>>cost;
cout<<endl;
nodes[node1].push_back(node2);
costs[node1].push_back(cost);
}
for(it=nodes.begin();it<nodes.end();it++){
for(int i=0;i<nodes[it].size();i++){
cout<<nodes[it][i]<<" ";
}
cout<<endl;
}
return 0;
}
` `
You should have told us about the compilation error that you've encountered. 您应该已经告诉我们您遇到的编译错误。
Trying to compile your code immediately shows an inconsistency in the loop: 尝试立即编译代码会在循环中显示不一致:
for(it=nodes.begin();it<nodes.end();it++)
and on the following uses of it
. 并就下列用途it
。 In fact you use it
in indexes, as if it were an int
. 实际上,您在索引中使用it
,就好像它是一个int
。 But you've declared it as an iterator: 但是您已将其声明为迭代器:
vector<int>::iterator it; // you say it is an iterator that iterates through integers in ONE vector
Inexed access and iterators are two different concepts. Inexed访问和迭代器是两个不同的概念。
Solution 1: use indexes 解决方案1:使用索引
Just use an index of type int
(or better size_t
): 只需使用int
类型的索引(或更好的size_t
):
...
const size_t maxnodes=100;
vector<int>nodes[maxnodes],costs[maxnodes];
// + remove the definition of it
...
for(size_t it=0;it<maxnodes;it++) {
...
Solution 2: use iterators, but correctly 解决方案2:正确使用迭代器
Let the compiler define the right type for the iterator, and then dereference the iterator (ie handle it as if it were a pointer): 让编译器为迭代器定义正确的类型,然后取消引用迭代器(即像处理指针一样对其进行处理):
// remove the definition of the it at the begin of main
... // rest of code unchanged except for the loop
for(auto it=begin(nodes);it!=end(nodes);it++){ // attention at the condition
for(int i=0;i<it->size();i++){
cout<<(*it)[i]<<" ";
}
cout<<endl;
}
Solution 3: use the confortable range for 解决方案3:将舒适范围用于
Forget about the iterators and let the compiler handle the job for you: 忘记迭代器,让编译器为您处理作业:
for(auto& n : nodes) { // note the &, to get the original element and not a clone of it
for(int i=0;i<n.size();i++){
cout<<n[i]<<" ";
}
cout <<endl;
}
Here another live demo . 这是另一个现场演示 。
You will immediately realize that you could as well use the range for to handle the inner loop: 您将立即意识到,您也可以使用range来处理内部循环:
for(int& target : n) { // when reading the code outloud, say " in " instead of ":"
cout<<target<<" ";
}
Final remarks 结束语
You should always verify the user's data input, especially to ensure that it's in the valid range if you are using it as index. 您应该始终验证用户的数据输入,尤其是在将其用作索引时,请确保其在有效范围内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.