<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vishal Yadav</title>
    <description>The latest articles on DEV Community by Vishal Yadav (@vishal_yadav_6811).</description>
    <link>https://dev.to/vishal_yadav_6811</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2061030%2Ff3357fed-7964-46e9-80a1-0036943c90d1.png</url>
      <title>DEV Community: Vishal Yadav</title>
      <link>https://dev.to/vishal_yadav_6811</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vishal_yadav_6811"/>
    <language>en</language>
    <item>
      <title>Mastering accumulate in C++:</title>
      <dc:creator>Vishal Yadav</dc:creator>
      <pubDate>Fri, 10 Oct 2025 17:45:37 +0000</pubDate>
      <link>https://dev.to/vishal_yadav_6811/mastering-accumulate-in-c-4mn4</link>
      <guid>https://dev.to/vishal_yadav_6811/mastering-accumulate-in-c-4mn4</guid>
      <description>&lt;h2&gt;
  
  
  Mastering std::accumulate in C++:
&lt;/h2&gt;

&lt;p&gt;Simplify Your Aggregation Logic&lt;br&gt;
In C++, iterating over containers like arrays or vectors to perform operations such as summing values is common. However, the  library provides a powerful and concise tool, std::accumulate, that can reduce a range of values into a single result in just one line of code. This article explores how to use std::accumulate for various operations, including custom logic, with clear and practical examples.&lt;br&gt;
&lt;strong&gt;What is std::accumulate?&lt;/strong&gt;&lt;br&gt;
The std::accumulate function, defined in the  header, aggregates values in a range by repeatedly applying a binary operation (e.g., addition, multiplication, XOR) to produce a single result. It has two primary overloads:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;accumulate(first, last, initial_value);
accumulate(first, last, initial_value, binary_op);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;first: Iterator to the beginning of the range.&lt;br&gt;
last: Iterator to the end of the range (exclusive).&lt;br&gt;
initial_value: Starting value for the accumulation.&lt;br&gt;
binary_op: (Optional) A binary operation to apply (defaults to addition).&lt;/p&gt;

&lt;p&gt;Basic Usage: Summing a Vector&lt;br&gt;
Instead of writing a loop to sum elements, std::accumulate simplifies the process. Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;vector&amp;gt;
#include &amp;lt;numeric&amp;gt;

int main() {
    std::vector&amp;lt;int&amp;gt; nums = {1, 2, 3, 4, 5};
    int sum = std::accumulate(nums.begin(), nums.end(), 0);
    std::cout &amp;lt;&amp;lt; "Sum: " &amp;lt;&amp;lt; sum &amp;lt;&amp;lt; std::endl; // Output: Sum: 15
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is equivalent to the following traditional loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int sum = 0;
for (int n : nums) {
    sum += n;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using std::accumulate is more concise and expressive.&lt;br&gt;
Alternatively, you can use a compact header for competitive programming:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;bits/stdc++.h&amp;gt;
using namespace std;

int main() {
    vector&amp;lt;int&amp;gt; nums = {1, 2, 3, 4, 5};
    int sum = accumulate(nums.begin(), nums.end(), 0);
    cout &amp;lt;&amp;lt; sum &amp;lt;&amp;lt; endl; // Output: 15
    return 0;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Beyond Addition: Custom Operations&lt;br&gt;
std::accumulate supports custom binary operations, making it versatile for operations like multiplication, XOR, or even custom logic.&lt;br&gt;
Example 1: Multiplication&lt;br&gt;
To compute the product of all elements in a vector:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;vector&amp;gt;
#include &amp;lt;numeric&amp;gt;
#include &amp;lt;functional&amp;gt;

int main() {
    std::vector&amp;lt;int&amp;gt; nums = {1, 2, 3, 4, 5};
    int product = std::accumulate(nums.begin(), nums.end(), 1, std::multiplies&amp;lt;int&amp;gt;());
    std::cout &amp;lt;&amp;lt; "Product: " &amp;lt;&amp;lt; product &amp;lt;&amp;lt; std::endl; // Output: Product: 120
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;std::multiplies&amp;lt;int&amp;gt;()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is used as the binary operation, and the initial value is 1 (since multiplying by 0 would yield 0).&lt;br&gt;
Example 2: Bitwise XOR&lt;br&gt;
To compute the XOR of all elements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;vector&amp;gt;
#include &amp;lt;numeric&amp;gt;
#include &amp;lt;functional&amp;gt;

int main() {
    std::vector&amp;lt;int&amp;gt; nums = {1, 2, 3, 4, 5};
    int xor_result = std::accumulate(nums.begin(), nums.end(), 0, std::bit_xor&amp;lt;int&amp;gt;());
    std::cout &amp;lt;&amp;lt; "XOR: " &amp;lt;&amp;lt; xor_result &amp;lt;&amp;lt; std::endl; // Output: XOR: 1
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Custom Logic with Lambda Functions&lt;br&gt;
For more complex scenarios, you can provide a custom lambda function as the binary operation.&lt;br&gt;
Example 3: Concatenating Strings&lt;br&gt;
To concatenate a vector of strings into a single string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;vector&amp;gt;
#include &amp;lt;numeric&amp;gt;
#include &amp;lt;string&amp;gt;

int main() {
    std::vector&amp;lt;std::string&amp;gt; words = {"C++", " ", "is", " ", "awesome!"};
    std::string sentence = std::accumulate(
        words.begin(), words.end(), std::string(""),
        [](const std::string &amp;amp;a, const std::string &amp;amp;b) {
            return a + b;
        });
    std::cout &amp;lt;&amp;lt; sentence &amp;lt;&amp;lt; std::endl; // Output: C++ is awesome!
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the &lt;code&gt;lambda [](const std::string &amp;amp;a, const std::string &amp;amp;b) { return a + b; }&lt;/code&gt;defines the custom concatenation logic.&lt;br&gt;
Example 4: Counting Even Numbers&lt;br&gt;
To count how many numbers in a vector are even:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;vector&amp;gt;
#include &amp;lt;numeric&amp;gt;

int main() {
    std::vector&amp;lt;int&amp;gt; nums = {1, 2, 3, 4, 5, 6};
    int even_count = std::accumulate(
        nums.begin(), nums.end(), 0,
        [](int count, int n) {
            return count + (n % 2 == 0 ? 1 : 0);
        });
    std::cout &amp;lt;&amp;lt; "Even numbers: " &amp;lt;&amp;lt; even_count &amp;lt;&amp;lt; std::endl; // Output: Even numbers: 3
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;lambda [](int count, int n) { return count + (n % 2 == 0 ? 1 : 0); }&lt;/code&gt; increments the count for even numbers.&lt;br&gt;
Why Use std::accumulate?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conciseness: Reduces verbose loops to a single line.&lt;br&gt;
Flexibility: Supports any binary operation, including custom ones via lambdas.&lt;br&gt;
Readability: Makes the intent of the code clearer.&lt;br&gt;
Performance: Often optimized by compilers for efficiency.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The std::accumulate function is a powerful tool in C++ for aggregating values in a range. Whether you're summing numbers, computing products, performing bitwise operations, or implementing custom logic, std::accumulate simplifies your code while maintaining flexibility. Next time you need to reduce a container to a single value, consider reaching for std::accumulate!&lt;br&gt;
Try experimenting with std::accumulate in your projects to streamline your aggregation logic. Happy coding!&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>bitmaipulation</category>
      <category>accumulate</category>
      <category>competativeprogramming</category>
    </item>
  </channel>
</rss>
