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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:36:31+00:00 2026-06-16T16:36:31+00:00

I have: struct Mystruct { void Update(float Delta); } typedef std::map<int, Mystruct*> TheMap; typedef

  • 0

I have:

struct Mystruct
{
    void Update(float Delta);
}

typedef std::map<int, Mystruct*> TheMap;
typedef TheMap::iterator         TheMapIt;

TheMap Container;

and wants to do:

for(TheMapIt It = Container.begin(), Ite = Container.end(); It != Ite; ++It)
{
    It->second->Update(Delta);
}

using std::for_each, how to do this?

I think I can declare function like:

void Do(const std::pair<int, Mystruct*> Elem)
{
    Elem->Update(/*problem!*/); ---> How to pass Delta in?
}

Or make another struct:

struct Doer
{
    Doer(float Delta): d(Delta) {}

    void operator(std::pair<int, Mystruct*> Elem)
    {
        Elem->Update(d);
    }
}

But this requires a new struct.

What I wants to achieve is using plain std::for_each with something like std::bind_1st, std::mem_fun like the way with std::vector, is it possible?

Please consider using std way before using boost, thanks!

I’ve referenced this but it doesnt metion about member function with input…
How would I use for_each to delete every value in an STL map?

  • 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-06-16T16:36:34+00:00Added an answer on June 16, 2026 at 4:36 pm

    This is just a trade between coding style, for loop and for_each doesn’t make big difference, below are two other approaches besides for loop:

    If you use C++11, could try lambda:

    std::for_each(TheMap.begin(), TheMap.end(), 
                  [](std::pair<int, Mystruct*>& n){ n.second->Update(1.0); });
    

    Or in C++03, you could add a member function to wrapper class then call std::bind1st and std::mem_fun

    struct MapWrapper
    {
      MapWrapper(int value=1.0):new_value(value) {}
    
      void Update(std::pair<int, Mystruct*> map_pair)
      {
        map_pair.second->Update(new_value);
      }
      void setValue(float value) { new_value = value; }
      float new_value;
      std::map<int, Mystruct*> TheMap;
    };
    
    MapWrapper wrapper;
    wrapper.setvalue(2.0);
    std::for_each(wrapper.TheMap.begin(), 
                  wrapper.TheMap.end(),std::bind1st(std::mem_fun(&MapWrapper::Update), &wrapper));
    

    Write a functor isn’t a bad choice, why you are against it? A functor provides better design as it provides clean and clear purpose.

    struct Doer
    {
        Doer(float Delta): d(Delta) {}
    
        void operator()(std::pair<int, Mystruct*> e)
        {
          e.second->Update(d);
        }
        float d;
    };
    Doer doer(1.0);
    std::for_each(wrapper.TheMap.begin(), wrapper.TheMap.end(), doer);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this problem... i have a my struct: typedef struct Mystruct{ float a;
I have a structure typedef struct myStruct_st { int a; }myStruct; It can be
I have a struct like this: typedef struct { int sizes[3]; float **vals[3]; //
I have this C code : #include<stdio.h> typedef struct { int foo; } MyStruct;
I have a structure: struct mystruct { int* pointer; }; structure mystruct* struct_inst; Now
I have a struct that looks like this: typedef struct _my_struct { float first_vector[SOME_NUM][OTHER_NUM];
Ok so I have struct like this typedef struct { float x; float y;
let's say I have this: struct myStruct { int A; int B; } Is
I have a global var struct: typedef struct { int myvar1; int myvar2; int
Say I have this struct: struct MyStruct { int iID; int iMyNumber; }; Then

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.