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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:24:21+00:00 2026-05-30T17:24:21+00:00

I have written a bit of code to match two vector of objects that

  • 0

I have written a bit of code to match two vector of objects that have some of the same instances of objects within both of the vectors.

The idea is to find the index of the object in the ‘main’ vector and match that to the object of the other vector.

The index of the main vector would then be used in a map with that object.

I think looking at the code may make my explanation a bit clearer:

ifndef OBJECTMAPMATCH_H
#define OBJECTMAPMATCH_H

#include <map>
#include <utility>
#include <vector>
#include <typeinfo>
#include <iostream>
#include <stdlib.h>

namespace ObjectMapMatch {

  ...
  ...
  template< class A, class B >
    std::map<int, B*>*  getIndexMap( std::vector<A*>* x , std::vector<B*>* y,  std::map<int, B*>* output )
     {
      typename std::vector<A*>::iterator Aitr  = x->begin();
      typename std::vector<A*>::iterator AitrE = x->end();
      typename std::vector<B*>::iterator Bitr  = y->begin();
      typename std::vector<B*>::iterator BitrE = y->end();

      for(int index=0; Aitr!=AitrE; ++Aitr, ++index){
        //Keep track of original index
        int AntupIndex = (*Aitr)->Index();
        int match = false;

        for(; Bitr!=BitrE; ++Bitr){
           int BntupIndex = (*Bitr)->Index();

           if( AntupIndex == BntupIndex ){
              match = true;
              output[index] = (*Bitr);
            }
           } //End of loop B

           if(!match){
             std::cout << "ERROR:ObjectMapMatch::getIndexMap: Can not Find Match" <<  typeid(y).name() << " FOR " << typeid(x).name() << std::endl;
             exit(1);
            }

           }//End of Loop A

          }

   ...
   ...
}
#endif

As you can see I am basically comparing the two objects with there unique index and if this matches the object will match.

My quesitons:

I know I could have overloaded the comparison operator in the object class, but I was not sure if something like this would be correct??

bool operator==(object1& lhs, object2& rhs){
   &lhs == &rhs ? return true : return false;
}

Also,

Is there a shorter/more efficient way of the above code using some STL algorithms (can not use the boost libs) or something smarter??

Mike

  • 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-30T17:24:22+00:00Added an answer on May 30, 2026 at 5:24 pm

    There are couple of ways to do this more efficiently than O(n^2).

    For example:

    • Sort elements of the first vector.
    • Sort elements of the second vector.
    • Use set_intersection on sorted vectors.

    Or:

    • Put elements of both vectors to multiset (or unordered_multiset).
    • Keys of the multiset that have more than one element indicate a match.

    For both of these methods, you could use pointers or indexes in original vectors instead of the actual elements. Just be careful to provide comparer (to sort, set_intersection and multiset) able to deal with pointers/indexes.

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

Sidebar

Related Questions

I have some old code written in C for 16-bit using Borland C++ that
I have written some C++ code that generates a std::vector. I also have a
I have written a bit of code that helps with versioning of js files.
I have written a bit of code that when the listview is clicked, the
I'm refactoring some code and I have written a method that modifies a Dictionary
I have just been re-working an old bit of compiler-like code written using bison.
I have written some code in my VB.NET application to send an HTML e-mail
I have written quite a bit of code which uses the Linq2Sql table relationships
I've written a bit of code in JavaScript that uses the window.navigator.language and the
I have written a function match-rewriter that is essentially match-lambda except that it returns

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.