bool operator()( int value ) {
return value % 2 ? false : true; }
};
int main()
{
int new_value = 0;
int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 };
vector int, allocator vec( ia, ia+10 );
ostream_iterator int ofile( cout, " " );
cout "исходная последовательность:\n";
copy( ia, ia+10, ofile ); cout '\n';
replace_if( &ia[0], &ia[10],
bind2nd(lessint(),10), new_value );
cout "последовательность после применения replace_if 10 "
"с заменой на 0:\n";
copy( ia, ia+10, ofile ); cout '\n';
replace_if( vec.begin(), vec.end(),
EvenValue(), new_value );
cout "последовательность после применения replace_if четное"
"с заменой на 0:\n";
copy( vec.begin(), vec.end(), ofile ); cout '\n';
}
Алгоритм reverse()
template class BidirectionalIterator
void
reverse( BidirectionalIterator first,
BidirectionalIterator last );
reverse() меняет порядок элементов контейнера в диапазоне [first,last) на противоположный. Например, если есть последовательность {0,1,1,2,3}, то после обращения получится {3,2,1,1,0}. Алгоритм reverse_copy() template class BidirectionalIterator, class OutputIterator OutputIterator reverse_copy( BidirectionalIterator first, BidirectionalIterator last, OutputIterator result ); reverse_copy() ведет себя так же, как reverse(), только новая последовательность копируется в контейнер, начиная с result. Возвращаемый итератор указывает на элемент, расположенный за последним скопированным. Исходный контейнер остается без изменения. #include algorithm #include list #include string #include iostream.h /* печатается: Исходная последовательность строк: Signature of all things I am here to read seaspawn and seawrack that rusty boot Последовательность строк после применения reverse(): boot rusty that seawrack and seaspawn read to here am I things all of Signature */ class print_elements { public: void operator()( string elem ) { cout elem ( _line_cnt++%8 ? " " : "\n\t" ); } static void reset_line_cnt() { _line_cnt = 1; } private: static int _line_cnt; }; int print_elements::_line_cnt = 1; int main() { string sa[] = { "Signature", "of", "all", "things", "I", "am", "here", "to", "read", "seaspawn", "and", "seawrack", "that", "rusty", "boot" }; list string, allocator slist( sa, sa+15 ); cout "Исходная последовательность строк:\n\t"; for_each( slist.begin(), slist.end(), print_elements() ); cout "\n\n"; reverse( slist.begin(), slist.end() ); print_elements::reset_line_cnt(); cout "Последовательность строк после применения reverse():\n\t"; for_each( slist.begin(), slist.end(), print_elements() ); cout "\n"; list string, allocator slist_copy( slist.size() ); reverse_copy( slist.begin(), slist.end(), slist_copy.begin() ); }
Алгоритм rotate()
template class ForwardIterator
void
rotate( ForwardIterator first,
ForwardIterator middle, ForwardIterator last );
rotate() перемещает элементы из диапазона [first,last) в конец контейнера. Элемент, на который указывает middle, становится первым. Например, для слова "hissboo" вращение вокруг буквы 'b' превращает слово в "boohiss". Алгоритм rotate_copy()
template class ForwardIterator, class OutputIterator
OutputIterator
rotate_copy( ForwardIterator first, ForwardIterator middle,
ForwardIterator last, OutputIterator result );
rotate_copy() ведет себя так же, как rotate(), только новая последовательность копируется в контейнер, начиная с result. Возвращаемый итератор указывает на элемент, расположенный за последним скопированным. Исходный контейнер остается без изменения.
#include algorithm
#include vector
#include iostream.h
/* печатается:
исходная последовательность:
1 3 5 7 9 0 2 4 6 8 10
вращение вокруг среднего элемента(0) ::
0 2 4 6 8 10 1 3 5 7 9