Sequential container of c++ STL container

Table of contents

vectorcontainer

Default construction of vector objects

Initialization of vector

Vector traversal

Addition, deletion, modification and query of vector

Add and remove operations at the end of the vector  

Vector data access

deque container

Default construction of deque objects

Add and remove operations at the end of the deque

Deque data access

 stack container

The default construction of the stack object

stack's push() and pop() methods

Copy construction and assignment of stack objects

Stack traversal

queue container

Initialization and traversal of the queue container

list container

Add and remove operations at the head and tail of the list

List data access

Insertion of list

list deletion


There are three main types of Sequence containers in C++: vector, deque, and list  .

vectorcontainer

  • Vector is a container that manages elements in a dynamic array.
  • Vector can randomly access elements (support direct access to index value, use [] operator or at() method).

Adding or removing elements from the end of a vector is very fast. But inserting elements or removing elements in the middle or head is more time-consuming.

Default construction of vector objects

Vector is implemented by template class, and the default construction form of vector object

vector<T> vecT;

vector<int> vecInt; //A vector container storing int.

vector<float> vecFloat; //A vector container for storing floats.

vector<string> vecString; //A vector container that stores strings.

... //The pointer type or custom type can also be set in the angle brackets.

Class CA{};

vector<CA*> vecpCA; //The vector container used to store the pointer of the CA object.

vector<CA> vecCA; //The vector container used to store CA objects. Since the storage of container elements is carried out by copying by value, the CA must provide the copy constructor of the CA at this time to ensure that the copy between CA objects is normal.

Initialization of vector

//vector的初始化
void main32()
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(3);
	v1.push_back(5);
	v1.push_back(7);

	vector<int> v2 = v1;
	vector<int> v3(v1.begin(), v1.begin() + 2);
}

Vector traversal

There are two main ways, array traversal and iterator traversal.

void printV(vector<int>& v)
{
	for (int i = 0; i < v.size(); i++)
	{
		cout << v[i] << " ";
	}
	cout << endl;
}
void main35()
{
	vector<int> v1(10);
	for (int i = 0; i < 10; i++)
	{
		v1[i] = i + 1;
	}

	for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
	{
		cout << *it << " ";
	}

	cout << endl;
	//逆向遍历
	for (vector<int>::reverse_iterator rit = v1.rbegin();rit != v1.rend(); rit++)
	{
		cout << *rit << " ";
	}
}

Addition, deletion, modification and query of vector

Add and remove operations at the end of the vector  

vector<int> vecInt;

vecInt.push_back(1); //Add an element at the end of the container

vecInt.push_back(3);  

vecInt.push_back(5);  

vecInt.push_back(7);

vecInt.push_back(9);

vecInt.pop_back(); //Remove the last element in the container

vecInt.pop_back();

//{5 ,7 ,9}  

Vector data access

vec.at(idx); //Return the data pointed by the index idx, if idx is out of bounds, an out_of_range exception will be thrown.

vec[idx]; //Return the data pointed by the index idx, when it is out of bounds, the operation will directly report an error

vector<int> vecInt; //Assume it contains 1 ,3 ,5 ,7 ,9

vecInt.at(2) == vecInt[2]; //5

vecInt.at(2) = 8; or vecInt[2] = 8;

vecInt contains 1, 3, 8, 7, 9 values

int iF = vector.front(); //iF==1

int iB = vector.back(); //iB==9

vector.front() = 11; //vecInt contains {11,3,8,7,9}

vector.back() = 19; //vecInt contains {11,3,8,7,19}

//vector 删除
void main36()
{
	vector<int> v1(10);
	for (int i = 0; i < 10; i++)
	{
		v1[i] = i + 1;
	}

	//区间删除
	v1.erase(v1.begin(), v1.begin() + 3);
	printV(v1);

	//根据元素的位置删除
	v1.erase(v1.begin());
	printV(v1);

	//根据元素的值
	v1[1] = 2;
	v1[3] = 2;
	printV(v1);

	for (vector<int>::iterator it = v1.begin(); it != v1.end();)
	{
		if (*it == 2)
		{
			it = v1.erase(it);//当删除迭代器所指向的元素的时候,erease删除函数会让it自动下移动
		}
		else
		{
			it++;
		}
	}
	printV(v1);

	v1.insert(v1.begin(), 100);
	v1.insert(v1.end(), 200);
	printV(v1);
}

deque container

  • Deque is the abbreviation of "double-ended queue". Like vector, it is an STL container. Deque is a double-ended array, while vector is single-ended.
  • Deque is very similar to vector in interface, and can be directly replaced in many operations.
  • Deque can randomly access elements (supports direct access to index values, using the [] operator or at() method, which will be discussed in detail later).
  • Both adding and removing elements from the head and tail of the deque are very fast. But inserting elements or removing elements in the middle is more time-consuming.

Default construction of deque objects

Deque is implemented by a template class, and the default construction form of a deque object is: deque<T> deqT;  

deque <int> deqInt; //A deque container for storing int.

deque <float> deq Float; //A deque container storing float.

deque <string> deq String; //A deque container to store string.

 //The pointer type or custom type can also be set in the angle brackets.

Add and remove operations at the end of the deque

  • deque.push_back(elem); //Add a data at the end of the container
  • deque.push_front(elem); //Insert a data at the head of the container
  • deque.pop_back(); //Delete the last data in the container
  • deque.pop_front(); //Delete the first number of the container

Deque data access

  • deque.at(idx); //Return the data pointed by the index idx, if idx is out of bounds, throw out_of_range.
  • deque[idx]; //Return the data pointed by the index idx, if idx is out of bounds, no exception will be thrown, and an error will be made directly.
  • deque.front(); //Return the first data.
  • deque.back(); //return the last data
void main41()
{
	deque<int> d1;
	d1.push_back(1);
	d1.push_back(3);
	d1.push_back(5);

	d1.push_front(-11);
	d1.push_front(-33);
	d1.push_front(-55);
	printD(d1);

	cout << endl;
	cout << "头部元素:" << d1.front() << endl;
	cout << "尾部元素:" << d1.back() << endl;
	
	d1.pop_front();
	d1.pop_back();
	printD(d1);
	cout << endl;

	//查找-33在数组下标的值
	deque<int>::iterator it = find(d1.begin(), d1.end(), -33);
	if (it != d1.end())
	{
		cout << "-33数组下标是" << distance(d1.begin(), it) << endl;
	}
	else
	{
		cout << "没有找到值为-33的元素" << endl;
	}

}

 stack container

  • stack is a stack container, which is a "first in, last out" container.
  • The stack simply decorates the deque container to become another kind of container.

The default construction of the stack object

The stack is implemented by a template class, and the default construction form of the stack object is: stack <T> stkT;  

stack <int> stkInt; //A stack container for storing int.

stack <float> stkFloat; //A stack container for storing floats.

stack <string> stkString; //A stack container for storing string.

//The pointer type or custom type can also be set in the angle brackets.

//栈模型
//栈的算法和栈容器数据类型的分离
void main51()
{
	stack<int> s;

	//入栈
	for (int i = 0; i < 10; i++)
	{
		s.push(i + 1);
	}
	cout << "栈的大小" << s.size() << endl;

	//出栈
	while (!s.empty())
	{
		//获取栈顶元素
		int tmp = s.top();
		cout << tmp << " ";
		s.pop();//弹出栈顶元素
	}

}

stack's push() and pop() methods

stack.push(elem); //Add elements to the stack head

stack.pop(); //Remove the first element from the stack head

Copy construction and assignment of stack objects

stack(const stack &stk); //copy constructor

stack& operator=(const stack &stk); // overloaded equal sign operator

Stack traversal

void main52()
{
	Teacher5 t1, t2, t3;
	t1.age = 31;
	t2.age = 32;
	t3.age = 33;

	stack<Teacher5> s;
	s.push(t1);
	s.push(t2);
	s.push(t3);

	while (!s.empty())
	{
		Teacher5 tmp = s.top();
		tmp.prinT();
		s.pop();
	}
}

queue container

  • queue is a queue container, which is a "first in, first out" container.
  • Queue simply decorates the deque container to become another kind of container.

The queue is implemented by a template class, and the default construction form of the queue object is: queue<T> queT; such as:

queue<int> queInt; //A queue container for storing int.

queue<float> queFloat; //A queue container for storing floats.

queue<string> queString; //A queue container for storing strings.

//The pointer type or custom type can also be set in the angle brackets.

Initialization and traversal of the queue container

//队列中基本数据类型
void main61()
{
	queue<int> q;
	q.push(1);
	q.push(2);
	q.push(3);

	cout << "队头元素" << q.front() << endl;
	cout << "队列的大小" << q.size() << endl;
	
	while (!q.empty())
	{
		int tmp = q.front();
		cout << tmp << " ";
		q.pop();
	}
}

list container

  • list is a doubly linked list container that can efficiently insert and delete elements.
  • The list cannot randomly access elements, so the at.(pos) function and the [] operator are not supported. It++(ok) it+5(err)

The list is implemented using a template class, and the default construction form of the object is: list<T> lstT; such as:

list<int> lstInt; //Define a list container for storing int.

list<float> lstFloat; //Define a list container for storing floats.

list<string> lstString; //Define a list container for storing strings.

//The pointer type or custom type can also be set in the angle brackets.

Add and remove operations at the head and tail of the list

  • list.push_back(elem); //Add an element at the end of the container
  • list.pop_back(); //Delete the last element in the container
  • list.push_front(elem); //Insert an element at the beginning of the container
  • list.pop_front(); //Remove the first element from the beginning of the container

List data access

  • list.front(); //Returns the first element.
  • list.back(); // Return the last element.
void main71()
{
	list<int> l;
	cout << "list的大小:" << l.size() << endl;
	for (int i = 0; i < 10; i++)
	{
		l.push_back(i);//尾部插入元素 尾插法
	}
	cout << "list的大小:" << l.size() << endl;

	list<int>::iterator it = l.begin();

	while (it != l.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
	//lsit不能随机访问
	it = l.begin();
	it++;
	it++;
	it++;
	//it += 5; 不支持随机的访问容器
	l.insert(it, 100);//100插入在什么位置
	
	for (list<int>::iterator it = l.begin(); it != l.end(); it++)
	{
		cout << *it << " ";
	}

	//结论1:链表的结点的index序号是从0号位置开始
	//结论2:在3号位置插入元素,是让原来的3号位置变成4号位置 原来的4号位置变成5号位置
	
}

Insertion of list

  • list.insert(pos,elem); //Insert a copy of the elem element at pos, and return the position of the new data.
  • list.insert(pos,n,elem); //Insert n elem data at position pos, no return value.
  • list.insert(pos,beg,end); //Insert the data in [beg,end) range at position pos, no return value.

list deletion

  • list.clear(); //Remove all data in the container
  • list.erase(beg,end); //Delete the data in the interval [beg,end), and return the position of the next data.
  • list.erase(pos); //Delete the data at position pos and return the position of the next data.

lst.remove(elem); //Delete all elements in the container that match the value of elem

//list删除
void main72()
{
	list<int> l;
	cout << "list的大小:" << l.size() << endl;
	for (int i = 0; i < 10; i++)
	{
		l.push_back(i);//尾部插入元素 尾插法
	}
	cout << "list的大小:" << l.size() << endl;

	list<int>::iterator it = l.begin();

	while (it != l.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
	list<int>::iterator it1 = l.begin();
	list<int>::iterator it2 = l.begin();
	it2++;
	it2++;
	it2++;


	l.erase(it1, it2);
	it = l.begin();

	while (it != l.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;

	l.insert(l.begin(), 100);
	l.insert(l.begin(), 100);
	l.insert(l.begin(), 100);

	it = l.begin();

	while (it != l.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
	l.erase(l.begin());

	it = l.begin();

	while (it != l.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
	l.remove(100);

	it = l.begin();

	while (it != l.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
}

 

Guess you like

Origin blog.csdn.net/qq_45526401/article/details/130183977