C ++ std :: deque basic usage

#include <iostream>
#include <string>
#include <deque>

// https://zh.cppreference.com/w/cpp/container/deque

Difference // vector and the deque
// https://www.cnblogs.com/zhuyf87/archive/2012/12/09/2809896.html

using namespace std;


int main ()
{
	int a[] = { 1,2,3 };
	and <int> = {1,2,3} and the areas;

	//////////////////////////////////////////////////////////////////////////
	
	deq.assign(4, 5); // 5 5 5 5

	deque<int> deq1 = deq; // deep copy
	deq1 [0] = 10;
	deq1.at(0) = 25;
	int vhead = deq1.front();
	int vtail = deq1.back();

	//////////////////////////////////////////////////////////////////////////
	// iterator

	for (auto it = deq1.begin(); it != deq1.end(); ++it)
	{
		std::cout << *it << " ";
	}
	std::cout << std::endl; // 25 2 3

	for (auto it = deq1.cbegin(); it != deq1.cend(); ++it)
	{
		std::cout << *it << " ";
	}
	std::cout << std::endl; // 25 2 3

	for (auto it = deq1.rbegin(); it != deq1.rend(); ++it)
	{
		std::cout << *it << " ";
	}
	std::cout << std::endl; // 3 2 25


	//////////////////////////////////////////////////////////////////////////
	
	bool isEmpty = deq1.empty();
	size_t size = deq1.size();
	size_t mSzie = deq1.max_size();

	deq1.clear();
	deq1.shrink_to_fit();

	deq1.insert(deq1.begin(), 6);
	deq1.emplace(deq1.begin(), 7);

	deq1.insert(deq1.begin(), 2, 55);

	and <int> deqtt = {11,22,33}; 
	deq1.insert(deq1.begin(), deqtt.begin(), deqtt.end()); // 11 22 33 55 55 7 6 

	deq1.insert(deq1.begin(), {999, 888}); // 999 888 11 22 33 55 55 7 6 

	deq1.push_back(88);
	deq1.emplace_back(99);

	deq1.push_front(100);
	deq1.emplace_front(110);

	deq1.erase(deq1.begin());
	deq1.erase(deq1.begin(), deq1.end());

	deq1.swap (deqtt); // exchange of size need not be the same size

	//////////////////////////////////////////////////////////////////////////
	// resize and when initialization function value

	deq1.clear();
	deq1.resize(2); // 0 0
	deq1.resize (2, 4); // 0 0 4 which also does not work.

	deq1.clear();
	deq1.resize(2, 4); // 4 4 
	deq1.resize (2); // 4 4 did not change size, see the description below

	deq1.clear();
	deq1.resize(2); // 0 0
	deq1.resize (3, 4); // 0 0 4 if size is to be changed, the initial value of 4 new value! It will not be deleted, nor changed the original space.

	deq1.clear();
	deq1.resize(2, 4); // 4 4 
	deq1.resize (3); // 4 4 0 0 default initial value int

	return 0;
}

  

 

Guess you like

Origin www.cnblogs.com/alexYuin/p/12080028.html