Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C++ для начинающих.pdf
Скачиваний:
183
Добавлен:
01.05.2014
Размер:
3.97 Mб
Скачать

}

Алгоритм fill()

template< class ForwardIterator, class Type

>

void

fill( ForwardIterator first,

ForwardIterator last, const Type& value );

fill() помещает копию значения value в каждый элемент диапазона, ограниченного парой итераторов [first,last).

#include <algorithm> #include <list> #include <string> #include <iostream.h>

/* печатается:

исходная последовательность элементов массива: 0 1 1 2 3 5 8

массив после fill(ia+1,ia+6): 0 9 9 9 9 9 8

исходная последовательность элементов списка: c eiffel java ada perl

список после fill(++ibegin,--iend): c c++ c++ c++ perl

*/

int main()

{

const int value = 9;

int ia[] = { 0, 1, 1, 2, 3, 5, 8 }; ostream_iterator< int > ofile( cout, " " );

cout << "исходная последовательность элементов массива:\n";

copy( ia, ia+7, ofile ); cout << "\n\n";

fill( ia+1, ia+6, value );

cout << "массив после fill(ia+1,ia+6):\n"; copy( ia, ia+7, ofile ); cout << "\n\n";

string the_lang( "c++" );

string langs[5] = { "c", "eiffel", "java", "ada", "perl" };

list< string, allocator > il( langs, langs+5 ); ostream_iterator< string > sofile( cout, " " );

cout << "исходная последовательность элементов списка:\n"; copy( il.begin(), il.end(), sofile ); cout << "\n\n";

typedef list<string,allocator>::iterator iterator;

iterator ibegin = il.begin(), iend = il.end(); fill( ++ibegin, --iend, the_lang );

cout << "список после fill(++ibegin,--iend):\n"; copy( il.begin(), il.end(), sofile ); cout << "\n\n";

}

Алгоритм fill_n()

template< class ForwardIterator, class Size, class Type

>

void

fill_n( ForwardIterator first,

Size n, const Type& value );

fill_n() присваивает count элементам из диапазона [first,first+count) значение value.

#include <algorithm> #include <vector> #include <string> #include <iostream.h>

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;

/* печатается:

исходная последовательность элементов массива: 0 1 1 2 3 5 8

массив после fill_n( ia+2, 3, 9 ): 0 1 9 9 9 5 8 исходная последовательность строк:

Stephen closed his eyes to hear his boots crush crackling wrack and shells

последовательность после применения fill_n():

Stephen closed his xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx crackling wrack and shells

*/

int main()

{

int value = 9; int count = 3;

int ia[] = { 0, 1, 1, 2, 3, 5, 8 }; ostream_iterator< int > iofile( cout, " " );

cout << "исходная последовательность элементов массива:\n"; copy( ia, ia+7, iofile ); cout << "\n\n";

fill_n( ia+2, count, value );

cout << "массив после fill_n( ia+2, 3, 9 ):\n"; copy( ia, ia+7, iofile ); cout << "\n\n";

string replacement( "xxxxx" );

string sa[] = { "Stephen", "closed", "his", "eyes", "to", "hear", "his", "boots", "crush", "crackling", "wrack", "and", "shells" };

vector< string, allocator > svec( sa, sa+13 );

cout << "исходная последовательность строк:\n\t"; for_each( svec.begin(), svec.end(), print_elements() ); cout << "\n\n";

fill_n( svec.begin()+3, count*2, replacement );

print_elements::reset_line_cnt();

cout << "последовательность после применения fill_n():\n\t";

for_each( svec.begin(), svec.end(), print_elements() ); cout << "\n";