Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8693817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:37:49+00:00 2026-06-13T00:37:49+00:00

I have two sorted C++ std::vector without duplicates (you could call them sets) and

  • 0

I have two sorted C++ std::vector without duplicates (you could call them sets) and I want to know if they intersect. I do not need the vector of common elements.

I wrote the code at the end of this question using the boost::set_intersection algorithm in the boost “range” library (http://www.boost.org/doc/libs/1_50_0/libs/range/doc/html/range/reference/algorithms/set.html). This code avoids constructing the set of common elements but does scan all the elements of the vectors.

Is it possible to improve my function “intersects” using boost and the C++ STL without using a loop? I’d like to stop at the first common element in the vectors or at the very least avoid my counter class.

The boost range library provides “includes” and “set_intersection” but not “intersects”. This makes me think that “intersects” is trivial or provided elsewhere but I cannot find it.

thanks!

#include <vector>
#include <string>
#include <boost/assign/list_of.hpp>
#include <boost/function_output_iterator.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/algorithm_ext/erase.hpp>

template<typename T>
class counter
{
    size_t * _n;
public:
    counter(size_t * b) : _n(b) {}
    void operator()(const T & x) const
    {
        ++*_n;
    }
};

bool intersects(const std::vector<std::string> & a, const std::vector<std::string> & b)
{
    size_t found = 0;
    boost::set_intersection(a, b, boost::make_function_output_iterator(counter<std::string>(&found)));
    return found;
}

int main(int argc, char ** argv)
{
    namespace ba = boost::assign;
    using namespace std;
    vector<string> a = ba::list_of(string("b"))(string("vv"))(string("h"));
    vector<string> b = ba::list_of(string("z"))(string("h"))(string("aa"));
    boost::erase(a, boost::unique<boost::return_found_end>(boost::sort(a)));
    boost::erase(b, boost::unique<boost::return_found_end>(boost::sort(b)));
    cout << "does " << (intersects(a, b) ? "" : "not ") << "intersect\n";
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:37:51+00:00Added an answer on June 13, 2026 at 12:37 am

    Firstly to answer the comment, boost’s set_intersection takes ranges as parameters compared to the STL one which takes iterators.

    Other than that, there is no real difference with regards to the algorithm and the complexity.

    As far as I know there is no ready-made library function to do what you want to do, which is just test if two sequences are unique and stop immediately if they are not.

    You also have to realise that you will always have “worst case scenario” when they really are unique.

    The complexity is O(N+M) although you can also use binary-search on one of the collections which will make it O(N log M) or O(M log N) and if one is a lot larger than the other this can be a big saving. (e.g. N=1000000, M=20, M log N is only approx 400)

    You can “reduce” by taking the median of one, find it in the other and compare the sub-ranges in separate threads.

    There is also the “horrible” solution of having your functor that gets called on an intersection throw, thus breaking you out of the loop. (Yes there is one there, even if it’s hidden in an algorithm). We can probably write our own though that is O(N+M) very simply. I will do it with 4 iterators:

     template< typename Iter1, typename Iter2 >
     bool intersects( Iter1 iter1, Iter1 iter1End, Iter2 iter2, Iter2 iter2End )
     {
          while( iter1 != iter1End && iter2 != iter2End )
          {
             if( *iter1 < *iter2 )
             {
                 ++iter1;
             }
             else if ( *iter2 < *iter1 )
             {
                 ++iter2;
             }
             else
                 return true;
          }
          return false;
     }
    
     // Predicate version where the compare version returns <0 >0 or 0
    
     template< typename Iter1, typename Iter2, typename Comp >
     bool intersects( Iter1 iter1, Iter1 iter1End, Iter2 iter2, Iter2 iter2End, Comp comp )
     {
          while( iter1 != iter1End && iter2 != iter2End )
          {
             int res = comp( *iter1, *iter2 );
             if( res < 0 )
             {
                 ++iter1;
             }
             else if ( res > 0 )
             {
                 ++iter2;
             }
             else
                 return true;
          }
          return false;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
We have two sorted arrays of the same size n. Let's call the array
I have an assignment to merge two sorted vectors into a third sorted vector.
I have two lists - let's call them details and totals . The items
I have two lists that are already sorted how they need to be, and
For simplicity, let's say that I have two sets of words, sorted into alphabetical
I need the following functionality Given two sorted lists, merge them I have this
I have two or more javascript objects. I want to merge them adding values
I have this, and it works: # E. Given two lists sorted in increasing
I have two columns say Main and Sub . (they can be of same

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.