See also

Binary Predicate, unique, remove_copy, remove_copy_if, adjacent_find

reverse

Category: algorithms

Component type: function

Prototype

template <class BidirectionalIterator>

void reverse(BidirectionalIterator first, BidirectionalIterator last);

Description

Reverse reverses a range. That is: for every i such that 0 <= i <= (last – first) / 2), it exchanges *(first + i) and *(last – (i + 1)).

Definition

Defined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h.

Requirements on types

• BidirectionalIterator is a model of Bidirectional Iterator.

• BidirectionalIterator is mutable.

Preconditions

• [first, last) is a valid range.

Complexity

Linear: reverse(first, last) makes (last – first) / 2 calls to swap.

Example

vector<int> V;

V.push_back(0);

V.push_back(1);

V.push_back(2);

copy(V.begin(), V.end(), ostream_iterator<int>(cout, ' '));

// Output: 0 1 2

reverse(V.begin(), V.end());

copy(V.begin(), V.end(), ostream_iterator<int>(cout, ' '));

// Output: 2 1 0

See also

reverse_copy

reverse_copy

Category: algorithms

Component type: function

Prototype

template <class BidirectionalIterator, class OutputIterator>

OutputIterator reverse_copy(BidirectionalIterator first, BidirectionalIterator last, OutputIterator result);

Description

Reverse_copy copies elements from the range [first, last) to the range [result, result + (last – first)) such that the copy is a reverse of the original range. Specifically: for every i such that 0 <= i < (last – first), reverse_copy performs the assignment *(result + (last – first) – i) = *(first + i).

The return value is result + (last – first).

Definition

Defined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h.

Requirements on types

• BidirectionalIterator is a model of Bidirectional Iterator.

• OutputIterator is a model of Output Iterator.

• The value type of BidirectionalIterator is convertible to a type in OutputIterator's set of value types.

Preconditions

• [first, last) is a valid range.

• There is enough space to hold all of the elements being copied. More formally, the requirement is that [result, result + (last – first)) is a valid range.

• The ranges [first, last) and [result, result + (last – first)) do not overlap.

Complexity

Linear: exactly last – first assignments.

Example

vector<int> V;

V.push_back(0);

V.push_back(1);

V.push_back(2);

copy(V.begin(), V.end(), ostream_iterator<int>(cout, ' '));

// Output: 0 1 2

list<int> L(V.size());

reverse_copy(V.begin(), V.end(), L.begin());

copy(L.begin(), L.end(), ostream_iterator<int>(cout, ' '));

// Output: 2 1 0

See also

reverse, copy

rotate

Category: algorithms

Component type: function

Prototype

template <class ForwardIterator>

inline ForwardIterator rotate(ForwardIterator first, ForwardIterator middle, ForwardIterator last);

Description

Rotate rotates the elements in a range. That is, the element pointed to by

Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату