Changes

Jump to: navigation, search

Team Hortons

11 bytes added, 18:11, 29 October 2017
no edit summary
These algorithms provide a ''functional'' way to work on collections, and are present in several programming languages. These algorithms replace what usually would be a for/while loop, shifting the attention from "how to loop" to "what to do".
<precode><codepre>
#include <iostream>
#include <algorithm>
return 0;
}
</codepre></precode>
Output:
These executions are passed as the first parameter for the algorithm:
<precode><codepre>
std::copy(
std::execution::par,
b.start()
);
</codepre></precode>
Most compilers nowadays do not yet support this feature. Probably the only compiler that can already make use of these policies is the Intel C++ Compiler, which was used to perform the tests below.
Copying values from array ''a'' to array ''b''.
 <precode><codepre>
std::copy(
std::execution::par,
b
);
```</pre></code>
''Results''
''Snippet:''
<precode><codepre>
// Counting all multiples of 3
auto condition = [](mytype& i) { return i % 3 == 0; };
condition
);
</codepre></precode>
''Results:''
''Snippet:''
<precode><codepre>
size_t sum;
auto action = [&](mytype& i) { return sum += i; };
action
);
</codepre></precode>
Notice how this **for_each** behaves like a **reduce**: I am modifying the variable "sum" outside of this function. This is very likely to cause a **race condition**.
''Snippet:''
<precode><codepre>
size_t sum = std::reduce(
std::execution::par,
a.end()
);
</codepre></precode>
''Results:''
This algorithm is a bit different: we cannot use ''std::list'' with it. Since the algorithm requires random access, it is only possible to use arrays or vectors.
<precode><codepre>
auto sorter = [](mytype a, mytype b) { return a < b; };
sorter
);
</codepre></precode>
''Results:''
''Snippet:''
<precode><codepre>
auto action = [](mytype i) { return i += 5; };
action
);
</codepre></precode>
''Results:''

Navigation menu