Standard Template Library Deque на C++

Deque (двоичная очередь) — контейнер для хранения динамических данных. Данные могут быть расширены или сжаты на обоих концах (либо спереди, либо сзади).


#include "stdafx.h"
#include <iostream>
#include <deque>
int main()
{
std::deque<int> my_deque_begin;
for (int i = 0; i < 10; i++)
{
my_deque_begin.push_back(i);
}
std::deque<int>::iterator it = my_deque_begin.begin();
while (it != my_deque_begin.end())
{
std::cout << " " << *it++;
}
std::cout << "\n";
my_deque_begin.pop_front();
my_deque_begin.resize(11);
std::deque<int>::iterator it1 = my_deque_begin.begin();
while (it1 != my_deque_begin.end())
{
std::cout << " " << *it1++;
}
std::cout << "\n";
my_deque_begin.resize(15,7);
std::deque<int>::iterator it2 = my_deque_begin.begin();
while (it2 != my_deque_begin.end())
{
std::cout << " " << *it2++;
}
std::cout << "\n";
my_deque_begin.resize(20);
std::deque<int>::iterator it3 = my_deque_begin.begin();
while (it3 != my_deque_begin.end())
{
std::cout << " " << *it3++;
}
std::cout << "\n";
std::cout << my_deque_begin.size()<< "\n";
std::cout << my_deque_begin.front() << "\n";
std::cout << my_deque_begin.back() << "\n";
std::deque<int> my_deque_end;
for (int i = 0; i < 10; i++)
{
my_deque_end.insert(my_deque_end.end(),i);
}
std::deque<int>::iterator it_end = my_deque_end.begin();
while (it_end != my_deque_end.end())
{
std::cout << " " << *it_end++;
}
std::cout << "\n";
std::deque<int> my_deque_rbegin(5);
int i = 0;
for (std::deque<int>::reverse_iterator it_r = my_deque_rbegin.rbegin(); it_r != my_deque_rbegin.rend(); it_r++)
{
*it_r = ++i;
}
for (std::deque<int>::reverse_iterator it_r = my_deque_rbegin.rbegin(); it_r != my_deque_rbegin.rend(); it_r++)
{
std::cout << " " << *it_r;
}
std::cout<<"\n";
std::deque<int> my_deque_empty;
int sum = 0;
for (int i = 0; i < 10; i++)
{
my_deque_empty.push_back(i);
}
while (!my_deque_empty.empty())
{
sum+=my_deque_empty.front();
my_deque_empty.pop_front();
}
std::cout << sum << "\n";
std::deque<int> mydeque_op(10);
std::deque<int>::size_type sz = mydeque_op.size();
for (int i = 0; i < 10; i++)
{
mydeque_op[i] = i;
}
for (int i = 0; i < sz/2; i++)
{
int temp;
temp = mydeque_op[sz-1-i];
mydeque_op[sz - 1 - i] = mydeque_op[i];
mydeque_op[i]=temp;
}
for (int i = 0; i < 10; i++)
{
std::cout <<" "<< mydeque_op[i];
}
std::cout << "\n";
std::deque < int> deque_at(5);
for (int i = 0; i < deque_at.size(); i++)
{
mydeque_op.at(i) = i+1;
}
for (int i = 0; i < deque_at.size(); i++)
{
std::cout << " " << mydeque_op.at(i);
}
std::cout << "\n";
int massiv[4] = {33,5,8,27};
std::deque <int> deque_copy_massiv (massiv, massiv+sizeof(massiv)/sizeof(int));
std::deque <int> deque_copy_massiv_new(2);
deque_copy_massiv_new = deque_copy_massiv;
for (int i = 0; i < deque_copy_massiv_new.size(); i++)
{
std::cout << " " << deque_copy_massiv_new[i];
}
std::cout << "\n";
std::deque <int> deque_assign;
deque_assign.assign(11, 100);
std::cout << int(deque_assign.size());
std::cout << "\n";
std::deque <int> deque_insert;
for (int i = 0; i < 10; i++)
{
deque_insert.push_back(i);
}
std::deque <int> ::iterator it_ins = deque_insert.begin();
++it_ins;
deque_insert.insert(it_ins,78);
deque_insert.erase(deque_insert.begin()+5);
// deque_insert.emplace(deque_insert.begin()+7,1000);
// deque_insert.emplace_front(123);
// deque_insert.emplace_back(321);
std::deque <int> deque_new_swap;
deque_new_swap.swap(deque_insert);
for (it_ins = deque_new_swap.begin(); it_ins != deque_new_swap.end(); ++it_ins)
{
std::cout << " " << *it_ins;
}
std::cout << "\n";
deque_new_swap.clear();
std::cout<< deque_new_swap.size()<< "\n";
if (deque_new_swap == deque_insert)
{
std::cout << "equal " << "\n";
}
system("pause");
return 0;
}

Результат работы программы Deque (двоичная очередь) на C++:

Standard Template Library Deque на C++ STL

Leave a Reply

Ваш e-mail не будет опубликован.