1 1 3 5 13 21
*/
class EvenValue {
public:
bool operator()( int value ) {
return value % 2 ? false : true; }
};
int main()
{
int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 };
vector int, allocator ::iterator iter;
vector int, allocator vec( ia, ia+10 );
ostream_iterator int ofile( cout, " " );
cout "исходная последовательность:\n";
copy( vec.begin(), vec.end(), ofile ); cout '\n';
iter = remove_if( vec.begin(), vec.end(),
bind2nd(lessint(),10) );
vec.erase( iter, vec.end() );
cout "последовательность после применения remove_if
Алгоритм replace()
template
replace() заменяет в диапазоне [first,last) все элементы со значением old_value на new_value.
Алгоритм replace_copy()
template class InputIterator, class InputIterator,
class Type
OutputIterator
replace_copy( InputIterator first, InputIterator last,
class OutputIterator result,
const Type& old_value, const Type& new_value );
replace_copy() ведет себя так же, как replace(), только новая последовательность копируется в контейнер, начиная с result. Возвращаемый итератор указывает на элемент, расположенный за последним скопированным. Исходный контейнер остается без изменения.
#include algorithm
#include vector
#include iostream.h
/* печатается:
исходная последовательность:
Christopher Robin Mr. Winnie the Pooh Piglet Tigger Eeyore
последовательность после применения replace():
Christopher Robin Pooh Piglet Tigger Eeyore
*/
int main()
{
string oldval( "Mr. Winnie the Pooh" );
string newval( "Pooh" );
ostream_iterator string ofile( cout, " " );
string sa[] = {
"Christopher Robin", "Mr. Winnie the Pooh",
"Piglet", "Tigger", "Eeyore"
};
vector string, allocator vec( sa, sa+5 );
cout "исходная последовательность:\n";
copy( vec.begin(), vec.end(), ofile ); cout '\n';
replace( vec.begin(), vec.end(), oldval, newval );
cout "последовательность после применения replace():\n";
copy( vec.begin(), vec.end(), ofile ); cout '\n';
vector string, allocator vec2;
replace_copy( vec.begin(), vec.end(),
inserter( vec2, vec2.begin() ),
newval, oldval );
cout "последовательность после применения replace_copy():\n";
copy( vec.begin(), vec.end(), ofile ); cout '\n';
}
Алгоритм replace_if()
template class ForwardIterator, class Predicate, class Type
void
replace_if( ForwardIterator first, ForwardIterator last,
Predicate pred, const Type& new_value );
replace_if() заменяет значения всех элементов в диапазоне [first,last), для которых предикат pred равен true, на new_value. Алгоритм replace_copy_if()
templateclass ForwardIterator, class OutputIterator,
class Predicate, class Type
OutputIterator
replace_copy_if( ForwardIterator first, ForwardIterator last,
class OutputIterator result,
Predicate pred, const Type& new_value );
replace_copy_if() ведет себя так же, как replace_if(), только новая последовательность копируется в контейнер, начиная с result. Возвращаемый итератор указывает на элемент, расположенный за последним скопированным. Исходный контейнер остается без изменения.
#include algorithm
#include vector
#include iostream.h
/*
исходная последовательность:
0 1 1 2 3 5 8 13 21 34
последовательность после применения replace_if 10 с заменой на 0:
0 0 0 0 0 0 0 13 21 34
последовательность после применения replace_if четное с заменой на 0:
0 1 1 0 3 5 0 13 21 0
*/
class EvenValue {
public:
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() ); }