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

  • Home
  • SEARCH
  • 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 8518869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:05:53+00:00 2026-06-11T06:05:53+00:00

In Python, set is pretty handy for comparing 2 lists of strings (see this

  • 0

In Python, set is pretty handy for comparing 2 lists of strings (see this link). I was wondering if there’s a good solution for C++ in terms of performance. As each list has over 1 million strings in it.

It’s case-sensitive matching.

  • 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-11T06:05:54+00:00Added an answer on June 11, 2026 at 6:05 am

    The data types std::set<> (usually implemented as a balanced tree) and std::unordered_set<> (from C++11, implemented as a hash) are available. There is also a convenience algorithm called std::set_intersection that computes the actual intersection.

    Here is an example.

    #include <iostream>
    #include <vector>
    #include <string>
    #include <set>             // for std::set
    #include <algorithm>       // for std::set_intersection
    
    int main()
    {
      std::set<std::string> s1 { "red", "green", "blue" };
      std::set<std::string> s2 { "black", "blue", "white", "green" };
    
      /* Collecting the results in a vector. The vector may grow quite
         large -- it may be more efficient to print the elements directly. */     
      std::vector<std::string> s_both {};
    
      std::set_intersection(s1.begin(),s1.end(),
                            s2.begin(),s2.end(),
                            std::back_inserter(s_both));
    
      /* Printing the elements collected by the vector, just to show that
         the result is correct. */
      for (const std::string &s : s_both)
        std::cout << s << ' ';
      std::cout << std::endl;
    
      return 0;
    }
    

    Note. If you want to use std::unordered_set<>, the std::set_intersection cannot be used like this, because it expects the input sets to be ordered. You’d have to use the usual technique of a for-loop iterating over the smaller set and finding the elements in the larger one to determine the intersection. Nevertheless, for a large number of elements (especially, strings), the hash-based std::unordered_set<> may be faster. There are also STL-compatible implementations such as the one in Boost (boost::unordered_set) and the one created by Google (sparse_hash_set and dense_hash_set). For various other implementations and benchmarks (including one for strings), see here.

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

Sidebar

Related Questions

I just set up IDE env for Python 3. I was wondering how I
I'm pretty handy with django and python but I'm terrible at the visual aspect
I am pretty new to Python world and trying to learn it. This is
This is a pretty straight forward attempt. I haven't been using python for too
In python, is there an easy way to set up a file-like object for
I have a python logger set up, using python's logging module. I want to
I'm turning a list into a set in Python, like so: request.session['vote_set'] = set(request.session['vote_set'])
Possible Duplicate: How to make python scripts executable on Windows? Set up Python on
In Python, you can set an alias for a module with 'as': import mymodule
I've tried to set up a Django model with a python property, like so:

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.