从两个向量中删除公共实体? [英] Remove the common entities from two vector?

查看:99
本文介绍了从两个向量中删除公共实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有向量< class1a>,向量< class1b> 如何从两者中删除公共实体
我已经定义==运算符class1对象class1a,class1b

say I have vector<class1a>,vector<class1b> how to remove the common entities from both of them I have defined ==operator for the class1 objects class1a,class1b

推荐答案

stl算法提供了几个函数来执行集合操作,特别是计算

The stl algorithms provide several functions to perform set operations, notably calculating the set symmetric difference, which is what you need.

这里是一个使用示例:

#include <algorithm>
#include <vector>

int main(int argc, char **argv) {

    std::vector<int> v1;
    v1.push_back(1);
    v1.push_back(2);
    v1.push_back(3);
    v1.push_back(4);
    v1.push_back(5);
    v1.push_back(6);

    std::vector<int> v2;
    v2.push_back(2);
    v2.push_back(4);
    v2.push_back(6);
    v2.push_back(8);

    // Ranges must be sorted!
    std::sort(v1.begin(), v1.end());
    std::sort(v2.begin(), v2.end());

    std::vector<int> res; // Will contain the symmetric difference
    std::set_symmetric_difference(v1.begin(), v1.end(), 
                                  v2.begin(), v2.end(), 
                                  std::back_inserter(res));

    // Copy result to the output
    std::copy(res.begin(), res.end(), std::ostream_iterator<int>(cout, " "));
    // Prints "1 3 5"

    return 0;
}

std :: set_symmetric_difference 采用两个范围(即两对OutputIterator)和一个InputIterator,它将把结果。它还返回一个迭代器到结果范围的结尾。

std::set_symmetric_difference takes two range (i.e. two pairs of OutputIterators) and an InputIterator where it will put the result. It also returns an iterator to the end of the result range.


EDIT

EDIT

我刚读到您对您问题的意见。如果你想修改两个原始向量,可以使用 std :: set_difference

I just read your comments on your question. If you want the two original vectors to be modified, you can use std::set_difference:

vector<int>::iterator endRange;
endRange = set_difference(v1.begin(), v1.end(), 
                          v2.begin(), v2.end(), 
                          v1.begin());
v1.erase(endRange, v1.end());

这里,我们将设置差异v1 - v2的结果放入v1。然而,我们不能做反之亦然,因为v1现在被修改。解决方案是计算v1和v2的交集,然后与这个交集的差:

Here, we put the result of the set difference v1 - v2 into v1. However, we can't do the vice-versa since v1 is now modified. The solution is to calculate the intersection of v1 and v2, and then the difference with this intersection:

vector<int> inter;
set_intersection(v1.begin(), v1.end(),
                 v2.begin(), v2.end(),
                 back_inserter(inter));
// inter is "2 4 6"

v1.erase(set_difference(v1.begin(), v1.end(),
                        inter.begin(), inter.end(),
                        v1.begin())
         v1.end());
// v1 is "1 3 5"

v2.erase(set_difference(v2.begin(), v2.end(),
                        inter.begin(), inter.end(),
                        v2.begin())
         v2.end());
// v2 is "8"



我想有更多的高性能解决方案,这一个是清楚的,真正地通过使用广为人知的stl算法来传达你的意图。

I guess there are much more performant solutions, but this one is clear, and really convey your intents by using widely known stl algorithms.

这篇关于从两个向量中删除公共实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆