4

给定 2 个集合 (C++) 是否有一种方便的方法可以在没有任何分配的情况下获取交叉点的大小(如 std::set_intersection 所做的那样)

当然,我可以复制实现减去分配,但我总是宁愿不重新发明轮子

int count = 0;
while (first1!=last1 && first2!=last2)
{
    if (*first1<*first2) ++first1;
    else if (*first2<*first1) ++first2;
    else {
        count++; ++first1; ++first2;
    }
 }

我正在考虑使用 std::set_intersection 并传递一个“计数”插入器......?

4

2 回答 2

4

在 Boost Iterator 库和 C++14 的通用 lambda 的帮助下:

#include <set>
#include <algorithm>
#include <iostream>
#include <boost/function_output_iterator.hpp>

int main()
{
    std::set<int> s1 { 1,2,3,4 };
    std::set<int> s2 { 3,4,5,6 };

    int i = 0;
    auto counter = [&i](auto){ ++i; };  // C++14
 // auto counter = [&i](int ){ ++1; };  // C++11
 // pre C++11, you'd need a class with overloaded operator()

    std::set_intersection(
        s1.begin(), s1.end(), s2.begin(), s2.end(),
        boost::make_function_output_iterator(counter)
    );

    std::cout << i;
}

输出是2

于 2014-09-10T11:57:40.547 回答
0

另一种解决方案可能是查看std::set_intersection代码并实现您的计数器类以反映其行为。这取决于 operator++ 的使用, std::set_intersection使用前缀,但我还添加了后缀运算符。

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

class CountIt {
    public:
    CountIt() : storage(0), counter(0) {}
    CountIt& operator++()
    {
        ++counter;
        return *this;
    }
    CountIt operator++(int)
    {
        CountIt oldValue = *this;
        return ++( *this);
    }
    int& operator*() { return storage;}
    int storage, counter;
};

int main()
{
    std::set<int> s1 { 1,2,3,4 };
    std::set<int> s2 { 3,4,5,6 };

   CountIt const & c = std::set_intersection(
        s1.begin(), s1.end(), s2.begin(), s2.end(),
        CountIt()
    );

    std::cout << c.counter;  // 2, hopefuly
}

http://ideone.com/j8GrBB

于 2014-09-10T17:24:07.610 回答