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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:49:40+00:00 2026-05-21T07:49:40+00:00

dear all, i have a map, defined as map<int, vector<int> > my_map . So,

  • 0

dear all,
i have a map, defined as map<int, vector<int> > my_map. So, for example it looks like as follows,

my_map=
0 1 2 6 5 4
1 0 2 3 4 5
2 0 1 4
3 1
4 1 2 0 7 5 6
5 1 0 6 4
6 0 5 4
7 4

vector<int> element_common(map<int, vector<int> > &my_map, int s1, int s2){
     vector<int> common2;
     vector<int> first_vector=my_map[s1];     
     vector<int>::iterator snd_vector_begin= my_map[s2].begin();
     vector<int>::iterator snd_vector_end= my_map[s2].end();          
     for (vector<int>::iterator any=first_vector.begin();
                            any!=first_vector.end();
                            any++){            
         // check if any is in other_list
         vector<int>::iterator any_in_snd_vector_iterator= find (snd_vector_begin,snd_vector_end, *any);            
         if(any_in_snd_vector_iterator==snd_vector_end){
            common2.push_back(*any); 
            }
         }
     return common2;

i wrote a function above function to get common elements from the vector parts which are relevant to 2 given keys. when keys are equal to s1 and s2, i.e. s1=1 and s2=4. then my function should give me 0,2,5. but i got 3,4. please help me to rectify my function.

  • 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-21T07:49:41+00:00Added an answer on May 21, 2026 at 7:49 am

    You got the test exactly backwards:

    if(any_in_2nd_vector_iterator==2nd_vector_end){
    

    is true when the element ISN’T FOUND in the second vector. Try !=.

    Then, you can avoid needless copying of vectors by using a reference:

    const vector<int>& first_vector=my_map[s1];
    

    and if your vectors get much larger, it may be faster to do a hash lookup (or pre-sorted merge if the vectors are always kept sorted) instead of a call to find which has to check every element.

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

Sidebar

Related Questions

dear all..i have a table it looks like: Name version DDX 01 DTX 05
Dear all, I now have a preliminary macro (defmacro key-if(test &key then else) `(cond
dear all, i have a string like this, ...1name: john, 2name: lice, 3name: mike....
Dear all, my issue is this. I have like the alert box to display
Dear All, i have a txt file like: --start-- jeff --end-- --start-- sophya --end--
Dear All, I used a vector of vector, say vector<vector<int> > no_1_2 to store
Dear All, I have 2 input string 1)stack,over,flow 2)stack/over,flow,com I would like to print
Dear all,Now i have this question in my java program,I think it should be
Dear all, I have a question about Facebook Page: ( NOT user profile page,
Dear all, I have a select query that currently produces the following results: DoctorName

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.