Modifying sequence operations: (修改容器操作)
copy
Copy range of elements (function template )
copy_n
Copy elements (function template )
copy_if
Copy certain elements of range (function template )
copy_backward
Copy range of elements backward (function template )
move
Move range of elements (function template )
move_backward
Move range of elements backward (function template )
swap
Exchange values of two objects (function template )
swap_ranges
Exchange values of two ranges (function template )
iter_swap
Exchange values of objects pointed to by two iterators
(function template )
transform
Transform range (function template )
replace
Replace value in range (function template )
replace_if
Replace values in range (function template )
replace_copy
Copy range replacing value (function template )
replace_copy_if
Copy range replacing value (function template )
fill
Fill range with value (function template )
fill_n
Fill sequence with value (function template )
generate
Generate values for range with function (function
template )
generate_n
Generate values for sequence with function (function
template )
remove
Remove value from range (function template )
remove_if
Remove elements from range (function template )
remove_copy
Copy range removing value (function template )
remove_copy_if
Copy range removing values (function template )
unique
Remove consecutive duplicates in range (function template
)
unique_copy
Copy range removing duplicates (function template )
reverse
Reverse range (function template )
reverse_copy
Copy range reversed (function template )
rotate
Rotate left the elements in range (function template )
rotate_copy
Copy range rotated left (function template )
random_shuffle
Randomly rearrange elements in range (function template )
shuffle
Randomly rearrange elements in range using generator
(function template )
观察本质
template <class InputIterator, class OutputIterator,
class UnaryPredicate>
OutputIterator copy_if (InputIterator first,
InputIterator last,
OutputIterator result, UnaryPredicate pred)
{
while (first!=last) {
if (pred(*first)) {
*result = *first;
++result;
}
++first;
}
return result;
}
-----------------------------------------------------------------------------------
??// copy_if example #include <iostream> // std::cout #include <algorithm> // std::copy_if, std::distance #include <vector> // std::vector
int main () {
std::vector<int> foo = {25,15,5,-5,-15};
std::vector<int> bar (foo.size());
// copy only positive numbers:
auto it = std::copy_if (foo.begin(), foo.end(),
bar.begin(), [](int i){return !(i<0);} );
bar.resize(std::distance(bar.begin(),it)); // shrink
container to new size
解释:
???核心代码 while (++first != last)
{ if (!(*result == *first)) // or: if
(!pred(*result,*first)) for version (2)
*(++result)=*first;
}
相邻两个值不相等的时候,返回++first
----------------------------------------vs2015--------------------------------------------------
// TEMPLATE FUNCTION unique
template<class _FwdIt> inline
_FwdIt unique(_FwdIt _First, _FwdIt _Last)
{ // remove each matching previous
return (_STD unique(_First, _Last, equal_to<>()));
}
?
// TEMPLATE FUNCTION unique WITH PRED
template<class _FwdIt, class _Pr> inline
_FwdIt _Unique_unchecked(_FwdIt _First, _FwdIt _Last,
_Pr& _Pred)
{ // remove each satisfying _Pred with previous
if (_First != _Last)
for (_FwdIt _Firstb; (void)(_Firstb = _First), ++_First
!= _Last; )
if (_Pred(*_Firstb, *_First))
{ // copy down
for (; ++_First != _Last; )
if (!_Pred(*_Firstb, *_First))
*++_Firstb = _STD move(*_First);
return (++_Firstb);
}
return (_Last);
}???????????????????
unique_copy
Copy range removing duplicates (复制范围删除重复
)
观察本质
template <class InputIterator, class
OutputIterator>
OutputIterator unique_copy (InputIterator first,
InputIterator last,
OutputIterator result)
{
if (first==last) return result;
*result = *first;
while (++first != last) {
typename
iterator_traits<InputIterator>::value_type val = *first;
if (!(*result == val)) // or: if (!pred(*result,val))
for version (2)
*(++result)=val;
}
return ++result;
}
-------------------------------------------------------------------------
// unique_copy example #include <iostream> // std::cout #include <algorithm> // std::unique_copy, std::sort,
std::distance #include <vector> // std::vector
bool myfunction (int i, int j) {
return (i==j);
}
int main () {
int myints[] = {10,20,20,20,30,30,20,20,10};
std::vector<int> myvector (9); // 0 0 0 0 0 0 0 0
0