C++ STL Notes
Sum all numbers in a container:
#include <iostream>
#include <vector>
#include <numeric> // for std::accumulate
int main() {
std::vector<float> numbers = {1, 2, 3, 4, 5};
// Calculate the sum of elements using std::accumulate
float sum = std::accumulate(numbers.begin(), numbers.end(), 0.0); //0.0 is initial value
std::cout << "Sum of elements: " << sum << std::endl;
return 0;
}
Example of Using inbuilt sort function
int a[4] = {5,9,8,1};
cout<<sizeof(a)/sizeof(int)<<'\n'; //getting size of a
sort(a,a+(sizeof(a)/sizeof(int))); //
for (int var = 0; var < 4; ++var) {
cout<<a[var]<<",";
}
cout<<"\n";
vector<int> av={55,3,4,2,87};
sort(av.begin(), av.end());
for(auto v: av)
cout<<v<<",";