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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:17:21+00:00 2026-05-27T06:17:21+00:00

I am trying to remove elements from a std::list and keep some stats of

  • 0

I am trying to remove elements from a std::list and keep some stats of deleted elements.

In order to do so, I use the remove_if function from the list, and I have a predicate. I would like to use this predicate to gather statistics. Here is the code for the predicate:

  class TestPredicate
  {
  private:
    int limit_;

  public:
    int sum;
    int count;
    TestPredicate(int limit) : limit_(limit), sum(0), count(0) {}

    bool operator() (int value) 
    {
      if (value >= limit_)
      {
        sum += value;
        ++count;       // Part where I gather the stats
        return true;
      }
      else
        return false;
    }
  };

And here is the code for the algo:

std::list < int > container;
container.push_back(11);
TestPredicate pred(10);
container.remove_if(pred)
assert(pred.count == 1);

Unfortunately, the assertion is false because the predicate is passed by value. Is there a way to force it to be passed by reference ?

  • 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-27T06:17:22+00:00Added an answer on May 27, 2026 at 6:17 am

    Pass a reference wrapper, available from <functional>:

    container.remove_if(std::ref(pred));
    

    If you only have C++98/03 but your compiler has TR1, you can use <tr1/functional> and std::tr1::ref if you make a small amendment to your predicate:

    #include <tr1/functional>
    
    class TestPredicate : public std::unary_function<int, bool>
    {                 //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      // ...
    }
    
    container.remove_if(std::tr1::ref(pred));
    

    If all else fails, then you can hack up a manual solution with relative ease:

    struct predref
    {
      TestPredicate & p;
      bool operator()(int n) { return p(n); }
      predref(TestPredicate & r) : p(r) { }
    };
    
    container.remove_if(predref(pred));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to add and remove elements from a list mapped as .HasMany(),
I have an XML that I need remove empty elements from. I am trying
I am trying to remove elements from a Dictionary<string, List<string>> in C# when the
I am trying to remove comments from a list of elements that were obtained
I am trying to remove all HTML elements from a String. Unfortunately, I cannot
I'm trying to load page and then remove some elements in it. While doing
Let's say I am trying to remove elements from array a = [1,1,1,2,2,3] .
I'm trying to pass elements from a list to a for loop and of
I am trying to remove all script elements from a HTML page. But for
I trying to remove completely from DOM LI Elements and it's contents including it's

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.