【问题标题】:Cannot insert element in std::set of iterator to std::list无法将迭代器的 std::set 中的元素插入到 std::list
【发布时间】:2018-02-13 23:32:47
【问题描述】:

在显示给我编译器错误的代码之前,我想简要解释一下为什么我需要一组迭代器到一个列表,只是为了避免像“你真的需要那个吗?”这样的 cmets。或者也许将它们更改为 cmets “您的代码可以通过这种方式解决......但是我应该像这样处理原始问题”。您可以跳过阅读并直接转到最后一部分(“未编译的代码”)。

背后的原因

我构建了一个有向二分加权图。每个弧都存储在一个结构中

typedef struct edge_ {
    int src;
    int des;
    double w; //weight of the arc
}edge;

我使用此类结构的列表存储有关图形的信息。

list<edge> all_edges;

之后,我按重量对弧进行排序,然后循环循环,从最轻到最重。 因为我想在循环中从all_edges 中删除一些元素,所以在每个循环步骤的开头我调用

list<edge>::iterator smallest = all_edges.begin();

在该循环的每个步骤中,在对弧的权重做了一些事情之后,我想从图中删除所有从 src 出发或以 des 结束的节点。为此,在构建图的过程中,我创建了两个向量,一个用于二分图的每个分量,由它们的元素索引,并且在每个向量的每个位置,我将所有迭代器的列表存储到边缘all_edges 离开与所考虑的向量位置相对应的节点。代码如下(“小”和“大”是我识别二部图两个分量的方式)

vector<list<list<edge>::iterator>> edges_from_small(small.size());
vector<list<list<edge>::iterator>> edges_from_big(big.size());

这是我用来填充上述向量的代码

//inside a loop...
    edge e;
    e.src = ...
    e.des = ...
    e.w = ...
    all_edges.push_back(e);                
    edges_from_small[e.src].push_back(--(all_edges.end()));
    edges_from_big[e.des].push_back(--(all_edges.end()));

假设我想删除边缘e。 我很想循环 edges_from_small[e.src] 的所有元素并让它们中的每一个都调用 all_edges.erase(iterator),但是这样做,因为可以在 edges_from_smalledges_from_big 中列出一个边,我将结束尝试删除一个使用取消引用的迭代器的元素!

设置将是一个解决方案!我只需要创建一组list&lt;edge&gt;::iterator 并用edges_from_small[e.src]edges_from_big[e.des] 的元素填充它,然后,由于重复项被删除,从列表all_edges 中删除所有元素。

但我的代码无法编译,它给了我一个我无法理解的错误,出现在以下行之一:

set<list<edge>::iterator> to_remove;

for (auto it = edges_from_small[smallest->src].begin(); it != edges_from_small[smallest->src].end(); ++it) {
        to_remove.insert(*it); //error here! 
    }
for (auto it = edges_from_big[smallest->des].begin(); it != edges_from_big[smallest->des].end(); ++it) {
        //to_remove.insert(*it); //error here!
    }
for (auto it = to_remove.begin(); it != to_remove.end(); ++it) {
        all_edges.erase(*it);
    } 

编译器给了我一个相当大的输出(都提到了上面的那一行),目前我只放了第一行和最后一行,我认为这是最具指示性的。

g++ -ggdb3 -g -O0 -std=c++14 -Wall -Werror -o compare main.cpp peaks.cpp common.cpp compare.cpp parameters.cpp 
In file included from compare.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:38:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:216:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__locale:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:439:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:628:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:606:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:344:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__functional_base:63:21: error: 
      invalid operands to binary expression ('const std::__1::__list_iterator<_edge,
      void *>' and 'const std::__1::__list_iterator<_edge, void *>')
        {return __x < __y;}
                ~~~ ^ ~~~
....................MUCH MORE OUTPUT....................
compare.cpp:226:23: note: in instantiation of member function
      'std::__1::set<std::__1::__list_iterator<_edge, void *>,
      std::__1::less<std::__1::__list_iterator<_edge, void *> >,
      std::__1::allocator<std::__1::__list_iterator<_edge, void *> > >::insert'
      requested here
            to_remove.insert(*it);
                      ^
1 error generated.
make: *** [compare] Error 1

Compilation exited abnormally with code 2 at Tue Sep  5 17:34:39

你知道那条线有什么问题吗?

未编译的代码

typedef struct _edge {
    int src;
    int des;
    double w; //weight of the arch
}edge;

list<edge> all_edges;
vector<list<const list<edge>::iterator>> edges_from_small(small.size());
vector<list<const list<edge>::iterator>> edges_from_big(big.size());

//graph construction    
//for loop ...
    edge e;
    e.src = ...
    e.des = ...
    e.w = ... 
    all_edges.push_back(e);
    edges_from_small[e.src].push_back(--(all_edges.end())); 
    edges_from_big[e.des].push_back(--(all_edges.end()));
//end of loop

list<edge>::iterator smallest = all_edges.begin();
set<list<edge>::iterator> to_remove;
for (auto it = edges_from_small[smallest->src].begin(); 
     it != edges_from_small[smallest->src].end(); ++it) {
    to_remove.insert(*it); //<--- COMPILER ERROR HERE, you can see the error description in the last lines of the previous paragraph
}    

【问题讨论】:

  • A std::set 要求元素是有序的,默认使用&lt;。您将如何在这些迭代器上定义一个有意义的顺序?如果您有一个好的答案,请创建该函数并将其作为第二个模板参数提供给 set
  • @BoBTFish 呃!可以给it数字it-&gt;src*size_of_big + it-&gt;des关联一个合理的顺序

标签: c++ iterator set


【解决方案1】:

您面临的问题是std::set requires elements to implement operator<(a,b)(或者,实际上,您未指定比较器的声明使用std::less)。现在,std::list's iteratorBidirectionalIterator,缺少上述操作员。

【讨论】:

    【解决方案2】:

    std::list::iterators 无法排序,因为没有函数或运算符来比较它们(wrt less/greater)。

    改为使用std::unordered_set,这不需要对元素进行排序,它使用元素的哈希将它们放入桶中。您可能必须提供一个哈希函数,只需在 namespace std 中放置 std::hash 的重载,以便 std::unordered_set 找到并使用它。
    另请注意,std::unordered_set 的平均插入时间复杂度为常数时间复杂度,而对数时间复杂度为 std::set

    【讨论】:

      【解决方案3】:

      关于您的问题的原因,现有答案是正确的,但您可能需要考虑 boost::bimap 并查看它是否适合您的问题(如果没有看到您编写的整个代码,我很难理解您正在执行的算法)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-10
        • 2021-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-22
        • 2011-02-21
        相关资源
        最近更新 更多