Skip to content

Commit 78e0977

Browse files
TylerMSFTTylerMSFT
authored andcommitted
fix another function
1 parent 55bb3b9 commit 78e0977

1 file changed

Lines changed: 45 additions & 38 deletions

File tree

‎docs/standard-library/algorithm-functions.md‎

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7034,7 +7034,7 @@ The order of the elements not replaced remains stable.
70347034

70357035
The `operator==` used to determine the equality between elements must impose an equivalence relation between its operands.
70367036

7037-
The complexity is linear. It makes (`last` - `first`) comparisons for equality and at most (`last` - `first`) assignments of new values.
7037+
The complexity is linear. It makes (`last - first`) comparisons for equality and at most (`last - first`) assignments of new values.
70387038

70397039
### Example
70407040

@@ -7049,58 +7049,65 @@ The complexity is linear. It makes (`last` - `first`) comparisons for equality a
70497049
int main()
70507050
{
70517051
using namespace std;
7052-
vector<int> v1;
7053-
list<int> L1 (15);
7054-
vector<int>::iterator Iter1;
7055-
list<int>::iterator L_Iter1;
7052+
vector<int> theVector;
7053+
list<int> theList(15);
70567054

7057-
int i;
7058-
for ( i = 0 ; i <= 9 ; i++ )
7059-
v1.push_back( i );
7060-
7061-
int ii;
7062-
for ( ii = 0 ; ii <= 3 ; ii++ )
7063-
v1.push_back( 7 );
7055+
for (int i = 0; i <= 9; i++)
7056+
{
7057+
theVector.push_back(i);
7058+
}
70647059

7065-
random_shuffle ( v1.begin( ), v1.end( ) );
7060+
for (int i = 0; i <= 3; i++)
7061+
{
7062+
theVector.push_back(7);
7063+
}
70667064

7067-
int iii;
7068-
for ( iii = 0 ; iii <= 15 ; iii++ )
7069-
v1.push_back( 1 );
7065+
random_shuffle(theVector.begin(), theVector.end());
7066+
7067+
for (int i = 0; i <= 15; i++)
7068+
{
7069+
theVector.push_back(1);
7070+
}
70707071

7071-
cout << "The original vector v1 is:\n ( " ;
7072-
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
7073-
cout << *Iter1 << " ";
7072+
cout << "The shuffled vector:\n ( ";
7073+
for (auto iter = theVector.begin(); iter != theVector.end(); iter++)
7074+
{
7075+
cout << *iter << " ";
7076+
}
70747077
cout << ")." << endl;
70757078

7076-
// Replace elements in one part of a vector with a value of 7
7077-
// with a value of 70 and copy into another part of the vector
7078-
replace_copy ( v1.begin( ), v1.begin( ) + 14,v1.end( ) -15, 7 , 70);
7079+
// Replace the 7s in part of the vector with 70s
7080+
// and copy into another part of the vector
7081+
replace_copy(theVector.begin(), theVector.begin() + 14, theVector.end() - 15, 7, 70);
70797082

7080-
cout << "The vector v1 with a value 70 replacing that of 7 is:\n ( " ;
7081-
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
7082-
cout << *Iter1 << " ";
7083+
cout << "The vector with instances of 7 replaced with 70 starting at position 14:\n ( ";
7084+
for (auto iter = theVector.begin(); iter != theVector.end(); iter++)
7085+
{
7086+
cout << *iter << " ";
7087+
}
70837088
cout << ")." << endl;
70847089

7085-
// Replace elements in a vector with a value of 70
7086-
// with a value of 1 and copy into a list
7087-
replace_copy ( v1.begin( ), v1.begin( ) + 14,L1.begin( ), 7 , 1);
7090+
// Replace 7s found in the first 14 positions in the vector with 1s and then copy the result into a list
7091+
replace_copy(theVector.begin(), theVector.begin() + 14, theList.begin(), 7, 1);
70887092

7089-
cout << "The list copy L1 of v1 with the value 0 replacing "
7090-
<< "that of 7 is:\n ( " ;
7091-
for ( L_Iter1 = L1.begin( ) ; L_Iter1 != L1.end( ) ; L_Iter1++ )
7092-
cout << *L_Iter1 << " ";
7093+
cout << "List containing the contents of the vector but 7s replaced with 1s.\n ( ";
7094+
for (auto iter = theList.begin(); iter != theList.end(); iter++)
7095+
{
7096+
cout << *iter << " ";
7097+
}
70937098
cout << ")." << endl;
70947099
}
70957100
```
70967101

7102+
Because of the `random_shuffle()` call in the preceding code, your output may be different.
7103+
70977104
```Output
7098-
The original vector v1 is:
7099-
( 4 7 7 7 0 5 7 1 6 9 3 7 8 2 ).
7100-
The vector v1 with a value 70 replacing that of 7 is:
7101-
( 4 70 70 70 0 5 70 1 6 9 3 70 8 2 ).
7102-
The list copy L1 of v1 with the value 0 replacing that of 7 is:
7103-
( 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ).
7105+
The shuffled vector:
7106+
( 7 1 9 2 0 7 7 3 4 6 8 5 7 7 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ).
7107+
The vector with instances of 7 replaced with 70 starting at position 14:
7108+
( 7 1 9 2 0 7 7 3 4 6 8 5 7 7 1 70 1 9 2 0 70 70 3 4 6 8 5 70 70 1 ).
7109+
List containing the contents of the vector but 7s replaced with 1s.
7110+
( 1 1 9 2 0 1 1 3 4 6 8 5 1 1 0 ).
71047111
```
71057112

71067113
## <a name="replace_copy_if"></a> `replace_copy_if`

0 commit comments

Comments
 (0)