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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:43:22+00:00 2026-05-25T12:43:22+00:00

I trying to convert some loops in my code to use the for_each functionality

  • 0

I trying to convert some loops in my code to use the for_each functionality of the STL. Currently, I calculate and accumulate two separate values over the same set of data, requiring me to loop over the data twice. In the interest of speed, I want to loop once and accumulate both values. Using for_each was suggested as it apparently can be worked into a multithreaded or multiprocessor implementation fairly easily (I haven’t learned how to do that yet.)

Creating a function that only loops over the data once and calculates both values is easy, but I need to return both. To use with for_each, I need to return both calculated values at each iteration so STL can sum them. From my understanding, this isn’t possible as for_each expects a single value returned.

The goal with using for_each, besides cleaner code (arguably?) is to eventually move to a multithreaded or multiprocessor implementation so that the loop over the data can be done in parallel so things run faster.

It was suggested to me that I look at using a functor instead of a function. However, that raises two issues.

  1. How will using a functor instead allow the return accumulation of two values?
  2. I have two methods of applying this algorithm. The current code has a virtual base class and then two classes that inherit and implement the actual working code. I can’t figure out how to have a “virtual functor” so that each method class can implement its own version.

Thanks!

  • 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-25T12:43:23+00:00Added an answer on May 25, 2026 at 12:43 pm

    Here is an example of using a functor to perform two accumulations in parallel.

    struct MyFunctor
    {
        // Initialise accumulators to zero
        MyFunctor() : acc_A(0), acc_B(0) {}
    
        // for_each calls operator() for each container element
        void operator() (const T &x)
        {
            acc_A += x.foo();
            acc_B += x.bar();
        }
    
        int acc_A;
        int acc_B;
    };
    
    
    // Invoke for_each, and capture the result
    MyFunctor func = std::for_each(container.begin(), container.end(), MyFunctor());
    

    [Note that you could also consider using std::accumulate(), with an appropriate overload for operator+.]

    As for virtual functors, you cannot do these directly, as STL functions take functors by value, not by reference (so you’d get a slicing problem). You’d need to implement a sort of “proxy” functor that in turn contains a reference to your virtual functor.* Along the lines of:

    struct AbstractFunctor
    {
        virtual void operator() (const T &x) = 0;
    };
    
    struct MyFunctor : AbstractFunctor
    {
        virtual void operator() (const T &x) { ... }
    };
    
    struct Proxy
    {
        Proxy(AbstractFunctor &f) : f(f) {}
        void operator() (const T &x) { f(x); }
        AbstractFunctor &f;
    };
    
    MyFunctor func;
    std::for_each(container.begin(), container.end(), Proxy(func));
    

    * Scott Meyers gives a good example of this technique in Item 38 of his excellent Effective STL.

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

Sidebar

Related Questions

I'm trying to convert some code that worked great in VB, but I can't
I'm trying to convert some code from C# to C so that it can
There is some code that I'm trying to convert from IList to IEnumerable :
I'm attempting to convert some of my code from Tkinter to wxPython. Currently I'm
I'm trying to convert some strings that are in French Canadian and basically, I'd
I'm trying to convert some Xaml to HTML using the .NET XslCompiledTransform and am
I am trying to convert some of my stored procedures to Linq and am
I am having some issues trying to convert a double to C++ string. Here
I am trying to convert an SVN repository to Mercurial, but I'm having some
I am trying to convert some xml into a json object using PHP. This

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.