Matching variables in two Vectors and making an Map of the comparison in an efficient way
<p>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.</p>

<p>The idea is to find the index of the object in the 'main' vector and match that to the object of the other vector.</p>

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

<p>I think looking at the code may make my explanation a bit clearer:</p>

<pre><code>ifndef OBJECTMAPMATCH_H
#define OBJECTMAPMATCH_H

#include &lt;map&gt;
#include &lt;utility&gt;
#include &lt;vector&gt;
#include &lt;typeinfo&gt;
#include &lt;iostream&gt;
#include &lt;stdlib.h&gt;

namespace ObjectMapMatch {

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

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

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

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

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

           }//End of Loop A

          }

   ...
   ...
}
#endif
</code></pre>

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

<p>My quesitons:</p>

<p>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??</p>

<pre><code>bool operator==(object1&amp; lhs, object2&amp; rhs){
   &amp;lhs == &amp;rhs ? return true : return false;
}
</code></pre>

<p>Also,</p>

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

<p>Mike</p>
