v.begin(), v.end(), auto and accumulate() function in C++ STL.
#Accumulate function in C++ STL#
std::accumulate is a function in the C++ STL (Standard Template Library) that allows you to calculate the sum (or product) of a range of elements in a container. The function is declared in the <numeric> header.
Syntax: accumulate (InputIterator first, InputIterator last, init);
The accumulate function takes an input iterator first and an input iterator last, which define the range of elements to be summed, and an initial value init.
Here's an example usage of the std::accumulate function to calculate the sum of elements in a vector.
#include <numeric>
#include <vector>
#include <iostream>
int main() {
std::vector<int> vec{1, 2, 3, 4, 5};
int sum = std::accumulate(vec.begin(), vec.end(), 0);
std::cout << "The sum is " << sum << std::endl;
return 0;
}
OUTPUT: The sum is 15
In this example, vec.begin() returns an iterator to the first element of the std::vector<int>, vec.end() returns an iterator to one past the last element, and 0 is the initial value of the sum. The function calculates the sum of the elements between the iterators vec.begin() and vec.end() and returns the final sum value which is then printed to the console.
If you replace the initial value 0 with 2 in the std::accumulate function call, like this:
int sum = std::accumulate(vec.begin(), vec.end(), 2);
The output will be 17.
This is because the std::accumulate function starts with an initial value of 2, and then adds all the elements in the range [vec.begin(), vec.end()] to this initial value.
So, the calculation becomes: 2 + 1 + 2 + 3 + 4 + 5 = 17
v.begin() and v.end()
v.begin() and v.end() are member functions of a container class v in C++, which return iterators that respectively point to the beginning and end of the elements stored in the container v.
For example, if v is a std::vector<int> containing the elements {1, 2, 3, 4, 5}, v.begin() will return an iterator pointing to the first element 1 and v.end() will return an iterator pointing one past the last element 6.
These iterators can be used to iterate through the elements of the container, such as with a for loop:
std::vector<int> v = {1, 2, 3, 4, 5};
for (auto it = v.begin(); it != v.end(); ++it) {
std::cout << *it << " ";
}
This will output: 1 2 3 4 5.
Note that v.end() does not point to the last element of the container, but rather to one past the last element. This is because the iterator range is defined as [begin, end), meaning that the element pointed to by end is excluded from the range.
NOTE: one past the last element means the after the last element.
ex: v={1,2,3,4,5} first element is '1' , last element is '5' and one past the last element is '6'
'auto' keyword:
1) auto with an expression
python code: auto sum = 2.5 + 3; // deduced type is double
In this example, the auto keyword is used to declare a variable sum and initialize it with the sum of 2.5 (which is a double value) and 3 (which is an int value). The compiler automatically deduces that the type of sum is double, because that's the result of the expression.
2) auto with a string literal: auto str = "Hello, world!"; // deduced type is const char*
In this example, the auto keyword is used to declare a variable str and initialize it with the string literal "Hello, world!". The compiler automatically deduces that the type of str is const char*, because that's the type of a string literal in C++.
In this example, the auto keyword is used to declare a variable result and initialize it with the return value of the function my_function(). The compiler automatically deduces the type of result based on the return type of my_function(). If my_function() returns an int, for example, then the type of result will be int. If my_function() returns a std::string, then the type of result will be std::string. And so on.
Comments
Post a Comment