c++ - 遍历 vector c++ 中的列表

标签 c++ list vector stl

我有这个代码:

#include <iostream>
#include <vector>
#include <list>

using namespace std;

class Graph {
    public:
        //Create graph with n nodes
        Graph(int size);
             ~Graph();

        //Initialize graph
        void InitializeGraphWithRandomNum();


    private:

        vector <list <int*> > *graph;

};


void Graph::InitializeGraphWithRandomNum() {
    //Here I want to iterate
    for (int i=0; i< graph->size(); i++) {
        std::list <int*>::iterator it;
        for (it = graph[i].begin(); it< graph[i].end();++it) {
                 ..........
            }

    }
}

这里有问题。它说

No match for ‘operator=’ in ‘it = (((Graph*)this)->Graph::graph + ((unsigned int)(((unsigned int)i) * 12u)))->std::vector<_Tp, _Alloc>::begin with _Tp = std::list, _Alloc = std::allocator >, std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator*, std::vector > >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer = std::list*’ DejkstraAlg.cpp

谢谢 最好的

最佳答案

graph 是指向 vector 的指针,而不是 vector 。要么将其声明为 vector ,要么使用 (*graph)[i].begin()

关于c++ - 遍历 vector c++ 中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19655853/

相关文章:

c++ - std::optional:不参与重载决议与被定义为已删除

C++:将自身结构的地址作为参数传递给 pthread_create 时出错:什么覆盖了我的数据?

python - 列表上的动态嵌套循环

以不确定长度作为参数的 R 函数列表

来自 csv 文件的 C++ 2D vector

c++ - 如何在 C++ 中访问成员函数内的对象数组?

c++ - 是否可以像在其他编译器中的 Microsoft C 中那样使用类型化名称或类型名称来声明构造函数?

c# - 我需要使用 Linq 比较列表和数据表并过滤值

c++ - 在 C++ Primer 中使用 Struct 和 Vector 时出现错误 C2661

android - 如何让 QVideoProbe 工作?