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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:26:46+00:00 2026-05-27T02:26:46+00:00

I need to erase elements from a vector based on their value, I’ve tried

  • 0

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

map<string, TLorentzVector> map_jets
vector<pair<string,double> > jets_pt


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

but I get a segmentation violation when jets_pt 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

  if( fabs(map_jets[it1->first].PseudoRapidity()) > 2.5 )
      jets_pt.erase(it1);

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

     if( jets_emfr[k] > 0.9 )                                                                                                                                                                                  
        jets_pt.erase(it);  
  • 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-27T02:26:46+00:00Added an answer on May 27, 2026 at 2:26 am

    I assume in your real code you actually call operator++ for the iterator somewhere in the loop. However you still have the problem that erase invalidates the iterator, so you would need to do

    it = jets_pt.erase(it1);
    

    However the remove_if - erase is really a much more suitable solution here
    I ould remmend something like the following:

    struct remove_functor
    {
        map<string, TLorentzVector>& map_jets;
        map<...>& map_leps;
    
        remove_functor(map<string, TLorentzVector>& m_jets, map<...>& m_leps): map_jets(m_jets), map_leps(m_leps)
        {}
    
        bool operator()(const pair<string,double>& p)
        {
            return (fabs(map_jets[p.first].PseudoRapidity()) > 2.5) 
               ||  ((map_jets[p.first].DeltaR(map_leps["lep1"]) < 0.4)
                 && (map_jets[p.first].DeltaR(map_leps["lep2"]) < 0.4)
                 && (map_jets[p.first].DeltaR(map_leps["lep3"]));
        }
     }
    

    Then you can simply use

    jets_pt.erase(remove_if(jets_pt.begin(), jets_pt.end(), remove_functor(map_jets, map_leps)), jets_pt.end());
    

    Of course in c++0x you can simply use a lambda function:

    auto predicate = [&](const pair<string, double>& p) {bool operator()(const pair<string,double>& p)
        {
            return (fabs(map_jets[p.first].PseudoRapidity()) > 2.5) 
               ||  ((map_jets[p.first].DeltaR(map_leps["lep1"]) < 0.4)
                 && (map_jets[p.first].DeltaR(map_leps["lep2"]) < 0.4)
                 && (map_jets[p.first].DeltaR(map_leps["lep3"]));
        };
    jets_pt.erase(remove_if(jets_pt.begin(), jets_pt.end(), predicate), jets_pt.end());
    

    predicate = [&](...){...}; creates an functor (by means of lambda syntax), which captures all used variables by reference (so map_jets, map_leps,..., indicated by the [&]) which can be used for the remove_if. auto means that the compiler should infer the type of the variable (since we don’t have a name for the type generated by the compiler for this lambda).

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

Sidebar

Related Questions

I need to remove elements from the middle of a std::vector. So I tried:
I need to take a C++ vector with potentially a lot of elements, erase
I want to clear a element from a vector using the erase method. But
I have a std::set and I need to erase similar adjacent elements: DnaSet::const_iterator next
I need to erase a certain session variable from a javascript function how can
I want to loop through a vector and erase certain elements that correspond to
Based on issues growing out of previous questions: vector::erase with pointer member , Remove
I need to Erase the file contents from a selected Point (C++ fstream) which
Need a way to allow sorting except for last item with in a list.
Need to call a filter function on some options based on a radio selected

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.