如何获取两个 std::set元素之间的差异?

发布于 2024-12-01 19:57:26 字数 362 浏览 1 评论 0原文

所以我们有 set; aset; b 我们想要得到 std::set; c 将包含表示 a - b 的项目(这意味着如果我们从中删除 b 中的所有项目,则 a 中剩下的内容code>,如果 b 包含多个 aa 中不存在的项目,我们希望让它们保持类似这样简单的数字数学:5-6 = 03-2 = 1)

So we have set<string> a and set<string> b and we want to get std::set<string> c which would contain items that would represent a - b (meaning what is left from a if we remove from it all items from b, if b contains more than a or items not present in a we want to keep them alike such simple math with numbers: 5-6 = 0 while 3-2 = 1)

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

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

发布评论

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

评论(3

岁月打碎记忆 2024-12-08 19:57:26

我认为您需要 中的 std::set_difference()

#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <iterator>

using namespace std;

set<string> a;
set<string> b;
set<string> result;


int main()
{
    a.insert("one");
    a.insert("two");
    a.insert("three");

    b.insert("a");
    b.insert("b");
    b.insert("three");

    set_difference( a.begin(), a.end(), b.begin(), b.end(), inserter(result, result.begin()));

    cout << "Difference" << endl << "-------------" << endl;

    for (set<string>::const_iterator i = result.begin(); i != result.end(); ++i) {
        cout << *i << endl;
    }

    result.clear();
    set_symmetric_difference(a.begin(), a.end(), b.begin(), b.end(), inserter(result, result.begin()));

    cout << "Symmetric Difference" << endl << "-------------" << endl;

    for (set<string>::const_iterator i = result.begin(); i != result.end(); ++i) {
        cout << *i << endl;
    }

    return 0;
}

I think you want std::set_difference() from <algorithm>.

#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <iterator>

using namespace std;

set<string> a;
set<string> b;
set<string> result;


int main()
{
    a.insert("one");
    a.insert("two");
    a.insert("three");

    b.insert("a");
    b.insert("b");
    b.insert("three");

    set_difference( a.begin(), a.end(), b.begin(), b.end(), inserter(result, result.begin()));

    cout << "Difference" << endl << "-------------" << endl;

    for (set<string>::const_iterator i = result.begin(); i != result.end(); ++i) {
        cout << *i << endl;
    }

    result.clear();
    set_symmetric_difference(a.begin(), a.end(), b.begin(), b.end(), inserter(result, result.begin()));

    cout << "Symmetric Difference" << endl << "-------------" << endl;

    for (set<string>::const_iterator i = result.begin(); i != result.end(); ++i) {
        cout << *i << endl;
    }

    return 0;
}
极度宠爱 2024-12-08 19:57:26

假设您的意思是集合的差异:

set_difference

如果您的意思是元素之间的比较,实际上不可能以一般或简单的方式回答这个问题。答案将非常具体地针对的问题,但没有具体说明或明确。

Assuming you mean the difference of the sets:

set_difference

If you mean comparison between the elements, it is not really possible to answer it in a general or simple way. The answer would be pretty specific to your problem, which isn't specified or clear.

离旧人 2024-12-08 19:57:26

我想这应该可行。

for( set<string> :: iterator it = a.begin(); it != a.end(); ++it )
{
     set<string>:: iterator iter = find( b.begin(), b.end(), *it );
     if( iter == b.end() )
     {        // ^^^^^^^   Note: find returns b.end() if it does not find anything.
        c.insert(*iter)
     }
}

This should work, I guess.

for( set<string> :: iterator it = a.begin(); it != a.end(); ++it )
{
     set<string>:: iterator iter = find( b.begin(), b.end(), *it );
     if( iter == b.end() )
     {        // ^^^^^^^   Note: find returns b.end() if it does not find anything.
        c.insert(*iter)
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文