stl - How to sum up elements of a C++ vector?

Stl - How to sum up elements of a C++ vector?

You can sum up the elements of a C++ vector using various methods. Here are a few common approaches:

  1. Using a Loop: Iterate through the vector and accumulate the sum.
#include <iostream> #include <vector> int sumVector(const std::vector<int>& vec) { int sum = 0; for (int i = 0; i < vec.size(); ++i) { sum += vec[i]; } return sum; } int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; int result = sumVector(vec); std::cout << "Sum of elements: " << result << std::endl; return 0; } 
  1. Using STL Algorithms: Use the std::accumulate algorithm from the <numeric> header.
#include <iostream> #include <vector> #include <numeric> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; int result = std::accumulate(vec.begin(), vec.end(), 0); std::cout << "Sum of elements: " << result << std::endl; return 0; } 
  1. Using Range-Based For Loop: Use a range-based for loop to iterate through the elements and accumulate the sum.
#include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; int sum = 0; for (int num : vec) { sum += num; } std::cout << "Sum of elements: " << sum << std::endl; return 0; } 

All these methods will give you the sum of all elements in the vector. Choose the method that best fits your coding style and requirements.

Examples

  1. C++ STL Accumulate Function

    Description: Use the std::accumulate function from <numeric> header to sum up elements of a vector.

    #include <iostream> #include <vector> #include <numeric> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; int sum = std::accumulate(vec.begin(), vec.end(), 0); std::cout << "Sum of vector elements: " << sum << std::endl; return 0; } 

    Code Explanation: This code initializes a vector vec with integers and calculates the sum of its elements using std::accumulate, which starts with an initial value of 0.

  2. Range-based For Loop

    Description: Sum up vector elements using a range-based for loop.

    #include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; int sum = 0; for (int num : vec) { sum += num; } std::cout << "Sum of vector elements: " << sum << std::endl; return 0; } 

    Code Explanation: This code iterates through each element of the vector vec using a range-based for loop and accumulates the sum in the variable sum.

  3. Manual Accumulation

    Description: Manually sum up vector elements without using STL functions.

    #include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; int sum = 0; for (size_t i = 0; i < vec.size(); ++i) { sum += vec[i]; } std::cout << "Sum of vector elements: " << sum << std::endl; return 0; } 

    Code Explanation: This code demonstrates a basic loop to iterate through each element of the vector vec and calculate the sum manually.

  4. Lambda Function with Accumulate

    Description: Use a lambda function with std::accumulate for custom summing logic.

    #include <iostream> #include <vector> #include <numeric> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; int sum = std::accumulate(vec.begin(), vec.end(), 0, [](int a, int b) { return a + b; }); std::cout << "Sum of vector elements: " << sum << std::endl; return 0; } 

    Code Explanation: This example uses a lambda function as the binary operation in std::accumulate to sum up vector elements in a custom way.

  5. Using Standard Algorithms

    Description: Utilize other standard algorithms like std::for_each for accumulating vector elements.

    #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; int sum = 0; std::for_each(vec.begin(), vec.end(), [&sum](int num) { sum += num; }); std::cout << "Sum of vector elements: " << sum << std::endl; return 0; } 

    Code Explanation: This code demonstrates the use of std::for_each with a lambda function to accumulate the sum of vector elements.

  6. Parallel Accumulation with Execution Policies (C++17)

    Description: Use C++17's execution policies for parallel accumulation of vector elements.

    #include <iostream> #include <vector> #include <numeric> #include <execution> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; int sum = std::reduce(std::execution::par, vec.begin(), vec.end()); std::cout << "Sum of vector elements (parallel): " << sum << std::endl; return 0; } 

    Code Explanation: This code uses std::reduce with std::execution::par execution policy to perform parallel accumulation of vector elements.

  7. Accumulate with Initializer List

    Description: Sum up elements of a vector using an initializer list with std::accumulate.

    #include <iostream> #include <vector> #include <numeric> int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; int sum = std::accumulate({vec.begin(), vec.end()}, 0); std::cout << "Sum of vector elements (with initializer list): " << sum << std::endl; return 0; } 

    Code Explanation: This example uses an initializer list within std::accumulate to sum up elements of the vector vec.

  8. Accumulate with User-defined Struct

    Description: Sum up elements of a vector of custom structs using std::accumulate.

    #include <iostream> #include <vector> #include <numeric> struct Item { int value; }; int main() { std::vector<Item> items = {{1}, {2}, {3}, {4}, {5}}; int sum = std::accumulate(items.begin(), items.end(), 0, [](int a, const Item& b) { return a + b.value; }); std::cout << "Sum of vector elements (custom struct): " << sum << std::endl; return 0; } 

    Code Explanation: This code sums up the value member of a vector of custom Item structs using std::accumulate with a custom lambda function.

  9. Accumulate with Floating Point Numbers

    Description: Sum up elements of a vector containing floating point numbers.

    #include <iostream> #include <vector> #include <numeric> int main() { std::vector<double> vec = {1.1, 2.2, 3.3, 4.4, 5.5}; double sum = std::accumulate(vec.begin(), vec.end(), 0.0); std::cout << "Sum of vector elements (floating point): " << sum << std::endl; return 0; } 

    Code Explanation: This example calculates the sum of elements in a vector of double using std::accumulate.

  10. Accumulate with Custom Binary Operation

    Description: Use std::accumulate with a custom binary operation to sum up elements based on a specific condition.

    #include <iostream> #include <vector> #include <numeric> bool isOdd(int num) { return num % 2 != 0; } int main() { std::vector<int> vec = {1, 2, 3, 4, 5}; int sum = std::accumulate(vec.begin(), vec.end(), 0, [](int a, int b) { return a + (isOdd(b) ? b : 0); }); std::cout << "Sum of odd elements in vector: " << sum << std::endl; return 0; } 

    Code Explanation: This code demonstrates std::accumulate with a custom lambda function to sum up elements that satisfy a specific condition (isOdd function in this case).


More Tags

isnull java-platform-module-system spss adminer this random uniq microservices vue-test-utils jboss5.x

More Programming Questions

More Cat Calculators

More Statistics Calculators

More Chemical thermodynamics Calculators

More Auto Calculators