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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:36:17+00:00 2026-05-24T08:36:17+00:00

In my code below I want to inform for_each if I find the item?

  • 0

In my code below I want to inform for_each if I find the item? How do that?

#include <list>
#include <algorithm>
#include <functional>

using namespace std;

class widget {
 public:
  widget(int id) : m_id(id) {}

private:
   int m_id;
};

class findwidget {
public:
   findwidget(widget* p) : m_widget(p) {}

   bool operator()(widget* p) const { 
      return p == m_widget ? true : false;
   }

   widget* m_widget;
};

list<widget*> m_widgetList;

void push_back(widget* pi){
   if(m_widgetList.empty()) {
      m_widgetList.push_back(pi);
   } else {
      if(!std::for_each(m_widgetList.begin(), m_widgetList.end(), findwidget(pi)))
         m_widgetList.push_back(pi);
   }
}

int main(int argc, char* argv[])
{
   widget w1(1);
   push_back(&w1);
   return 0;
}
  • 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-24T08:36:18+00:00Added an answer on May 24, 2026 at 8:36 am

    The solution is : Dont use std::for_each. Use any from the following (whatever suits your situation):

    • std::find
    • std::find_if
    • std::find_first_of

    Example of std::find:

    if(std::find(m_widgetList.begin(), m_widgetList.end(),pi) == m_widgetList.end())
         m_widgetList.push_back(pi);
    

    Note if you use std::find, you don’t need the findWidget functor. After all, you’re comparing just the addresses (i.e the pointers).

    By the way, it seems that you want the list to contain unique elements, and don’t want to keep duplicates. If that is so, then you better consider:

    • std::set

    std::set automatically will handle the dupilcate elements. That is, you can do this:

    m_widgetSet.insert(pi);
    

    then it will insert pi into the set if its not in it. And if its already containing it, insert will not insert it into the set.

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

Sidebar

Related Questions

Using the code below, I want to keep the same menu div opened in
There is a script that triggers the code below I want to disallow executing
in the code below i want to implement the floyd algorithm,the user enter some
This is my ajax pager code below: Now I want to make sure that
I've got some very simple code below that I want to use as a
See code below: I want Page 1 to alert #testing... This works in C
I have the code below and I want to loop this every 5 seconds.
As a PHP developer, I always use the code below whenever I want to
The code below does not work but meant to illustrate what I want to
I want to know if the code below removes all input type='text' values back

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.