Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 141c6b9

Browse files
authored
Merge pull request #150 from bernhardmgruber/shuffle
Use shuffle and reduce
2 parents 18d5852 + 5487694 commit 141c6b9

File tree

7 files changed

+17
-13
lines changed

7 files changed

+17
-13
lines changed

‎code/smartPointers/CMakeLists.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
cmake_minimum_required( VERSION 3.1 )
44
project( smartPointers LANGUAGES CXX )
55

6+
set(CMAKE_CXX_STANDARD 17)
7+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
8+
69
# Set up the compilation environment.
710
include( "${CMAKE_CURRENT_SOURCE_DIR}/../CompilerSettings.cmake" )
811
include( "${CMAKE_CURRENT_SOURCE_DIR}/../SolutionTarget.cmake" )

‎code/smartPointers/smartPointers.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ double sumEntries(const double* data, std::size_t size) {
3434
if (size > 200)
3535
throw std::invalid_argument("I only want to sum 200 numbers or less.");
3636

37-
return std::accumulate(data, data + size, 0);
37+
return std::reduce(data, data + size, 0);
3838
}
3939

4040
// Often, data are owned by one entity, and only used by others. Fix the leak.

‎code/smartPointers/solution/smartPointers.sol.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ double sumEntries(const double* data, std::size_t size) {
3535
if (size > 200)
3636
throw std::invalid_argument("I only want to sum 200 numbers or less.");
3737

38-
return std::accumulate(data, data + size, 0.);
38+
return std::reduce(data, data + size, 0.);
3939
}
4040

4141
// Often, data are owned by one entity, and only used by others. Fix the leak.

‎code/stl/CMakeLists.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
cmake_minimum_required( VERSION 3.1 )
44
project( stl LANGUAGES CXX )
55

6+
set(CMAKE_CXX_STANDARD 17)
7+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
8+
69
# Set up the compilation environment.
710
include( "${CMAKE_CURRENT_SOURCE_DIR}/../CompilerSettings.cmake" )
811
include( "${CMAKE_CURRENT_SOURCE_DIR}/../SolutionTarget.cmake" )

‎code/stl/Makefile‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ clean:
55
rm -f *o randomize *~ randomize.sol randomize.nostl
66

77
randomize : randomize.cpp
8-
${CXX} -g -O0 -Wall -Wextra -L. -o $@ $<
8+
${CXX} -std=c++17 -g -O0 -Wall -Wextra -L. -o $@ $<
99

1010
randomize.nostl : randomize.nostl.cpp
11-
${CXX} -g -O0 -Wall -Wextra -L. -o $@ $<
11+
${CXX} -std=c++17 -g -O0 -Wall -Wextra -L. -o $@ $<
1212

1313
randomize.sol : solution/randomize.sol.cpp
14-
${CXX} -g -O0 -Wall -Wextra -I. -L. -o $@ $<
14+
${CXX} -std=c++17 -g -O0 -Wall -Wextra -I. -L. -o $@ $<

‎code/stl/randomize.cpp‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#include <iostream>
2-
#include <math.h>
32
#include <algorithm>
43
#include <vector>
54
#include <numeric>
5+
#include <random>
66
#include "Complex.hpp"
77

88
using namespace std;
9-
using namespace __gnu_cxx;
109

1110
template<typename T>
1211
struct Generator {
@@ -20,7 +19,7 @@ void compute(int len, T initial, T step) {
2019

2120
// fill and randomize v
2221
generate(, , Generator...);
23-
random_shuffle(...);
22+
shuffle(..., std::default_random_engine{});
2423

2524
// compute differences
2625
adjacent_difference(...);

‎code/stl/solution/randomize.sol.cpp‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#include <iostream>
2-
#include <math.h>
32
#include <algorithm>
43
#include <vector>
54
#include <numeric>
5+
#include <random>
66
#include "Complex.hpp"
77

88
using namespace std;
9-
using namespace __gnu_cxx;
109

1110
template<typename T>
1211
struct Generator {
@@ -31,14 +30,14 @@ void compute(int len, T initial, T step) {
3130

3231
// fill and randomize v
3332
generate(v.begin(), v.end(), Generator<T>(initial, step));
34-
random_shuffle(v.begin(), v.end());
33+
shuffle(v.begin(), v.end(), std::default_random_engine{});
3534

3635
// compute differences
3736
adjacent_difference(v.begin(), v.end(), diffs.begin());
3837

3938
// compute standard deviation of it
40-
T sum = accumulate(diffs.begin()+1, diffs.end(), T());
41-
T sumsq = accumulate(diffs.begin()+1, diffs.end(), T(), sumsquare<T>());
39+
T sum = reduce(diffs.begin()+1, diffs.end(), T());
40+
T sumsq = reduce(diffs.begin()+1, diffs.end(), T(), sumsquare<T>());
4241
T mean = sum/len;
4342
T variance = sumsq/len - mean*mean;
4443

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /