Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 32 additions & 30 deletions docs/standard-library/algorithm-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ An output iterator addressing the position that is one past the final element in

The source range must be valid and there must be sufficient space at the destination to hold all the elements being copied.

The `copy_backward` algorithm imposes more stringent requirements than that the `copy` algorithm. Both its input and output iterators must be bidirectional.
The `copy_backward` algorithm imposes more stringent requirements than the `copy` algorithm. Both its input and output iterators must be bidirectional.

The `copy_backward` and [`move_backward`](algorithm-functions.md#move_backward) algorithms are the only C++ Standard Library algorithms designating the output range with an iterator pointing to the end of the destination range.

Expand Down Expand Up @@ -908,13 +908,13 @@ int main()

vector<int>::iterator::difference_type result;
result = count(v1.begin(), v1.end(), 10);
cout << "The number of 10s in v2 is: " << result << "." << endl;
cout << "The number of 10s in v1 is: " << result << "." << endl;
}
```

```Output
v1 = ( 10 20 10 40 10 )
The number of 10s in v2 is: 3.
The number of 10s in v1 is: 3.
```

## <a name="count_if"></a> `count_if`
Expand Down Expand Up @@ -2149,7 +2149,7 @@ public:
{
}

// The function call to process the next elment
// The function call to process the next element
void operator( ) ( int elem )
{
num++; // Increment the element count
Expand Down Expand Up @@ -2292,7 +2292,7 @@ public:
}
};

// Utility to display the contents of a vector
// Utility to display the contents of a vector
template <class T> void print_vector(const std::vector<T> &vec)
{
std::cout << "( ";
Expand Down Expand Up @@ -2686,7 +2686,7 @@ int main()
cout << *Iter3b << " ";
cout << ")." << endl;

// To test for inclusion under an asscending order
// To test for inclusion under an ascending order
// with the default binary predicate less<int>( )
bool Result1;
Result1 = includes ( v1a.begin( ), v1a.end( ),
Expand Down Expand Up @@ -2892,7 +2892,7 @@ int main()
// Applying a user defined (UD) binary predicate mod_lesser
inplace_merge ( v3.begin( ), break3, v3.end( ), mod_lesser );
cout << "Merged inplace with binary predicate mod_lesser specified,\n "
<< "vector v3mod = ( " ; ;
<< "vector v3mod = ( " ;
for ( Iter3 = v3.begin( ) ; Iter3 != v3.end( ) ; Iter3++ )
cout << *Iter3 << " ";
cout << ")" << endl;
Expand Down Expand Up @@ -4424,7 +4424,7 @@ int main() {
merge ( v3a.begin( ), v3a.end( ), v3b.begin( ), v3b.end( ),
v3.begin( ), mod_lesser );
cout << "Merged inplace with binary predicate mod_lesser specified,\n "
<< "vector v3mod = ( " ; ;
<< "vector v3mod = ( " ;
for ( Iter3 = v3.begin( ) ; Iter3 != v3.end( ) ; Iter3++ )
cout << *Iter3 << " ";
cout << ")." << endl;
Expand Down Expand Up @@ -4949,13 +4949,14 @@ Use the dual-range overloads in C++14 code because the overloads that only take

```cpp
template<class InputIterator1, class InputIterator2>
pair<InputIterator1, InputIterator2>>
pair<InputIterator1, InputIterator2>
mismatch(
InputIterator1 first1,
InputIterator1 last1,
InputIterator2 first2 );

template<class InputIterator1, class InputIterator2, class BinaryPredicate> pair<InputIterator1, InputIterator2>>
template<class InputIterator1, class InputIterator2, class BinaryPredicate>
pair<InputIterator1, InputIterator2>
mismatch(
InputIterator1 first1,
InputIterator1 last1,
Expand All @@ -4964,14 +4965,15 @@ mismatch(

//C++14
template<class InputIterator1, class InputIterator2>
pair<InputIterator1, InputIterator2>>
pair<InputIterator1, InputIterator2>
mismatch(
InputIterator1 first1,
InputIterator1 last1,
InputIterator2 first2,
InputIterator2 last2 );

template<class InputIterator1, class InputIterator2, class BinaryPredicate> pair<InputIterator1, InputIterator2>>
template<class InputIterator1, class InputIterator2, class BinaryPredicate>
pair<InputIterator1, InputIterator2>
mismatch(
InputIterator1 first1,
InputIterator1 last1,
Expand Down Expand Up @@ -5933,7 +5935,7 @@ int main()
cout << *Iter1 << " ";
cout << ")." << endl;

// Partition the range with predicate greater10
// Partition the range with predicate greater5
partition ( v1.begin( ), v1.end( ), greater5 );
cout << "The partitioned set of elements in v1 is: ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
Expand Down Expand Up @@ -6660,7 +6662,7 @@ Vector v2 is a copy of v1 with the value 7 removed:

## <a name="remove_copy_if"></a> `remove_copy_if`

Copies elements from a source range to a destination range, except for elements that satisfy a predicate. Elements are copied without disturbing the order of the remaining elements. Returns the end of a new destination range.
Copies elements from a source range to a destination range, except for elements that satisfy a predicate. Elements are copied without disturbing the order of the remaining elements. Returns the end of a new destination range.

```cpp
template<class InputIterator, class OutputIterator, class UnaryPredicate>
Expand Down Expand Up @@ -6694,7 +6696,7 @@ An input iterator addressing the position one past the final element in the rang
An output iterator addressing the position of the first element in the destination range to which elements are being removed.

*`pred`*\
The unary predicate that must be satisfied is the value of an element is to be replaced.
The unary predicate that must be satisfied if the value of an element is to be replaced.

### Return value

Expand Down Expand Up @@ -6751,7 +6753,7 @@ int main()
new_end = remove_copy_if ( v1.begin( ), v1.end( ),
v2.begin( ), greater6 );

cout << "After the appliation of remove_copy_if to v1,\n "
cout << "After the application of remove_copy_if to v1,\n "
<< "vector v1 is left unchanged as ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
Expand All @@ -6767,7 +6769,7 @@ int main()

```Output
The original vector v1 is: ( 4 7 7 7 0 5 7 1 6 9 3 7 8 2 ).
After the appliation of remove_copy_if to v1,
After the application of remove_copy_if to v1,
vector v1 is left unchanged as ( 4 7 7 7 0 5 7 1 6 9 3 7 8 2 ).
Vector v2 is a copy of v1 with values greater than 6 removed:
( 4 0 5 1 6 3 2 ).
Expand Down Expand Up @@ -6804,7 +6806,7 @@ A forward iterator pointing to the position of the first element in the range fr
A forward iterator pointing to the position one past the final element in the range from which elements are being removed.

*`pred`*\
The unary predicate that must be satisfied is the value of an element is to be replaced.
The unary predicate that must be satisfied if the value of an element is to be replaced.

### Return value

Expand Down Expand Up @@ -7148,7 +7150,7 @@ An input iterator pointing to the position one past the final element in the ran
An output iterator pointing to the position of the first element in the destination range to which elements are being copied.

*`pred`*\
The unary predicate that must be satisfied is the value of an element is to be replaced.
The unary predicate that must be satisfied if the value of an element is to be replaced.

*`value`*\
The new value being assigned to the elements whose old value satisfies the predicate.
Expand Down Expand Up @@ -7277,7 +7279,7 @@ A forward iterator pointing to the position of the first element in the range fr
An iterator pointing to the position one past the final element in the range from which elements are being replaced.

*`pred`*\
The unary predicate that must be satisfied is the value of an element is to be replaced.
The unary predicate that must be satisfied if the value of an element is to be replaced.

*`value`*\
The new value being assigned to the elements whose old value satisfies the predicate.
Expand Down Expand Up @@ -8269,7 +8271,7 @@ int main()
cout << *Iter3b << " ";
cout << ")." << endl;

// To combine into a difference in asscending
// To combine into a difference in ascending
// order with the default binary predicate less<int>( )
Result1 = set_difference ( v1a.begin( ), v1a.end( ),
v1b.begin( ), v1b.end( ), v1.begin( ) );
Expand All @@ -8294,7 +8296,7 @@ int main()
Result3 = set_difference ( v3a.begin( ), v3a.end( ),
v3b.begin( ), v3b.end( ), v3.begin( ), mod_lesser );
cout << "Set_difference of source ranges with binary "
<< "predicate mod_lesser specified,\n vector v3mod = ( " ; ;
<< "predicate mod_lesser specified,\n vector v3mod = ( " ;
for ( Iter3 = v3.begin( ) ; Iter3 != Result3 ; Iter3++ )
cout << *Iter3 << " ";
cout << ")." << endl;
Expand Down Expand Up @@ -8488,7 +8490,7 @@ int main()
cout << *Iter3b << " ";
cout << ")." << endl;

// To combine into an intersection in asscending order with the
// To combine into an intersection in ascending order with the
// default binary predicate less<int>( )
Result1 = set_intersection ( v1a.begin( ), v1a.end( ),
v1b.begin( ), v1b.end( ), v1.begin( ) );
Expand All @@ -8513,7 +8515,7 @@ int main()
Result3 = set_intersection ( v3a.begin( ), v3a.end( ),
v3b.begin( ), v3b.end( ), v3.begin( ), mod_lesser );
cout << "Intersection of source ranges with binary predicate "
<< "mod_lesser specified,\n vector v3mod = ( " ; ;
<< "mod_lesser specified,\n vector v3mod = ( " ;
for ( Iter3 = v3.begin( ) ; Iter3 != Result3 ; ++Iter3 )
cout << *Iter3 << " ";
cout << ")." << endl;
Expand Down Expand Up @@ -8736,7 +8738,7 @@ int main()
Result3 = set_symmetric_difference ( v3a.begin( ), v3a.end( ),
v3b.begin( ), v3b.end( ), v3.begin( ), mod_lesser );
cout << "Set_symmetric_difference of source ranges with binary "
<< "predicate mod_lesser specified,\n vector v3mod = ( " ; ;
<< "predicate mod_lesser specified,\n vector v3mod = ( " ;
for ( Iter3 = v3.begin( ) ; Iter3 != Result3 ; Iter3++ )
cout << *Iter3 << " ";
cout << ")." << endl;
Expand Down Expand Up @@ -8959,7 +8961,7 @@ int main()
Result3 = set_union ( v3a.begin( ), v3a.end( ),
v3b.begin( ), v3b.end( ), v3.begin( ), mod_lesser );
cout << "Union of source ranges with binary predicate "
<< "mod_lesser specified,\n vector v3mod = ( " ; ;
<< "mod_lesser specified,\n vector v3mod = ( " ;
for ( Iter3 = v3.begin( ) ; Iter3 != Result3 ; Iter3++ )
cout << *Iter3 << " ";
cout << ")." << endl;
Expand Down Expand Up @@ -9170,7 +9172,7 @@ Heaps have two properties:

- Elements may be added or removed in logarithmic time.

After the application if this algorithm, the range it was applied to is no longer a heap.
After the application of this algorithm, the range it was applied to is no longer a heap.

`sort_heap` isn't a stable sort because the relative order of equivalent elements isn't necessarily preserved.

Expand Down Expand Up @@ -9585,7 +9587,7 @@ A forward iterator pointing to one past the final position of the second range w

The ranges referenced must be valid; all pointers must be dereferenceable and within each sequence the last position is reachable from the first by incrementation. The second range has to be as large as the first range.

The complexity is linear with *last1* - *first1* swaps performed. If elements from containers of the same type are being swapped, them the `swap` member function from that container should be used, because the member function typically has constant complexity.
The complexity is linear with *last1* - *first1* swaps performed. If elements from containers of the same type are being swapped, then the `swap` member function from that container should be used, because the member function typically has constant complexity.

### Example

Expand Down Expand Up @@ -9922,7 +9924,7 @@ int main()
cout << *v1_Iter1 << " ";
cout << ")." << endl;

// Remove consecutive duplicates under the binary prediate mod_equals
// Remove consecutive duplicates under the binary predicate mod_equals
v1_NewEnd2 = unique ( v1.begin( ), v1_NewEnd1 , mod_equal );

cout << "Removing adjacent duplicates from vector v1 under the\n "
Expand Down Expand Up @@ -10078,7 +10080,7 @@ int main() {
for ( iv = 0 ; iv <= 7 ; iv++ )
v1.push_back( 10 );

// Remove consecutive duplicates under the binary prediate mod_equals
// Remove consecutive duplicates under the binary predicate mod_equals
v1_NewEnd2 = unique_copy ( v1.begin( ), v1.begin( ) + 14,
v1.begin( ) + 14 , mod_equal );

Expand Down