gpt4 book ai didi

c++ - 无法在 std::set 上使用 set_intersection

转载 作者:太空狗 更新时间:2023-10-29 19:58:12 25 4
gpt4 key购买 nike

我正在尝试使用下面提到的 set_intersection

std::set<std::pair<char*, int>,FileLinePairComapare > netSet;
std::set<std::pair<char*, int>,FileLinePairComapare > portSet;

std::set<std::pair<char*, int>,FileLinePairComapare> result;
std::set<std::pair<char*, int>,FileLinePairComapare>::iterator it;
std::set_intersection(netSet.begin(),netSet.end(),portSet.begin(),portSet.end(),result.begin());

我在最后一行遇到编译错误

In instantiation of ‘_OIter std::set_intersection(_IIter1, _IIter1, _IIter2, _IIter2, _OIter) [with _IIter1 = std::_Rb_tree_const_iterator >; _IIter2 = std::_Rb_tree_const_iterator >; _OIter = std::_Rb_tree_const_iterator >]’:

passing ‘const std::pair’ as ‘this’ argument of ‘std::pair& std::pair::operator=(const std::pair&)’ discards qualifiers [-fpermissive]

在我使用这些集合和 set_intersection 的地方没有 cons 函数。

最佳答案

您不能使用算法直接写入 std::set 迭代器。 所有 set 迭代器都是常量,这是有充分理由的,因为更改任何值都会破坏树(这同样适用于 std::map 键 - map 迭代器只能修改映射值)。

即使你可以它也不会工作,因为容器是空的(例如,如果你试图使用 std::vector 作为目标容器,你最终会得到未定义的行为) .

使用std::inserter

std::set_intersection(
netSet.begin(), netSet.end(),
portSet.begin(), portSet.end(),
std::inserter(result, result.end())
);

关于c++ - 无法在 std::set 上使用 set_intersection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24774583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com