C++ deque container related operations

Title C++ deque (queue) container

1. Deque's constructor
1. c.push_back(k) creates an element whose value is t or created by args at the end of c. Return void
2, c.push_front(t), create an element whose value is t or created by args at the head of c. Return void

    deque<int> d;
	d.push_back(10);
	d.push_back(20);
	d.push_back(30);
	d.push_back(40);
	printDeque(d);

Output:

10 20 30 40
请按任意键继续. . .

2. Swap exchange
1, swap(c1,c2)
2, c1.swap(c2).
Both of the appeals are the exchange of elements in c1 and c2. c1 and c2 must have the same type. swap is usually much faster than copying elements from c2 to c1

    deque<int> d;
	d.push_back(10);
	d.push_back(20);
	d.push_back(30);
	d.push_back(40);
	
	deque<int> d2(10, 1);
	printDeque(d2);
	d2.swap(d);
	printDeque(d2);

Output:

1 1 1 1 1 1 1 1 1 1
10 20 30 40
请按任意键继续. . .

3. Deque size operation
1. The member function size returns the number of elements in the container;
2. Empty returns a boolean value true when size is 0, otherwise it returns false;
3. max_size returns a maximum that is greater than or equal to the container of this type The value of the number of elements
4, d.resize(20,0) re-specifies the length of the element, and fills it with the second element

deque<int> d = {
    
    10,20,30,40,50,60};
	printDeque(d);
	cout << "d中元素个数为:" << d.size() << endl;
	if (!d.empty())
	{
    
    
		cout << "d的容器不为空" << endl;
	}
	else
	{
    
    
		cout << "d的容器为空" << endl;
	}
	d.resize(20, 0);
	printDeque(d);

Output:

10 20 30 40 50 60
d中元素个数为:6
d的容器不为空
10 20 30 40 50 60 0 0 0 0 0 0 0 0 0 0 0 0 0 0
请按任意键继续. . .

Four, deque double-ended insertion and deletion operations
1, d.push_back(t); insert a data into the end of the container
2, d.push_front(t); insert a data into the head of the container
3, d.pop_back(); // Delete the last data of the container
4, d.pop_front();//Delete the first data of the container

	deque<int> d = {
    
     10,20,30,40,50,60 };
	cout << "d中的原始元素为:";
	printDeque(d);
	d.push_back(25);
	d.push_front(20);
	cout << "插入操作后的d中的元素为:";
	printDeque(d);
	d.pop_back();
	d.pop_front();
	cout << "删除操作后的d中的元素为:";
	printDeque(d);

Output:

d中的原始元素为:10 20 30 40 50 60
插入操作后的d中的元素为:20 10 20 30 40 50 60 25
删除操作后的d中的元素为:10 20 30 40 50 60
请按任意键继续. . .

Five, deque insert operation and delete operation
1, d.insert(++d.begin(), 4);//Insert the second parameter at the first parameter position, the first parameter is iterator
2, d. erase§; Delete the element specified by the iterator p and return an iterator pointing to the element after the deleted element. If p points to the tail element, it returns the iterator after the end. If p is a post-tail iterator, the function behavior is undefined

deque<int> d = {
    
     10,20,30,40,50,60 };
	printDeque(d);
	d.insert(++d.begin(), 4); 
	cout << "插入之后的d容器的元素为:";
	printDeque(d);

	d.erase(++d.begin());
	cout << "删除后的操作为:";
	printDeque(d);

Output:

10 20 30 40 50 60
插入之后的d容器的元素为:10 4 20 30 40 50 60
删除后的操作为:10 20 30 40 50 60
请按任意键继续. . .

Guess you like

Origin blog.csdn.net/Little_XWB/article/details/108023169