简体   繁体   English

使用C ++向量的有向图的邻接表表示

[英]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;
}

Here a live demo 这是现场演示

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.

相关问题 在C ++中为有向图创建邻接列表 - Making an adjacency list in C++ for a directed graph 在 C++ 中将邻接列表转换为有向无环图的邻接矩阵 - Converting an Adjacency List to a an Adjacency Matrix for a Directed Acyclic Graph in C++ 在有向图中有效表示邻接表和权重 - Efficient representation for adjacency list and weights in directed graph 将边添加到使用C ++中的链接列表实现的有向图(邻接列表)中 - Add edge to a directed graph (Adjacency list) implemented using Linked List in C++ "在 C++ 中使用向量的向量(2D 向量)表示邻接表" - Adjacency list representation using vector of vectors(2D vector) in C++ C ++指针,迭代器,图的邻接表表示形式的问题 - Problems with C++ Pointers, Iterator, Adjacency List Representation of Graph 在C ++中使用STL通过邻接表进行图形表示 - graph representation through adjacency lists using STL in c++ 考虑到遍历的路径每次都是相同的,如何在C ++中实现有向图而不使用邻接表? - How do I implement a directed graph in C++ without using an adjacency list, given that the path of traversal will be the same every time? 图的 C++ 邻接表表示 - C++ Adjacency List Representation of Graphs 如何在C ++中使用向量对向量创建邻接表? - How to make an Adjacency List using a vector of vector pair in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM