C++ C++;STL alogrithm-like';通信';公用事业

C++ C++;STL alogrithm-like';通信';公用事业,c++,algorithm,stl,comm,C++,Algorithm,Stl,Comm,有人能告诉我,如果STL中有一些算法可以用unix comm实用程序计算每次调用的差分和交集,请告诉我 int main() { //For example we have two sets on input std::set<int>a = { 1 2 3 4 5 }; std::set<int>b = { 3 4 5 6 7 }; std::call_some_func(a, b, ... ); //So as result we need

有人能告诉我,如果STL中有一些算法可以用unix comm实用程序计算每次调用的差分和交集,请告诉我

int main()  
{  
 //For example we have two sets on input
 std::set<int>a = { 1 2 3 4 5 };  
 std::set<int>b = { 3 4 5 6 7 };  

 std::call_some_func(a, b, ... );
 //So as result we need obtain 3 sets  
 //x1 = {1, 2}  // present in a, but absent in b (difference)  
 //x2 = {3, 4, 5} // present on both sets (intersection)  
 //x3 = {6, 7} // present in b, but absent in a  
}  
intmain()
{  
//例如,我们有两个输入集
std::seta={1 2 3 4 5};
std::setb={3 4 5 6 7};
std::调用某些函数(a,b,…);
//因此,我们需要获得3套
//x1={1,2}//存在于a中,但在b中不存在(差异)
//x2={3,4,5}//存在于两个集合上(交叉点)
//x3={6,7}//存在于b中,但不存在于a中
}  

我当前的实现使用了两个调用'std::set_difference'和一个调用'std::set_intersection'。

我认为这可能是一个相当有效的实现:

特点:

a) 以线性时间运行

b) 用于输入的所有有序容器类型和输出的所有迭代器类型


c) 仅需要
运算符我认为这可能是一个相当有效的实现:

特点:

a) 以线性时间运行

b) 用于输入的所有有序容器类型和输出的所有迭代器类型


c) 只需要
操作符没有一个函数可以做到这一点,您必须调用您提到的三个函数,或者自己编写一些东西。话虽如此,这是我的尝试,尽管我不确定它是否会比您已经描述的三步方法快

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

template <typename T>
void partition_sets(std::set<T> const& a,
                    std::set<T> const& b,
                    std::set<T>& difference_a,
                    std::set<T>& difference_b,
                    std::set<T>& intersection)
{
    std::set_intersection(begin(a), end(a),
                          begin(b), end(b),
                          std::inserter(intersection, intersection.begin()));

    std::copy_if(begin(a), end(a), std::inserter(difference_a, difference_a.begin()), [&intersection](int i)
    {
        return intersection.find(i) == intersection.end();  
    });

    std::copy_if(begin(b), end(b), std::inserter(difference_b, difference_b.begin()), [&intersection](int i)
    {
        return intersection.find(i) == intersection.end();  
    });
}

没有一个函数可以做到这一点,你必须调用你提到的三个函数,或者自己编写一些东西。话虽如此,这是我的尝试,尽管我不确定它是否会比您已经描述的三步方法快

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

template <typename T>
void partition_sets(std::set<T> const& a,
                    std::set<T> const& b,
                    std::set<T>& difference_a,
                    std::set<T>& difference_b,
                    std::set<T>& intersection)
{
    std::set_intersection(begin(a), end(a),
                          begin(b), end(b),
                          std::inserter(intersection, intersection.begin()));

    std::copy_if(begin(a), end(a), std::inserter(difference_a, difference_a.begin()), [&intersection](int i)
    {
        return intersection.find(i) == intersection.end();  
    });

    std::copy_if(begin(b), end(b), std::inserter(difference_b, difference_b.begin()), [&intersection](int i)
    {
        return intersection.find(i) == intersection.end();  
    });
}

只需为算法的三个调用编写一个包装器

比如说

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

template <class T>
auto comm(const std::set<T> &first, const std::set<T> &second)
{
    std::tuple<std::set<T>, std::set<T>, std::set<T>> t;

    std::set_difference(first.begin(), first.end(),
        second.begin(), second.end(),
        std::inserter(std::get<0>(t), std::get<0>(t).begin()));

    std::set_intersection(first.begin(), first.end(),
        second.begin(), second.end(),
        std::inserter(std::get<1>(t), std::get<1>(t).begin()));

    std::set_difference(second.begin(), second.end(),
        first.begin(), first.end(),
        std::inserter(std::get<2>(t), std::get<2>(t).begin()));

    return t;
}

int main()
{
    std::set<int> a = { 1, 2, 3, 4, 5 };
    std::set<int> b = { 3, 4, 5, 6, 7 };

    auto t = comm(a, b);

    for (auto x : std::get<0>(t)) std::cout << x << ' ';
    std::cout << std::endl;

    for (auto x : std::get<1>(t)) std::cout << x << ' ';
    std::cout << std::endl;

    for (auto x : std::get<2>(t)) std::cout << x << ' ';
    std::cout << std::endl;

    return 0;
}

只需为算法的三个调用编写一个包装器

比如说

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

template <class T>
auto comm(const std::set<T> &first, const std::set<T> &second)
{
    std::tuple<std::set<T>, std::set<T>, std::set<T>> t;

    std::set_difference(first.begin(), first.end(),
        second.begin(), second.end(),
        std::inserter(std::get<0>(t), std::get<0>(t).begin()));

    std::set_intersection(first.begin(), first.end(),
        second.begin(), second.end(),
        std::inserter(std::get<1>(t), std::get<1>(t).begin()));

    std::set_difference(second.begin(), second.end(),
        first.begin(), first.end(),
        std::inserter(std::get<2>(t), std::get<2>(t).begin()));

    return t;
}

int main()
{
    std::set<int> a = { 1, 2, 3, 4, 5 };
    std::set<int> b = { 3, 4, 5, 6, 7 };

    auto t = comm(a, b);

    for (auto x : std::get<0>(t)) std::cout << x << ' ';
    std::cout << std::endl;

    for (auto x : std::get<1>(t)) std::cout << x << ' ';
    std::cout << std::endl;

    for (auto x : std::get<2>(t)) std::cout << x << ' ';
    std::cout << std::endl;

    return 0;
}

没有一个函数可以做到这一点,你必须调用你提到的三个函数,或者写一些东西yourself@Sergey朱可夫为这三个调用编写了一个包装器。:)没有一个函数可以做到这一点,你必须调用你提到的三个函数,或者写点什么yourself@Sergey朱可夫为这三个调用编写了一个包装器:)看来向标准化委员会提交关于将此实现/算法纳入STL的提案是个好主意。谢谢。看来向标准化委员会提交关于将此实现/算法纳入STL的提案是个好主意。谢谢
int main()  
{  
    //For example we have two sets on input
    std::set<int> a = { 1, 2, 3, 4, 5 };  
    std::set<int> b = { 3, 4, 5, 6, 7 };  

    std::set<int> x1;
    std::set<int> x2;
    std::set<int> x3;
    partition_sets(a, b, x1, x2, x3);

    std::cout << "a - b\n\t";
    for (int i : x1)
    {
        std::cout << i << " ";
    }
    std::cout << "\n";

    std::cout << "b - a\n\t";
    for (int i : x2)
    {
        std::cout << i << " ";
    }
    std::cout << "\n";

    std::cout << "intersection\n\t";
    for (int i : x3)
    {
        std::cout << i << " ";
    }
}
a - b
    1 2 
b - a
    6 7 
intersection
    3 4 5 
#include <iostream>
#include<tuple>
#include <set>
#include <iterator>
#include <algorithm>

template <class T>
auto comm(const std::set<T> &first, const std::set<T> &second)
{
    std::tuple<std::set<T>, std::set<T>, std::set<T>> t;

    std::set_difference(first.begin(), first.end(),
        second.begin(), second.end(),
        std::inserter(std::get<0>(t), std::get<0>(t).begin()));

    std::set_intersection(first.begin(), first.end(),
        second.begin(), second.end(),
        std::inserter(std::get<1>(t), std::get<1>(t).begin()));

    std::set_difference(second.begin(), second.end(),
        first.begin(), first.end(),
        std::inserter(std::get<2>(t), std::get<2>(t).begin()));

    return t;
}

int main()
{
    std::set<int> a = { 1, 2, 3, 4, 5 };
    std::set<int> b = { 3, 4, 5, 6, 7 };

    auto t = comm(a, b);

    for (auto x : std::get<0>(t)) std::cout << x << ' ';
    std::cout << std::endl;

    for (auto x : std::get<1>(t)) std::cout << x << ' ';
    std::cout << std::endl;

    for (auto x : std::get<2>(t)) std::cout << x << ' ';
    std::cout << std::endl;

    return 0;
}
1 2
3 4 5
6 7