Skip to content

Commit abefe71

Browse files
author
Colin Robertson
committed
Add contributed examples and output
1 parent a86d220 commit abefe71

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

docs/standard-library/algorithm-functions.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,14 @@ int main()
409409
}
410410
```
411411

412+
```Output
413+
List1 = ( 5 10 20 25 30 50 )
414+
There is an element in list List1 with a value equal to 10.
415+
There is an element in list List1 with a value greater than 10 under greater than.
416+
Ordered using mod_lesser, vector v1 = ( 0 -1 1 -2 2 3 4 )
417+
There is an element with a value equivalent to -3 under mod_lesser.
418+
```
419+
412420
## <a name="clamp"></a> clamp
413421

414422
Compares a value to an upper and lower bound, and returns a reference to the value if it is between the bounds, or a reference to the upper or lower bound if the value is above or below them, respectively.
@@ -643,6 +651,13 @@ int main() {
643651
}
644652
```
645653

654+
```Output
655+
v1 = ( 0 10 20 30 40 50 )
656+
v2 = ( 0 3 6 9 12 15 18 21 24 27 30 )
657+
v2 with v1 insert = ( 0 3 6 9 0 10 20 21 24 27 30 )
658+
v2 with shifted insert = ( 0 3 6 9 0 10 0 10 20 27 30 )
659+
```
660+
646661
## <a name="copy_if"></a> copy_if
647662

648663
In a range of elements, copies the elements that are **true** for the specified condition.
@@ -693,6 +708,61 @@ The template function evaluates
693708

694709
once for each `N` in the range `[0, last - first)`, for strictly increasing values of `N` starting with the lowest value. If *dest* and *first* designate regions of storage, *dest* must not be in the range `[ first, last )`.
695710

711+
### Example
712+
713+
```cpp
714+
// alg_copy_if.cpp
715+
// compile with: /EHsc
716+
#include <list>
717+
#include <algorithm>
718+
#include <iostream>
719+
720+
void listlist(std::list<int> l)
721+
{
722+
std::cout << "( ";
723+
for (auto const& el : l)
724+
std::cout << el << " ";
725+
std::cout << ")" << std::endl;
726+
}
727+
728+
int main()
729+
{
730+
using namespace std;
731+
list<int> li{ 46, 59, 88, 72, 79, 71, 60, 5, 40, 84 };
732+
list<int> le(li.size()); // le = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
733+
list<int> lo(li.size()); // lo = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
734+
735+
cout << "li = ";
736+
listlist(li);
737+
738+
// is_even checks if the element is even.
739+
auto is_even = [](int const elem) { return !(elem % 2); };
740+
// use copy_if to select only even elements from li
741+
// and copy them to le, starting from le's begin position
742+
auto ec = copy_if(li.begin(),li.end(), le.begin(), is_even);
743+
le.resize(std::distance(le.begin(), ec)); // shrink le to new size
744+
745+
cout << "Even numbers are le = ";
746+
listlist(le);
747+
748+
// is_odd checks if the element is odd.
749+
auto is_odd = [](int const elem) { return (elem % 2); };
750+
// use copy_if to select only odd elements from li
751+
// and copy them to lo, starting from lo's begin position
752+
auto oc = copy_if(li.begin(), li.end(), lo.begin(), is_odd);
753+
lo.resize(std::distance(lo.begin(), oc)); // shrink lo to new size
754+
755+
cout << "Odd numbers are lo = ";
756+
listlist(lo);
757+
}
758+
```
759+
760+
```Output
761+
li = ( 46 59 88 72 79 71 60 5 40 84 )
762+
Even numbers are le = ( 46 88 72 60 40 84 )
763+
Odd numbers are lo = ( 59 79 71 5 )
764+
```
765+
696766
## <a name="copy_n"></a> copy_n
697767

698768
Copies a specified number of elements.

0 commit comments

Comments
 (0)