Modifying sequence operations:

std::copy

<algorithm>
template <class InputIterator, class OutputIterator>
  OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result );
Copy range of elements
Copies the elements in the range  [first,last)  into a range beginning at  result .

Returns an iterator to the end of the destination range (which points to the element following the copy of  last ).

The behavior of this function template is equivalent to:
1
2
3
4
5
6
template<class InputIterator, class OutputIterator>
  OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result )
{
  while (first!=last) *result++ = *first++;
  return result;
}

std::copy_backward

<algorithm>
template <class BidirectionalIterator1, class BidirectionalIterator2>
  BidirectionalIterator2 copy_backward ( BidirectionalIterator1 first,
                                         BidirectionalIterator1 last,
                                         BidirectionalIterator2 result );
Copy range of elements backwards
Copies the elements in the range  [first,last)  into a range whose end element is  result . The function begins by copying  *(last-1)  into  *(result-1) , and then follows backwards by the elements preceeding these, until  first  is reached (and including it).

Returns an iterator to the first element in the destination range.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
template<class BidirectionalIterator1, class BidirectionalIterator2>
  BidirectionalIterator2 copy_backward ( BidirectionalIterator1 first,
                                         BidirectionalIterator1 last,
                                         BidirectionalIterator2 result )
{
  while (last!=first) *(--result) = *(--last);
  return result;
}

std::swap

<algorithm>
template <class T> void swap ( T& a, T& b );
Exchange values of two objects
Assigns the content of  a  to  b  and the content of  b  to  a .

The behavior of this function template is equivalent to:
1
2
3
4
template <class T> void swap ( T& a, T& b )
{
  T c(a); a=b; b=c;
}

std::swap_ranges

<algorithm>
template < class ForwardIterator1, class ForwardIterator2 >
  ForwardIterator2 swap_ranges ( ForwardIterator1 first1, ForwardIterator1 last1,
                                 ForwardIterator2 first2 );
Exchange values of two ranges
Swaps the values of each of the elements in the range  [first1,last1)  with those of their respective elements in the range beginning at  first2 .

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
template<class ForwardIterator1, class ForwardIterator2>
  ForwardIterator2 swap_ranges ( ForwardIterator1 first1, ForwardIterator1 last1,
                                 ForwardIterator2 first2 )
{
  while (first1!=last1) swap(*first1++, *first2++);
  return first2;
}

std::iter_swap

<algorithm>
template <class ForwardIterator1, class ForwardIterator2>
  void iter_swap ( ForwardIterator1 a, ForwardIterator2 b );
Exchange values of objects pointed by two iterators
Assigns the content of  *a  to  *b  and the content of  *b  to  *a .

The behavior of this function template is equivalent to:
1
2
3
4
5
template <class ForwardIterator1, class ForwardIterator2>
  void iter_swap ( ForwardIterator1 a, ForwardIterator2 b )
{
  swap (*a, *b);
}

std::transform

<algorithm>
template < class InputIterator, class OutputIterator, class UnaryOperator >
  OutputIterator transform ( InputIterator first1, InputIterator last1,
                             OutputIterator result, UnaryOperator op );

template < class InputIterator1, class InputIterator2,
           class OutputIterator, class BinaryOperator >
  OutputIterator transform ( InputIterator1 first1, InputIterator1 last1,
                             InputIterator2 first2, OutputIterator result,
                             BinaryOperator binary_op );
Apply function to range
The first version applies  op  to all the elements in the input range ( [first1,last1) ) and stores each returned value in the range beginning at  result .

The second version uses as argument for each call to  binary_op  one element from the first input range ( [first1,last1) ) and one element from the second input range (beginning at  first2 ).

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
template < class InputIterator, class OutputIterator, class UnaryOperator >
  OutputIterator transform ( InputIterator first1, InputIterator last1,
                             OutputIterator result, UnaryOperator op )
{
  while (first1 != last1)
    *result++ = op(*first1++);  // or: *result++=binary_op(*first1++,*first2++);
  return result;
}

std::replace

<algorithm>
template < class ForwardIterator, class T >
  void replace ( ForwardIterator first, ForwardIterator last,
                 const T& old_value, const T& new_value );
Replace value in range
Sets all the elements in the range  [first,last)  whose current value equals  old_value  to a value of  new_value .

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
template < class ForwardIterator, class T >
  void replace ( ForwardIterator first, ForwardIterator last,
                 const T& old_value, const T& new_value )
{
  for (; first != last; ++first)
    if (*first == old_value) *first=new_value;
}

std::replace_if

<algorithm>
template < class ForwardIterator, class Predicate, class T >
  void replace_if ( ForwardIterator first, ForwardIterator last,
                    Predicate pred, const T& new_value );
Replace values in range
Sets all those elements in the range  [first,last)  for which  pred  returns  true  when applied to its value, to a value of new_value .

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
template < class ForwardIterator, class Predicate, class T >
  void replace_if ( ForwardIterator first, ForwardIterator last,
                    Predicate pred, const T& new_value )
{
  for (; first != last; ++first)
    if (pred(*first)) *first=new_value;
}

std::replace_copy

<algorithm>
template < class InputIterator, class OutputIterator, class T >
  OutputIterator replace_copy ( InputIterator first, InputIterator last,
                                OutputIterator result,
                                const T& old_value, const T& new_value );
Copy range replacing value
Copies the values of the elements in the range  [first,last)  to the range positions beginning at  result , replacing the appearances of  old_value  by  new_value .

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
template < class InputIterator, class OutputIterator, class T >
  OutputIterator replace_copy ( InputIterator first, InputIterator last,
                                OutputIterator result, const T& old_value, const T& new_value )
{
  for (; first != last; ++first, ++result)
    *result = (*first==old_value)? new_value: *first;
  return result;
}

std::replace_copy_if

<algorithm>
template < class InputIterator, class OutputIterator, class Predicate, class T >
  OutputIterator replace_copy_if ( InputIterator first, InputIterator last,
                                   OutputIterator result, Predicate pred,
                                   const T& new_value );
Copy range replacing value
Copies the values of the elements in the range  [first,last)  to the range positions beginning at location pointed by result , replacing those where  pred  is true by  new_value .

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
template < class InputIterator, class OutputIterator, class Predicate, class T >
  OutputIterator replace_copy_if ( InputIterator first, InputIterator last,
                                   OutputIterator result, Predicate pred,
                                   const T& new_value )
{
  for (; first != last; ++first, ++result)
    *result = (pred(*first))? new_value: *first;
  return result;
}

std::fill

<algorithm>
template < class ForwardIterator, class T >
  void fill ( ForwardIterator first, ForwardIterator last, const T& value );
Fill range with value
Sets  value  to all elements in the range  [first,last) .

The behavior of this function template is equivalent to:
1
2
3
4
5
template < class ForwardIterator, class T >
  void fill ( ForwardIterator first, ForwardIterator last, const T& value )
{
  while (first != last)  *first++ = value;
}

std::fill_n

<algorithm>
template < class OutputIterator, class Size, class T >
  void fill_n ( OutputIterator first, Size n, const T& value );
Fill sequence with value
Sets  value  to the first  n  elements in the sequence pointed by  first .

The behavior of this function template is equivalent to:
1
2
3
4
5
template < class OutputIterator, class Size, class T >
  void fill_n ( OutputIterator first, Size n, const T& value )
{
  for (; n>0; --n)  *first++ = value;
}
function template

std::generate

<algorithm>
template <class ForwardIterator, class Generator>
  void generate ( ForwardIterator first, ForwardIterator last, Generator gen );
Generate values for range with function
Sets the value of the elements in the range  [first,last)  to the value returned by successive calls to  gen .

The behavior of this function template is equivalent to:
1
2
3
4
5
template <class ForwardIterator, class Generator>
  void generate ( ForwardIterator first, ForwardIterator last, Generator gen )
{
  while (first != last)  *first++ = gen();
}

std::generate_n

<algorithm>
template <class OutputIterator, class Size, class Generator>
  void generate_n ( OutputIterator first, Size n, Generator gen );
Generate values for sequence with function
Sets the value of the first  n  elements in the sequence pointed by  first  to the value returned by successive calls to gen .

The behavior of this function template is equivalent to:
1
2
3
4
5
template <class OutputIterator, class Size, class Generator>
  void generate_n ( OutputIterator first, Size n, Generator gen )
{
  for (; n>0; --n)  *first++ = gen();
}

std::remove

<algorithm>
template < class ForwardIterator, class T >
  ForwardIterator remove ( ForwardIterator first, ForwardIterator last,
                           const T& value );
Remove value from range
Compares the elements in the range  [first,last)  against  value , and removes those that compare equal from the resulting range. The resulting range consists of the elements between  first  and the iterator returned by the function, which points to the new end of the range.

The relative order of the elements not removed is preserved, while the elements past the new end of range are still valid, although with unspecified values.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
template < class ForwardIterator, class T >
  ForwardIterator remove ( ForwardIterator first, ForwardIterator last, const T& value )
{
  ForwardIterator result = first;
  for ( ; first != last; ++first)
    if (!(*first == value)) *result++ = *first;
  return result;
}

std::remove_if

<algorithm>
template < class ForwardIterator, class Predicate >
  ForwardIterator remove_if ( ForwardIterator first, ForwardIterator last,
                              Predicate pred );
Remove elements from range
Applies  pred  to the elements in the range  [first,last) , and removes those for which it does not return  false  from the resulting range. The resulting range consists of the elements between  first  and the iterator returned by the function, which points to the new end of the range.

The relative order of the elements not removed is preserved, while the elements past the new end of range are still valid, although with unspecified values.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
template < class ForwardIterator, class Predicate >
  ForwardIterator remove_if ( ForwardIterator first, ForwardIterator last,
                              Predicate pred )
{
  ForwardIterator result = first;
  for ( ; first != last; ++first)
    if (!pred(*first)) *result++ = *first;
  return result;
}

std::remove_copy

<algorithm>
template <class InputIterator, class OutputIterator, class T>
  OutputIterator remove_copy ( InputIterator first, InputIterator last,
                               OutputIterator result, const T& value );
Copy range removing value
Copies the values of the elements in the range  [first,last)  to the range positions beginning at  result , except for the elements that compare equal to  value , which are not copied.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
template <class InputIterator, class OutputIterator, class T>
  OutputIterator remove_copy ( InputIterator first, InputIterator last,
                               OutputIterator result, const T& value )
{
  for ( ; first != last; ++first)
    if (!(*first == value)) *result++ = *first;
  return result;
}

std::remove_copy_if

<algorithm>
template <class InputIterator, class OutputIterator, class Predicate>
  OutputIterator remove_copy_if ( InputIterator first, InputIterator last,
                                  OutputIterator result, Predicate pred );
Copy range removing values
Copies the values of the elements in the range  [first,last)  to the range positions beginning at  result , except those for which  pred  is true, which are not copied.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
template <class InputIterator, class OutputIterator, class Predicate>
  OutputIterator remove_copy_if ( InputIterator first, InputIterator last,
                                  OutputIterator result, Predicate pred )
{
  for ( ; first != last; ++first)
    if (!pred(*first)) *result++ = *first;
  return result;
}

std::unique

<algorithm>
template <class ForwardIterator>
  ForwardIterator unique ( ForwardIterator first, ForwardIterator last );

template <class ForwardIterator, class BinaryPredicate>
  ForwardIterator unique ( ForwardIterator first, ForwardIterator last,
                           BinaryPredicate pred );
Remove consecutive duplicates in range
Removes the duplicate consecutive elements from the range  [first,last) . This is done by removing from the resulting range all the elements that compare equal to the element right preceding them (only the first element in each group of consecutive equal elements is kept).

The resulting range consists of the elements between  first  and the iterator returned by the function, which points to the new end of range. The elements past the new end of range are still valid, although with unspecified values.

The comparison between elements is performed by either applying the  ==  comparison operator, or the template parameter  comp  (for the second version) between them.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
template <class ForwardIterator>
  ForwardIterator unique ( ForwardIterator first, ForwardIterator last )
{
  ForwardIterator result=first;
  while (++first != last)
  {
    if (!(*result == *first))  // or: if (!pred(*result,*first)) for the pred version
      *(++result)=*first;
  }
  return ++result;
}

std::unique_copy

<algorithm>
template <class InputIterator, class OutputIterator>
  OutputIterator unique_copy ( InputIterator first, InputIterator last,
                               OutputIterator result );

template <class InputIterator, class OutputIterator, class BinaryPredicate>
  OutputIterator unique_copy ( InputIterator first, InputIterator last,
                               OutputIterator result, BinaryPredicate pred );
Copy range removing duplicates
Copies the values of the elements in the range  [first,last)  to the range positions beginning at  result , except for the duplicate consecutive elements, which are not copied

The elements not copied are those that compare equal to the element right preceding them (only the first element in each group of consecutive equal elements is copied).

The comparison between elements is performed by either applying the  ==  comparison operator, or the template parameter  pred  (for the second version) between them.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
12
13
template <class InputIterator, class OutputIterator>
  OutputIterator unique_copy ( InputIterator first, InputIterator last,
                               OutputIterator result )
{
  typename std::iterator_traits<InputIterator>::value_type value = *first;
  *result=*first;
  while (++first != last)
  {
    if (!(value == *first))  // or: if (!pred(value,*first)) for the pred version
      *(++result) = value = *first;
  }
  return ++result;
}

std::reverse

<algorithm>
template <class BidirectionalIterator>
  void reverse ( BidirectionalIterator first, BidirectionalIterator last);
Reverse range
Reverses the order of the elements in the range  [first,last) .

The behavior of this function template is equivalent to:
1
2
3
4
5
6
template <class BidirectionalIterator>
  void reverse ( BidirectionalIterator first, BidirectionalIterator last)
{
  while ((first!=last)&&(first!=--last))
    swap (*first++,*last);
}

std::reverse_copy

<algorithm>
template <class BidirectionalIterator, class OutputIterator>
  OutputIterator reverse_copy ( BidirectionalIterator first,
                                BidirectionalIterator last, OutputIterator result );
Copy range reversed
Copies the values of the elements in the range [first,last) to the range positions beginning at result, but reversing its order.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
template <class BidirectionalIterator, class OutputIterator>
  OutputIterator reverse_copy ( BidirectionalIterator first,
                                BidirectionalIterator last, OutputIterator result )
{
  while (first!=last) *result++ = *--last;
  return result;
}

std::rotate

<algorithm>
template <class ForwardIterator>
  void rotate ( ForwardIterator first, ForwardIterator middle,
                ForwardIterator last );
Rotate elements in range
Rotates the order of the elements in the range  [first,last) , in such a way that the element pointed by  middle becomes the new first element.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
12
template <class ForwardIterator>
  void rotate ( ForwardIterator first, ForwardIterator middle,
                ForwardIterator last )
{
  ForwardIterator next = middle;
  while (first!=next)
  {
    swap (*first++,*next++);
    if (next==last) next=middle;
    else if (first == middle) middle=next;
  }
}

std::rotate_copy

<algorithm>
template <class ForwardIterator, class OutputIterator>
  OutputIterator rotate_copy ( ForwardIterator first, ForwardIterator middle,
                               ForwardIterator last, OutputIterator result );
Copy rotated range
Copies the values of the elements in the range  [first,last)  to the range positions beginning at  result , but rotating the order of the elements in such a way that the element pointed by  middle  becomes the first element in the resulting range.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
template <class ForwardIterator, class OutputIterator>
  OutputIterator rotate_copy ( ForwardIterator first, ForwardIterator middle,
                               ForwardIterator last, OutputIterator result )
{
  result=copy (middle,last,result);
  return copy (first,middle,result);
}

std::random_shuffle

<algorithm>
template <class RandomAccessIterator>
  void random_shuffle ( RandomAccessIterator first, RandomAccessIterator last );

template <class RandomAccessIterator, class RandomNumberGenerator>
  void random_shuffle ( RandomAccessIterator first, RandomAccessIterator last,
                        RandomNumberGenerator& rand );
Rearrange elements in range randomly
Rearranges the elements in the range  [first,last)  randomly.

The function swaps the value of each element with that of some other randomly chosen element. When provided, the function  rand  chooses which element.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
template <class RandomAccessIterator, class RandomNumberGenerator>
  void random_shuffle ( RandomAccessIterator first, RandomAccessIterator last,
                        RandomNumberGenerator& rand )
{
  iterator_traits<RandomAccessIterator>::difference_type i, n;
  n = (last-first);
  for (i=n-1; i>0; --i) swap (first[i],first[rand(i+1)]);
}

std::partition

<algorithm>
template <class BidirectionalIterator, class Predicate>
  BidirectionalIterator partition ( BidirectionalIterator first,
                                    BidirectionalIterator last, Predicate pred );
Partition range in two
Rearranges the elements in the range  [first,last) , in such a way that all the elements for which  pred  returns true precede all those for which it returns false. The iterator returned points to the first element of the second group.

The relative ordering within each group is not necessarily the same as before the call. See function  stable_partition for a function with a similar behavior and stability in the ordering.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <class BidirectionalIterator, class Predicate>
  BidirectionalIterator partition ( BidirectionalIterator first,
                                    BidirectionalIterator last, Predicate pred )
{
  while (true)
  {
    while (first!=last && pred(*first)) ++first;
    if (first==last--) break;
    while (first!=last && !pred(*last)) --last;
    if (first==last) break;
    swap (*first++,*last);
  }
  return first;
}

std::stable_partition

<algorithm>
template <class BidirectionalIterator, class Predicate>
  BidirectionalIterator stable_partition ( BidirectionalIterator first,
                                           BidirectionalIterator last,
                                           Predicate pred );
Partition range in two - stable ordering
Rearranges the elements in the range  [first,last) , in such a way that all the elements for which  pred  returns true precede all those for which it returns false, and, unlike function  partition , grants the preservation of the relative order of elements within each group.

This is generally implemented using a temporary buffer.

Parameters

first, last
Bidirectional iterators to the initial and final positions of the sequence to be partitioned. The range used is [first,last), which contains all the elements between  first and  last, including the element pointed by  firstbut not the element pointed by  last.
pred
Unary predicate taking an element in the range as argument, and returning a value indicating the falsehood (with  false, or a zero value) or truth ( true, or non-zero) of some condition applied to it. This can either be a pointer to a function or an object whose class overloads  operator().




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值