C++ study notes eight-sequential container vector list deque

1. What is a sequential container?
The elements in the sequence container are stored and accessed according to their location . In addition to sequential containers, the standard library also defines several associative containers whose elements are sorted by keys. For example, the standard library vector type is a sequential container. It gathers single type elements into a container, and then stores and accesses these elements according to their positions. This is the sequential container. The standard library defines three types of sequential containers: vector list and deque.
The following are several types of sequential containers

vector // 支持快速随机访问
list  //支持快速删除插入
deque //双端队列

1.1 Sequential container definition
When using various containers, the relevant header files must be included first

# include<vector>
# include <list>
# include<deque>

All containers are class templates. To define a special container, you must add a pair of angle brackets after the container name. The angle brackets provide the type of elements stored in the container. As defined below

vector<int> a;
list<string> b;
deque<double> items;

1.2 Initialization of elements in the container

vector<int> c;//创建一个名为c的vector容器,用于存放int 类型数据
vector<int> c(b);//创建容器b的副本c,c和b必须具有相同的容器类型,并存放相同类型的元素,适用于所有容器。
vector<int> c(n,t);  //用n个值为t的元素创建容器C,其中值t必须是容器c的元素类型的值。
vector<int> c(n);创建有n个值初始化元素容器C(只适用于顺序容器)

1.3 Initialization as a copy of a section of elements
Although it is not possible to directly assign elements in one type of container to another type of container, the system allows indirect implementation of functions by passing a pair of iterators.
The iterator marks the range of elements to be copied, and these elements are used to initialize the elements of the new container. The iterator marks the first and last element to be copied.

list<string> slist(svec.begin(),svec.end());//svec为已经定义好的容器
int a[]={
    
    0,1,2,3,4,5}
vector<int> s(a,a+3);//s[0]=0,s[1]=1,s[2]=2,s[3]=3;

1.4 Iterators and the scope
of iterators The size_type and iterator type iterators have been mentioned in previous articles. All container iterators support reading an element from the container by dereferencing operation. The following are commonly used iterator operations

*iter //返回迭代器iter所指向的元素引用
++iter //给iter加1,使其指向容器内下一个元素
--iter //给iter减1,使其指向容器的前一个元素

The iterator of the list container supports neither arithmetic operations (addition or subtraction) nor relational operations (<=,<,>=). It only provides pre- and post-increment, decrement, and equality operations.
There is a problem in c++primer. Please output the elements in the list container in reverse order.
If you want to try the following output

int a[] = {
    
    1,2,3,4,5,6};
	list<int> test(a, a + 2);
	int c = test.size();
	list<int>::iterator first = test.begin();
		while (first != test.end())
		{
    
    
			int i = 0;
			c = c - i;
			cout << *(first + c);
			first++;
			i++;
		}

Then an error will be reported. The reason for the error has also been mentioned. The iterator of the list cannot be added and subtracted. The correct way is as follows

#include <iostream>
# include <string>
# include <vector>
# include <list>
using namespace std;
int main()
{
    
    
	int a[] = {
    
    1,2,3,4,5,6};
	vector<int> han(a,a+2);
	list<int> test(a, a + 6);
	list<int> ::iterator  first= test.begin();
	list<int> ::iterator  end = test.end();
	while (first != end)
	{
    
    
		cout << "the result is" << *(--end) << endl;
	}

}

The result of the operation is shown in Figure
Insert picture description here
1.5. The begin() and end() members of the sequential container

c.begin() //返回一个迭代器,他指向容器c的第一个元素
c.front() //返回一个元素值,第一个元素的引用u
c.back() //返回容器c的最后一个元素的引用,最后一个元素的值,可以直接输出
c.end() //返回一个迭代器,它指向元素最后一个位置的下一个位置
c.at(0)//引用第一个元素,除了下标运用外也可以采取该方式

Adding elements to the sequence container mentioned the application before: push_back, all sequence containers support push_back, providing the function of inserting an element at the end of the container.

c.push_back(t) //在容器c的尾部添加值为t的元素,返回void类型
c.push_front(t) //在容器c的前端添加值为t的元素,返回void类型
//只适用于list 和 deque容器类型

c.insert(p,t) //在迭代器p所指向的元素面前插入值为t的新元素。返回指向新添加元素的迭代器
c.insert(p,n,t)//在迭代器p所指向的元素面前插入n个值为t的元素,返回void类型
//插入一段元素
c.insert(p,b,e)//在迭代器P所指向的元素面前插入有迭代器b和e标记范围内的元素。返回void

1.6 Container size operation

//顺序容器大小操作
c.size()// 返回容器中元素个数,返回的类型为 c::size_type
c.max_size() //返回容器c可容纳的最多元素个数,返回的类型为 c::size_type
c.resize(n) //调整容器c的长度大小,使其容纳n个元素,如果n<c.size(),则删除多余的元素。否则,添加采用值初始化的新元素。
c.empty() //返回标记容器大小是否为0的布尔值 

1.7 Delete elements

c.erase(p) //删除迭代器p所指向的元素,返回一个迭代器,指向被删除元素后面的元素,如果p本身就是指向超出末端的下一位置的迭代器,则该函数未定义
c.erase(b,e) //删除迭代器b和e所标记的范围内所有的元素,返回一个迭代器
c.clear() //删除容器内所有元素,返回void
c.pop_back(); //删除容器内最后一个元素
c.pop_front() //删除容器c的第一个元素。返回void

Guess you like

Origin blog.csdn.net/qq_41803340/article/details/108815887