C++ 相当于 Python difference_update?

标签 c++ python stdset

s1 和 s2 是集合(Python 集合或 C++ std::set)
要将 s2 的元素添加到 s1(set union),你可以这样做

Python: s1.update(s2)

C++: s1.insert(s2.begin(), s2.end());

要从s1中移除s2的元素(设置差异),你可以这样做

Python: s1.difference_update(s2)

这在 C++ 中的等价物是什么?代码

s1.erase(s2.begin(), s2.end());

不起作用,因为 s1.erase() 需要来自 s1 的迭代器。代码

std::set<T> s3;
std::set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), std::inserter(s3, s3.end());
s1.swap(s3);

有效,但似乎过于复杂,至少与 Python 相比是这样。

有没有更简单的方法?

最佳答案

使用 std::set_difference是在 C++ 中执行此操作的惯用方法。您偶然发现了 C++/STL 与许多其他语言之间的主要区别之一(双关语)。 STL 不直接将操作与数据结构捆绑在一起。这就是为什么 std::set不执行差异例程。

基本上,算法如std::set_difference将操作的结果写入另一个对象。有趣的是,算法 并不要求其中一个或两个操作数实际上是 std::set。 .算法定义为:

Effects: Copies the elements of the range [first1, last1) which are not present in the range [first2, last2) to the range beginning at result. The elements in the constructed range are sorted.

Requires: The resulting range shall not overlap with either of the original ranges. Input ranges are required to be order by the same operator<.

Returns: The end of the constructed range.

Complexity: At most 2 * ((last1 - first1) + (last2 - first2)) - 1 comparisons

有趣的区别是 C++ 版本适用于任何两个排序范围。在大多数语言中,您必须在访问集合差分 算法之前强制将调用对象(左侧操作数)转换或转换为集合。

这与您的问题并不相关,但这就是将各种 set 算法建模为独立算法而不是成员方法的原因。

关于C++ 相当于 Python difference_update?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6087388/

相关文章:

c++ - 从文件中读取数据

c++ - 如何命名我的新项目的命名空间?

C++ 信号量 (semi *lockfree*),我在哪里可以得到一个?

Python 请求:检查 URL 是否不是 HTML 网页

python - 如何忽略 Python 中额外的关键字参数?

c++ - std::set 与不一致的运算符<

c++ - 使用提示将排序范围插入 std::set

c++ - i686-elf-gcc CreateProcess 错误(没有那个文件或目录)

python - 在 Python Shell 中打印当前行

c++ - 从构造函数参数推断 std::set 的 Compare 类型