如何区分两个std :: set <string>的元素?

Rel*_*lla 3 c++ string diff set

所以我们已经set<string> aset<string> b我们希望得到std::set<string> c哪些将包含可以代表的项目a - b(意味着a如果我们从中移除所有项目b,如果b包含多个a或不存在的项目,a我们希望保持它们类似于这样的简单数学数字:5-6 = 0while 3-2 = 1)

Mic*_*urr 11

我想你想要std::set_difference()<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;
}
Run Code Online (Sandbox Code Playgroud)