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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:12:03+00:00 2026-06-10T01:12:03+00:00

Consider, I’m use std::for_each and object with overloaded operator() to accumulate some data about

  • 0

Consider, I’m use std::for_each and object with overloaded operator() to accumulate some data about vector content:

#include <iostream>
#include <vector>
#include <algorithm>

struct A{
    int a;
    A(): a(0){}

    void operator()(int i) {
        if(i)   a++;
        std::cout << "a:" << a << std::endl;
    }
};

int main(int argc, char *argv[]) {
    //test data
    std::vector<int> vec;
    vec.push_back(1);
    vec.push_back(1);
    vec.push_back(0);

    //accumulator
    A accum;

    std::for_each(vec.begin(), vec.end(), accum);
    std::cout << "non-zero elements:" << accum.a << std::endl;

    return 0;
}

This outputs:

a:1
a:2
a:2
non-zero elements:0

Why is non-zero elements 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-06-10T01:12:04+00:00Added an answer on June 10, 2026 at 1:12 am

    std::for_each() does not take its third argument by reference, so a copy of accum is made.

    If you add std::cout statements to A::A() you can witness this behaviour.

    Just to note, you can solve this particular problem using std::count_if():

    std::cout << "non-zero elements: "
              << std::count_if(vec.begin(),
                               vec.end(),
                               [](const int i) { return i != 0; })
              << std::endl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this code: #include <iostream> using namespace std; typedef int array[12]; array sample; array
Consider a standard use of the CRTP , for some expression template mechanism, which
Consider a table invoice_data containing data about invoices over 12 years. This data will
Consider the following Mechanize form object #<Mechanize::Form {name f1} {method POST} {action f.php} {fields
Consider the following script for outputting some XML code: var xmlAsString = '<?xml version=1.0?><person><name
Consider: Public MustInherit Class Column Public ReadOnly Property ReturnSomethingUseful() As Object Get 'return something
Consider the following program which inserts a range of elements into a vector: vector<string>
Consider this code: #include <iostream> int main() { int iTemp = 0; iTemp +=
Consider following case: #include<stdio.h> int main() { char A[5]; scanf(%s,A); printf(%s,A); } My question
Consider the following invalid C++ code. #include <assert.h> class NodeInterface { public: virtual ~NodeInterface

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.