Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C++ для начинающих (Стенли Липпман) 3-е хххх.pdf
Скачиваний:
86
Добавлен:
30.05.2015
Размер:
5.92 Mб
Скачать

С++ для начинающих

1109

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

class even_by_twos { public:

even_by_twos( int seed = 0 ) : _seed( seed ){} int operator()() { return _seed += 2; }

private:

int _seed;

};

template <class Type>

void print_elements( Type elem ) { cout << elem << " "; }

int main()

{

list< int, allocator > ilist( 10 ); void (*pfi)( int ) = print_elements;

generate_n( ilist.begin(), ilist.size(), even_by_twos() );

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

//generate_n с even_by_twos():

//2 4 6 8 10 12 14 16 18 20

cout << "generate_n с even_by_twos():\n";

for_each( ilist.begin(), ilist.end(), pfi ); cout << "\n"; generate_n( ilist.begin(), ilist.size(), even_by_twos( 100 ) );

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

//generate_n с even_by_twos( 100 ):

//102 104 106 108 110 112 114 116 118 120

cout << "generate_n с even_by_twos( 100 ):\n"; for_each( ilist.begin(), ilist.end(), pfi );

}

template< class InputIterator1, class InputIterator2 > bool

includes( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2 );

template< class InputIterator1, class InputIterator2, class Compare >

bool

includes( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2,

Алгоритм includes()

Compare comp );

includes() проверяет, каждый ли элемент последовательности [first1,last1) входит в последовательность [first2,last2). Первый вариант предполагает, что

С++ для начинающих

1110

последовательности отсортированы в порядке, определяемом оператором меньше”;

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

int main()

{

int ia1[] = { 13, 1, 21, 2, 0, 34, 5, 1, 8, 3, 21, 34 }; int ia2[] = { 21, 2, 8, 3, 5, 1 };

// алгоритму includes следует передавать отсортированные контейнеры

sort( ia1, ia1+12 ); sort( ia2, ia2+6 );

// печатает: каждый элемент ia2 входит в ia1? Да

bool res = includes( ia1, ia1+12, ia2, ia2+6 ); cout << "каждый элемент ia2 входит в ia1? "

<< (res ? "Да" : "Нет") << endl;

vector< int, allocator > ivect1( ia1, ia1+12 ); vector< int, allocator > ivect2( ia2, ia2+6 );

// отсортирован в порядке убывания

sort( ivect1.begin(), ivect1.end(), greater<int>() ); sort( ivect2.begin(), ivect2.end(), greater<int>() );

res = includes( ivect1.begin(), ivect1.end(), ivect2.begin(), ivect2.end(), greater<int>() );

// печатает: каждый элемент ivect2 входит в ivect1? Да

cout << "каждый элемент ivect2 входит в ivect1? " << (res ? "Да" : "Нет") << endl;

второй что порядок задается параметром-типом comp.

}

Алгоритм inner_product()

С++ для начинающих

1111

template< class InputIterator1, class InputIterator2 class Type >

Type inner_product(

InputIterator1 first1, InputIterator1 last, InputIterator2 first2, Type init );

template< class InputIterator1, class InputIterator2 class Type,

class BinaryOperation1, class BinaryOperation2 >

Type inner_product(

InputIterator1 first1, InputIterator1 last, InputIterator2 first2, Type init,

BinaryOperation1 op1, BinaryOperation2 op2 );

Первый вариант суммирует произведения соответственных членов обеих последовательностей и прибавляет результат к начальному значению init. Первая последовательность ограничена итераторами [first1,last1), вторая начинается с first2 и обходится синхронно с первой. Например, если даны последовательности {2,3,5,8} и {1,2,3,4}, то результат вычисляется следующим образом:

2*1 + 3*2 + 5*3 + 8*4

Если начальное значение равно 0, алгоритм вернет 55.

Во втором варианте вместо сложения используется бинарная операция op1, а вместо умножения бинарная операция op1. Например, если для приведенных выше последовательностей применить вычитание в качестве op1 и сложение в качестве op2, то результат будет вычисляться так:

(2+1) - (3+2) - (5+3) - (8+4)

inner_product() это один из численных алгоритмов. Для его использования в программу необходимо включить заголовочный файл <numeric>.

С++ для начинающих

1112

#include <numeric> #include <vector> #include <iostream.h>

int main()

{

int ia[] = { 2, 3, 5, 8 }; int ia2[] = { 1, 2, 3, 4 };

//перемножить пары элементов из обоих массивов,

//сложить и добавить начальное значение: 0

int res = inner_product( &ia[0], &ia[4], &ia2[0], 0 );

// печатает: скалярное произведение массивов: 55 cout << "скалярное произведение массивов: "

<< res << endl;

vector<int, allocator> vec( ia, ia+4 ); vector<int, allocator> vec2( ia2, ia2+4 );

//сложить пары элементов из обоих векторов,

//вычесть из начального значения: 0

res = inner_product( vec.begin(), vec.end(), vec2.begin(), 0,

minus<int>(), plus<int>() );

// печатает: скалярное произведение векторов: -28 cout << "скалярное произведение векторов: "

<< res << endl;

return 0;

}

template< class BidirectionalIterator > void

inplace_merge( BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last );

template< class BidirectionalIterator, class Compare > void

inplace_merge( BidirectionalIterator first, BidirectionalIterator middle,

Алгоритм inplace_merge()

BidirectionalIterator last, Compare comp );

inplace_merge() объединяет две соседние отсортированные последовательности, ограниченные парами итераторов [first,middle) и [middle,last). Результирующая последовательность затирает исходные, начиная с позиции first. В первом варианте

С++ для начинающих

1113

для упорядочения элементов используется оператор меньше”, определенный для типа

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

template <class Type>

void print_elements( Type elem ) { cout << elem << " "; }

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

ia разбит на два отсортированных подмассива:

12 15 17 20 23 26 29 35 40 51 10 16 21 41 44 54 62 65 71 74

ia inplace_merge:

10 12 15 16 17 20 21 23 26 29 35 40 41 44 51 54 62 65 71 74

ivec разбит на два отсортированных подвектора:

51 40 35 29 26 23 20 17 15 12 74 71 65 62 54 44 41 21 16 10

ivec inplace_merge:

74 71 65 62 54 51 44 41 40 35 29 26 23 21 20 17 16 15 12 10 */

int main()

{

int ia[] = { 29,23,20,17,15,26,51,12,35,40, 74,16,54,21,44,62,10,41,65,71 };

vector< int, allocator > ivec( ia, ia+20 ); void (*pfi)( int ) = print_elements;

// отсортировать обе подпоследовательности sort( &ia[0], &ia[10] );

sort( &ia[10], &ia[20] );

cout << "ia разбит на два отсортированных подмассива: \n"; for_each( ia, ia+20, pfi ); cout << "\n\n";

inplace_merge( ia, ia+10, ia+20 );

cout << "ia inplace_merge:\n"; for_each( ia, ia+20, pfi ); cout << "\n\n";

sort(

ivec.begin(),

ivec.begin()+10, greater<int>()

);

sort(

ivec.begin()+10,

ivec.end(),

greater<int>()

);

cout << "ivec разбит на два отсортированных подвектора: \n"; for_each( ivec.begin(), ivec.end(), pfi ); cout << "\n\n";

inplace_merge( ivec.begin(), ivec.begin()+10, ivec.end(), greater<int>() );

cout << "ivec inplace_merge:\n";

for_each( ivec.begin(), ivec.end(), pfi ); cout << endl;

элементов контейнера, во втором операция сравнения, переданная программистом.

}