How to erase last and/or unique element in a vector?
<p>I need to erase elements from a vector based on their value, I've tried the idiom erase-remove_if but the condition for the item to be erased is not so simple, I've tried something like</p>

<pre><code>map&lt;string, TLorentzVector&gt; map_jets
vector&lt;pair&lt;string,double&gt; &gt; jets_pt


  for( vector&lt;pair&lt;string,double&gt; &gt;::iterator it1 = jets_pt.begin(); it1 != jets_pt.end(); ){
   if( fabs(map_jets[it1-&gt;first].PseudoRapidity()) &gt; 2.5 )
      jets_pt.erase(it1);
</code></pre>

<p>but I get a <code>segmentation violation</code> when <code>jets_pt</code> has size = 1. 
The whole program takes data of an experiment and loops over, the map keeps track of the name of the event and the associated variable I need, while the vector stores the string of the map and a value that I need to keep track of.
I want to delete from the vector those value which does'n satisfy a few condition</p>

<pre><code>  if( fabs(map_jets[it1-&gt;first].PseudoRapidity()) &gt; 2.5 )
      jets_pt.erase(it1);

    if( map_jets[it-&gt;first].DeltaR(map_leps["lep1"]) &lt; 0.4 &amp;&amp; map_jets[it-&gt;first].DeltaR(map_leps["lep2"]) &lt; 0.4 &amp;&amp; map_jets[it-&gt;first].DeltaR(map_leps["lep3"]) )                                            
        jets_pt.erase(it);  

     if( jets_emfr[k] &gt; 0.9 )                                                                                                                                                                                  
        jets_pt.erase(it);  
</code></pre>
