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 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, A newbie to XPATH, I need an xpath to get the HREF
Possible Duplicate: R: Finding patterns across multiple columns- possibly duplicated()? Dear all, Here is
I have libTestLibrary.so library and I want add it to Android-emulator's lib folder. How
dear i am confused the use of content provider in android applications.i go through
Dear friends,I want to extract text 平均3.6 星 from this code segment excerpted from
hi my dear friends : how can i get href attrib of anchor element
hi my dear friends : the below code works perfect in ie9 , but
I have a .NET method for doing DES encryption on a string: public static
I'm working on a game engine in C++ using Lua for NPC behaviour. I
I am learning to how send email with Rails 3 with Agile Web Development

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.