如何更改另一个 'temporary' 容器中的容器元素

How to change a container elements in another 'temporary' container

我有一个临时列表,需要更改其元素,erase() 将它们从列表中标记为已完成。

std::list<Vertex> temp {vertices.begin(), vertices.end()};

我试过使用 Vertex&Vertex* 但没有成功,第二个使代码非常混乱。

正确的做法是什么?提前致谢

编辑:周边代码

    int label = 1;
    // change elements without affecting temporary list
    std::list<Vertex*> unlabeledVertices {graph.vertices.begin(), graph.vertices.end()};

    for (auto iter = unlabeledVertices.begin(); iter != unlabeledVertices.end(); ++label) {
        (*iter)->label = label;

        // temporary list
        std::list<Vertex*> currentLabelVertices;
        currentLabelVertices.push_back(*iter);

        // check if we can label any other vertex with the current color
        auto nestedIter = iter;
        for (nestedIter++; nestedIter != unlabeledVertices.end(); ) {
            // checking current vertex against any other colored vertex
            if (std::none_of(currentLabelVertices.begin(), currentLabelVertices.end(),
                [=](const Vertex* v){ return (*nestedIter)->isConnected(*v); })) {
                (*nestedIter)->label = label;
                currentLabelVertices.push_back(*nestedIter);

                nestedIter = unlabeledVertices.erase(nestedIter);
            }
            else {
                nestedIter++;
            }
        }

        iter = unlabeledVertices.erase(iter);
    }

错误信息:

/usr/bin/g++ -fdiagnostics-color=always -g /home/etzl/projects/c-cpp/test/*.cc -o exec
In file included from /usr/include/x86_64-linux-gnu/c++/10/bits/c++allocator.h:33,
                 from /usr/include/c++/10/bits/allocator.h:46,
                 from /usr/include/c++/10/string:41,
                 from /usr/include/c++/10/bits/locale_classes.h:40,
                 from /usr/include/c++/10/bits/ios_base.h:41,
                 from /usr/include/c++/10/ios:42,
                 from /usr/include/c++/10/ostream:38,
                 from /usr/include/c++/10/iostream:39,
                 from /home/etzl/projects/c-cpp/test/main.cc:1:
/usr/include/c++/10/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = Vertex*; _Args = {Vertex&}; _Tp = std::_List_node<Vertex*>]’:
/usr/include/c++/10/bits/alloc_traits.h:512:17:   required from ‘static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = Vertex*; _Args = {Vertex&}; _Tp = std::_List_node<Vertex*>; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::_List_node<Vertex*> >]’
/usr/include/c++/10/bits/stl_list.h:637:33:   required from ‘std::__cxx11::list<_Tp, _Alloc>::_Node* std::__cxx11::list<_Tp, _Alloc>::_M_create_node(_Args&& ...) [with _Args = {Vertex&}; _Tp = Vertex*; _Alloc = std::allocator<Vertex*>; std::__cxx11::list<_Tp, _Alloc>::_Node = std::__cxx11::list<Vertex*>::_Node]’
/usr/include/c++/10/bits/stl_list.h:1911:32:   required from ‘void std::__cxx11::list<_Tp, _Alloc>::_M_insert(std::__cxx11::list<_Tp, _Alloc>::iterator, _Args&& ...) [with _Args = {Vertex&}; _Tp = Vertex*; _Alloc = std::allocator<Vertex*>; std::__cxx11::list<_Tp, _Alloc>::iterator = std::__cxx11::list<Vertex*>::iterator]’
/usr/include/c++/10/bits/stl_list.h:1227:19:   required from ‘void std::__cxx11::list<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {Vertex&}; _Tp = Vertex*; _Alloc = std::allocator<Vertex*>]’
/usr/include/c++/10/bits/stl_list.h:1840:18:   required from ‘void std::__cxx11::list<_Tp, _Alloc>::_M_initialize_dispatch(_InputIterator, _InputIterator, std::__false_type) [with _InputIterator = std::_List_iterator<Vertex>; _Tp = Vertex*; _Alloc = std::allocator<Vertex*>]’
/usr/include/c++/10/bits/stl_list.h:806:26:   required from ‘std::__cxx11::list<_Tp, _Alloc>::list(_InputIterator, _InputIterator, const allocator_type&) [with _InputIterator = std::_List_iterator<Vertex>; <template-parameter-2-2> = void; _Tp = Vertex*; _Alloc = std::allocator<Vertex*>; std::__cxx11::list<_Tp, _Alloc>::allocator_type = std::allocator<Vertex*>]’
/home/etzl/projects/c-cpp/test/main.cc:58:87:   required from here
/usr/include/c++/10/ext/new_allocator.h:150:4: error: cannot convert ‘Vertex’ to ‘Vertex*’ in initialization
  150 |  { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Build finished with error(s)

您应该使用 std::unordered_set<Vertex*> currentLabelVertices 而不是列表。它提供 O(1) 查找而不是 O(n) 解决方案 std::none_of().

您的其他问题在这里:

std::list<Vertex*> unlabeledVertices {graph.vertices.begin(), graph.vertices.end()};

它无法编译,因为 graph.vertices 包含 Vertex 个实例,而不是指针。修复很简单:

std::list<Vertex*> unlabeledVertices;
for (auto& vertex : graph.vertices)
    unlabeledVertices.push_back(&vertex);