为什么在对集合执行 set_union 时需要调用插入器函数?

发布于 2024-08-06 17:02:39 字数 282 浏览 1 评论 0原文

我需要像这样调用STL的set_union函数:

set<int> a1, a2;

set_union(a1.begin(), a1.end(), a2.begin(), a2.end(), inserter(a1, a1.begin());

而不是

set_union(a1.begin(), a1.end(), a2.begin(), a2.end(), a1.begin());

为什么会这样?

i need to make the call to the set_union function of STL like this:

set<int> a1, a2;

set_union(a1.begin(), a1.end(), a2.begin(), a2.end(), inserter(a1, a1.begin());

and not

set_union(a1.begin(), a1.end(), a2.begin(), a2.end(), a1.begin());

why is that so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

☆獨立☆ 2024-08-13 17:02:39

a1.begin() 根本不是一个输出迭代器。 inserter(a1,a1.begin()) 返回一个输出迭代器,它将为每个元素调用集合的插入函数。但我什至不确定第一个版本是否正确。您迭代插入新元素的同一个集合。 (!)

因为你处理的是 set<>对象已经有了,为什么不直接写呢

a1.insert(a2.begin(),a2.end());

a1.begin() is simply not an output iterator. inserter(a1,a1.begin()) returns an output iterator which will invoke the set's insert function for each element. But I'm not even sure whether the first version is even correct. You iterate over the same set you insert new elements into. (!)

Since you deal with set<> objects already, why don't you simply write

a1.insert(a2.begin(),a2.end());

?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文