Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
MYлекция14.docx
Скачиваний:
9
Добавлен:
22.02.2015
Размер:
104.27 Кб
Скачать

Int main()

{

int arr1[] = { 1, 3, 5, 7, 9 };

int arr2[] = { 2, 4, 6, 8, 10 };

deque<int> d1;

deque<int> d2;

for(int j=0; j<5; j++) //transfer arrays to deques

{

d1.push_back( arr1[j] );

d2.push_back( arr2[j] );

}

copy( d1.begin(), d1.end(), d2.begin() ); //copy d1 to 2

for(int k=0; k<d2.size(); k++) // display d2

cout << d2[k] << ' ';

cout << endl;

return 0;

Пример3 для deque

// demonstrates insert iterators with queues

#include <iostream>

#include <deque>

#include <algorithm>

using namespace std;

Int main()

{

int arr1[] = { 1, 3, 5, 7, 9 }; //initialize d1

int arr2[] = {2, 4, 6}; //initialize d2

deque<int> d1;

deque<int> d2;

for(int i=0; i<5; i++) //transfer arrays to deques

d1.push_back( arr1[i] );

for(int j=0; j<3; j++)

d2.push_back( arr2[j] ); //copy d1 to back of d2

copy( d1.begin(), d1.end(), back_inserter(d2) );

// функция back_inserter(d2)вставляет элементы в конец контейнера, // //работает итератор back_inser_iteratorитератор вставки в конец

cout << "\nd2: "; //display d2

for(int k=0; k<d2.size(); k++)

cout << d2[k] << ' ';

cout << endl;

return 0;

}

Списки list

поддерживает конструкторы, операции, функции и итераторы, аналогичные векторам и очередям. Только списки реализуют операции:

  • void splice(it, l) – переносит другой список l в *this в позицию перед it.

  • void splice(it, l, first) – переносит первый элемент списка l в *this в позицию перед it.(элемент исчезает в одном списке и переносится в другой) .

  • void splice(it, l, first, last) ) – переносит часть списка l в *this в позицию перед it.

  • unique() – из двух соседних одинаковых элементов один уничтожает.

Пример1 для list

// list.cpp

// demonstrates push_front(), front(), pop_front()

#include <iostream>

#include <list>

using namespace std;

int main()

{

list<int> ilist;

ilist.push_back(30); // push items on back

ilist.push_back(40);

ilist.push_front(20); // push items on front

ilist.push_front(10);

int size = ilist.size(); // number of items

for(int j=0; j<size; j++)

{

cout << ilist.front() << ' '; // read item from front

ilist.pop_front(); // pop item off front

}

cout << endl;

return 0;

}

Пример2 для list

// listplus.cpp

// demonstrates reverse(), merge(), and unique()

#include <iostream>

#include <list>

using namespace std;

int main()

{

int j;

list<int> list1, list2;

int arr1[] = { 40, 30, 20, 10 };

int arr2[] = { 15, 20, 25, 30, 35 };

for(j=0; j<4; j++)

list1.push_back( arr1[j] ); // list1: 40, 30, 20, 10

for(j=0; j<5; j++)

list2.push_back( arr2[j] ); // list2: 15, 20, 25, 30, 35

list1.reverse(); // reverse list1: 10 20 30 40

list1.merge(list2); // merge list2 into list1

list1.unique(); // remove duplicate 20 and 30

int size = list1.size();

while( !list1.empty() )

{

cout << list1.front() << ' '; // read item from front

list1.pop_front(); // pop item off front

}

cout << endl;

return 0;

}

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]