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 1052021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:01:34+00:00 2026-05-16T17:01:34+00:00

From the title you’d almost assuredly think use set_union to create a list and

  • 0

From the title you’d almost assuredly think use set_union to create a list and then check if it’s empty. However, the objects I’m comparing are “expensive” to copy. I’ve looked at includes but that only works if all the items of one list are found in another. I’ve also looked at mismatch but rejected it for obvious reasons.

I can and have written my own function which assumes both lists are sorted but I’m wondering if an efficient function already exists in the STL. (Project is forbidden to use third-party libraries including Boost and TR1, don’t ask.)

  • 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-05-16T17:01:34+00:00Added an answer on May 16, 2026 at 5:01 pm

    If the sets are unsorted, then you can use find_first_of for an O(N*M) algorithm.

    If they are sorted (which would be required for set_intersection anyway), then you can iterate over one set calling equal_range in the other for every element. If every returned range is empty, there is no intersection. Performance is O(N log M).

    However, there is no excuse not to have O(N+M) performance, right? Nothing is copied by set_intersection if it’s passed a dummy iterator.

    struct found {};
    
    template< class T > // satisfy language requirement
    struct throwing_iterator : std::iterator< std::output_iterator_tag, T > {
        T &operator*() { throw found(); }
        throwing_iterator &operator++() { return *this; }
        throwing_iterator operator++(int) { return *this; }
    };
    
    template< class I, class J >
    bool any_intersection( I first1, I last1, J first2, J last2 ) {
        try {
            throwing_iterator< typename std::iterator_traits<I>::value_type > ti;
            set_intersection( first1, last1, first2, last2, ti );
            return false;
        } catch ( found const& ) {
            return true;
        }
    }
    

    This provides for early exit. You could alternately avoid the exception and just have the iterator remember how many times it was incremented, and no-op the assignment.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to parse the title from the following webpage: http://kid37.blogger.de/stories/1670573/ When I use
as from Title.
The question from title doesn't need more details.
SELECT events.title FROM events ORDER BY events.title DESC I'm getting the proper ordering for
Pretty clear from the title itself, what is meant by porting an application X
As you can guess from the title, the outlining with regions finally made me
I'm trying to parse a title from the following piece of html: Website (Newton)
I want a regular expression to extract the title from a HTML page. Currently
What is the easiest way to get the title from the youtybe video ,
hello guys i am trying to print title from a set of urls in

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.