[Home]STLAlgorithmExtensions/SetsIntersect

BOOST WIKI | STLAlgorithmExtensions | RecentChanges | Preferences | Page List | Links List

Difference (from prior major revision) (no other diffs)

Changed: 25,26c25
intersect, but this function has two advantages. You do not have to
allocate space for the output set, and this function returns true as soon as
intersect, but this function has one advantage. This function returns true as soon as

Changed: 36c35,70



You don't have to allocate space using std::set_intersection, just use output "iterator" like that:

struct SumIter
{
SumIter(int count = 0) : count(count) {}
SumIter& operator++()
{
++count;
return *this;
}
SumIter operator++(int)
{
SumIter res = *this;
++count;
return res;
}
SumIter& operator*()
{
return *this;
}
SumIter& operator=(int x)
{
count = 0;
}
template<class T>
SumIter& operator=(T t)
{
return *this;
}
int count;
};
...
std::set_intersection(f1, l1, f2, l2, get_count_only).count

So I'd suggest leaving set_intersection as is, just use SumIter? (maybe name should be like get_count_only)

One algorithm that I have found useful is sets_intersect(). It takes two sorted ranges and return a boolean saying whether they have any elements in common.

template <typename Iterator1, typename Iterator2>
bool sets_intersect(Iterator1 start1, Iterator1 end1, Iterator2 start2,
Iterator2 end2)
{
    while ((start1 != end1) && (start2 != end2)) {
          if (*start1 < *start2) {
                ++start1;
         } else if (*start2 < *start1) {
               ++start2;
         } else {
               return true;
         }
     }
    return false;
}

There is a similar function that takes a predicate as a third template parameter. You could use std::set_intersection() to determine if two ranges intersect, but this function has one advantage. This function returns true as soon as the first point of intersection is found. The one thing I'm not sure about is the name. It's very close to std::set_intersection(), but I can't think of a better one off the top of my head.


Another variation that might be useful is count_intersection, which would return the number of elements in the intersection. --People/JeremySiek
I suggest has_intersection as the name -- People/Jeff Garland
You don't have to allocate space using std::set_intersection, just use output "iterator" like that:
struct SumIter
{
    SumIter(int count = 0) : count(count) {}
    SumIter& operator++()
    {
        ++count;
        return *this;
    }
    SumIter operator++(int)
    {
	SumIter res = *this;
        ++count;
        return res;
    }
    SumIter& operator*()
    {
        return *this;
    }
    SumIter& operator=(int x)
    {
    	count = 0;
    }
    template<class T>
    SumIter& operator=(T t)
    {
        return *this;
    }
    int count;
};
...
std::set_intersection(f1, l1, f2, l2, get_count_only).count
So I'd suggest leaving set_intersection as is, just use SumIter? (maybe name should be like get_count_only)
BOOST WIKI | STLAlgorithmExtensions | RecentChanges | Preferences | Page List | Links List
Edit text of this page | View other revisions
Last edited January 10, 2007 2:48 pm (diff)
Search:
Disclaimer: This site not officially maintained by Boost Developers