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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:50:33+00:00 2026-05-24T13:50:33+00:00

I have two space separated strings… (the X doesn’t mean the same symbol) st1

  • 0

I have two space separated strings… (the X doesn’t mean the same symbol)

st1 = "abc def kok...."
st2 = "kok bbr def ffe ...."

i would like to construct an intersection string as follows:
common = "kok def"

what is the efficient way to do so in c++?

Thanks

  • 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-24T13:50:33+00:00Added an answer on May 24, 2026 at 1:50 pm

    Use std::set_intersection

    Sample program:

    I’m assuming you’ve tokenized your strings already (this solution seems easy to implement).

    // Data
    std::vector<string> a,b;
    a.push_back("abc");b.push_back("kok");
    a.push_back("def");b.push_back("bbr");
    a.push_back("kok");b.push_back("def");
    a.push_back("foo");b.push_back("ffe");
    
    // Allocate space for intersection
    std::vector<string> v(a.size()+b.size());
    
    // Sort as required by set_intersection
    std::sort(a.begin(),a.end());
    std::sort(b.begin(),b.end());
    // Compute
    std::vector<string>::iterator it = std::set_intersection(a.begin(),a.end(),b.begin(),b.end(),v.begin());
    
    // Display
    v.erase(it,v.end());
    for(std::vector<string>::iterator it = v.begin();it < v.end(); ++it) std::cout<<*it<<std::endl;
    

    Complexity should be O(n log n) in the number of tokens (or sub-strings).

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

Sidebar

Related Questions

I am trying to read in two strings separated by a space. cin>>a; cin>>b;
I have a text file that contains date, and two time-strings separated by spaces
abc def abcd efgh If I have a large string with a space that
In a two dimentional integer space, you have two points, A and B. This
I have two points in 3D space: a = (ax, ay, az) b =
i have two points in 3D space which have X-coordinates with different signum. so
I have two 2d circles in 3d space (defined by a center, normal, and
I have a UILabel with space for two lines of text. Sometimes, when the
I am trying to accept input of two integers, separated by a space: 3
For performance reasons I have separated my 2D and 3D rendering. I have two

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.