vector iterators incompatible with const vector&

前端 未结 1 414
半阙折子戏
半阙折子戏 2021-01-22 15:21

I\'m writing program for graphs. In this program I have a method which has to return vertices inside the weak component originating at vertex. I am getting: Error \"vector itera

相关标签:
1条回答
  • 2021-01-22 16:02

    Since you are taking g as a const graph&, this means g.gr is treated as const inside your function. begin on a const vector<T> returns a const_iterator. (also you used == instead of = for assignment)

    for (std::vector<int>::const_iterator j = g.gr[hodn].begin(); ...)
    

    But with C++11 or newer you may as well use auto to avoid this

    for (auto j = g.gr[hodn].begin(); ...)
    

    or a range-based for:

    for (auto&& e : g.gr) {
        if (!used[e]) {
            s.push(e);
            ret.push_back(e);
        }
    }
    
    0 讨论(0)
提交回复
热议问题