STL————deque容器

qq2628272394發表於2020-11-01

deque容器基本概念:

功能:
雙端陣列,可以對頭端進行插入刪除操作
deque與vector區別:
vector對於頭部的插入刪除效率低,資料量越大,效率越底
deque相對而言,對頭部插入刪除速度會比vector快
vector訪問元素時的速度會比deque快,這兩者內部實現有關

建構函式:

#include<iostream>
using namespace std;
#include<deque>


void Printdeque(const deque<int>& d)
{
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		cout << *it << "   ";
	}
	cout << endl;
}

void test01()
{
	deque<int>d1;
	for (int i = 0; i < 10; i++)
	{
		d1.push_back(i);
	}
	Printdeque(d1);


	deque<int>d2(d1.begin(), d1.end());
	Printdeque(d2);

	deque<int>d3(10, 100);
	Printdeque(d3);


	deque<int>d4(d1);
	Printdeque(d4);


}

int main()
{
	test01();
	system("pause");
	return 0;
}

賦值操作:

#include<iostream>
using namespace std;
#include<deque>

void Printdeque(const deque<int>& d)
{
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	deque<int>d1;
	for (int i = 0; i < 10; i++)
	{
		d1.push_back(i);
	}
	Printdeque(d1);

	//operator= 賦值
	deque<int>d2;
	d2 = d1;
	Printdeque(d2);

	//assign 賦值
	deque<int>d3;
	d3.assign(d1.begin(), d1.end());
	Printdeque(d3);

	deque<int>d4;
	d4.assign(10, 100);
	Printdeque(d4);

}


int main()
{
	test01();
	system("pause");
	return 0;
}


大小操作:

deque容器沒有容量概念

#include<iostream>
using namespace std;
#include<deque>

void Printdeque(const deque<int>& d)
{
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	deque<int>d1;
	for (int i = 0; i < 10; i++)
	{
		d1.push_back(i);
	}
	Printdeque(d1);

	if (d1.empty())
	{
		cout << "d1為空" << endl;
	}
	else
	{
		cout << "d1不為空" << endl;
		cout << "d1的大小為:" << d1.size() << endl;

	}

	//重新指定大小
	//d1.resize(15);
	d1.resize(15, 1);
	Printdeque(d1);

	d1.resize(5);
	Printdeque(d1);

}

int main()
{
	test01();
	system("pause");
	return 0;
}