mirror of
https://github.com/VictorEijkhout/TheArtOfHPC_vol3_cppf08programming.git
synced 2026-01-24 22:44:48 +09:00
51 lines
1.2 KiB
C++
51 lines
1.2 KiB
C++
/****************************************************************
|
|
****
|
|
**** This file belongs with the course
|
|
**** Introduction to Scientific Programming in C++/Fortran2003
|
|
**** copyright 2016-2023 Victor Eijkhout eijkhout@tacc.utexas.edu
|
|
****
|
|
**** sumsquare.cxx : C++20 ranges for sum of squares.
|
|
****
|
|
****************************************************************/
|
|
|
|
#include <iostream>
|
|
using std::cout;
|
|
#include <vector>
|
|
using std::vector;
|
|
|
|
//#include <ranges>
|
|
// we need accumulate
|
|
#include <range/v3/all.hpp>
|
|
#ifdef RANGES_V3_ALL_HPP
|
|
namespace rng = ranges;
|
|
#else
|
|
namespace rng = std::ranges;
|
|
#endif
|
|
|
|
#include <algorithm>
|
|
#include <numeric> // for accumulate
|
|
|
|
int main()
|
|
{
|
|
{
|
|
//codesnippet sumelem
|
|
vector<float> elements{.5f,1.f,1.5f};
|
|
auto sum_of_elts =
|
|
rng::accumulate( elements, 0.f );
|
|
cout << "Sum of elements: "
|
|
<< sum_of_elts << '\n';
|
|
//codesnippet end
|
|
}
|
|
{
|
|
//codesnippet sumsquaretransform
|
|
vector<float> elements{.5f,1.f,1.5f};
|
|
auto squares =
|
|
rng::views::transform(elements, [] (auto e) { return e*e; } );
|
|
auto sumsq =
|
|
rng::accumulate( squares, 0.f );
|
|
cout << "Sum of squares: " << sumsq << '\n';
|
|
//codesnippet end
|
|
}
|
|
return 0;
|
|
}
|