List Detailed c ++ STL container in

List Detailed c ++ STL container in

I. Summary

List is a doubly linked list container, List container to quickly remove an added element anywhere.

Interface similar to interface to other containers List container, the container can not List random data access elements (such as: or use by the subscript at () methods are not acceptable), data access is necessary to mention me because I before also do not understand, I asked the teacher know today, the data is stored into the data store, modify its value, whichever is to get it out, what values ​​stored here can be carried out using an iterator ++ operation, but can not be + 3, + 4 operation, when use must contain the same header files and deque container operations I just go over knowledge, it is a simple way, not going deep talk, there is a need I can see Detailed issued before the container deque

Two, List container constructor

//List 容器默认构造函数和带参数的构造函数
void demo1(void) {
	//List默认构造函数
	list<int> listInt;
	cout<<"listInt默认构造函数的元素个数:" << listInt.size() << endl;
	//List带参数的构造函数

	list<int> listIntA(10,666);
	cout << "listIntA带参数的构造函数的元素个数:" << listIntA.size() << endl;
	
	vector<int> vectInt;
	vectInt.capacity();

	//List 容器没有capacity() 容量函数 不存在预先分配空间
	//List 使用迭代器访问元素
	for (list<int>::iterator it = listIntA.begin(); it != listIntA.end(); it++) {
		cout <<"listIntA带参构造函数的元素:"<< (*it) << endl;
    }

}

operation result:
Here Insert Picture Description

Third, the container of the head and tail List add deletion and access data

//List 容器的头尾添加删除操作和数据的存取
void demo2(void) {
	list<int> listInt;

	//在尾部追加元素
	listInt.push_back(1);
	listInt.push_back(2);
	listInt.push_back(3);
	listInt.push_back(4);
	listInt.push_back(5);
	//在头部删除元素
	listInt.pop_front();
	listInt.pop_front();
	//在尾部删除元素
	listInt.pop_back();
	listInt.pop_back();
	//头部添加元素
	listInt.push_front(11);
	listInt.push_front(12);
	//使用迭代器遍历打印
	for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++) {
		cout << "listInt添加删除过后:" << (*it) << endl;
	}
	cout << "------------数据存取------------" << endl;
	//数据的存取
	listInt.back() = 14;//listInt的最后一个元素赋值为14
	listInt.front() = 16;//listInt的前面第一个元素赋值为16
	for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++) {
		cout << "listInt数据存取过后:" << (*it) << endl;
	}
}

operation result:

Here Insert Picture Description

Four, List container and iterator

//List 容器与迭代器
void demo3(void) {
	list<int> listInt;

	//这里与deque相似可以看我以前发的deque容器详解,这里只是回顾一下以前的知识.
	listInt.push_back(1);
	listInt.push_back(2);
	listInt.push_back(3);
	listInt.push_back(4);
	listInt.push_back(5);

	//使用普通的迭代器遍历打印
	for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++) {
		cout << "listInt的元素:" << (*it) << endl;
	}
	cout << " -----------------常量迭代器---------------------" << endl;
	//使用常量迭代器遍历打印
	for (list<int>::const_iterator cit = listInt.cbegin();cit != listInt.cend(); cit++) {
		cout << "listInt的元素:" << (*cit) << endl;
	}

	cout << " -----------------逆转迭代器---------------------" << endl;
	//使用逆转迭代器遍历打印
	for (list<int>::reverse_iterator rit = listInt.rbegin(); rit != listInt.rend(); rit++) {
		cout << "listInt的元素:" << (*rit) << endl;
	}

}

operation result:
Here Insert Picture Description

V. List assignment and size of container

//List 容器的赋值和大小
void demo4(void) {
	list<int> listInt,listIntA,listIntB,listIntC;

	listInt.push_back(1);
	listInt.push_back(2);
	listInt.push_back(3);
	listInt.push_back(4);
	listInt.push_back(5);

	//第一种
	//指定区间,listInt.begin()到listInt.end()的元素,赋值给listIntB
	//注意:包括listInt.begin()但不包括listInt.end()指向的元素(左闭右开),
	listIntB.assign(listInt.begin(), listInt.end());
	
	for (list<int>::iterator it = listIntB.begin(); it != listIntB.end(); it++) {
		//打印结果:1,2,3,4,5 listIntB.end() 不是指向的5,是指向的5后面的元素
		cout << "listIntB的元素:" << (*it) << endl;
	}
	//也可以用前置++ 前置--
	listIntB.assign(++listInt.begin(), --listInt.end());
	
	for (list<int>::iterator it = listIntB.begin(); it != listIntB.end(); it++) {
		//打印结果: 2,3,4
		cout << "listIntB使用前置--++赋值的元素:" << (*it) << endl;
	}

	//第二种,
	//指定元素个数和元素值
	listIntC.assign(4,666);
	for (list<int>::iterator it = listIntC.begin(); it != listIntC.end(); it++) {
		//打印结果: 666,666,666,666,
		cout << "listIntC使用是指定元素个数和元素值赋值:" << (*it) << endl;
	}

	//第三种 使用赋值构造函数
	listIntA = listIntC;

	for (list<int>::iterator it = listIntA.begin(); it != listIntA.end(); it++) {
		//打印结果: 666,666,666,666,
		cout << "listIntA使用赋值构造函数构造listIntC的元素:" << (*it) << endl;
	}


	cout << "---------------list容器的大小--------------" << endl;
	//这里与deque相似可以看我以前发的deque容器详解,这里只是回顾一下以前的知识.

	list<int> listIntD;

	listIntD.push_back(1);
	listIntD.push_back(2);
	listIntD.push_back(3);
	
	if (!listIntD.empty()) {//empty
		int size = listIntD.size();//size=3
		listIntD.resize(2);  //resize函数是重新定义元素个数//1,2
		listIntD.resize(5);//1,2,0,0,0
		listIntD.resize(7, 22);//1,2,0,0,0,22,22
	}

	for (list<int>::iterator it = listIntD.begin(); it != listIntD.end(); it++) {
		//打印结果://1,2,0,0,0,22,22
		cout << "listIntD的元素:" << (*it) << endl;
	}

}

operation result:
Here Insert Picture Description

Six, list container insertions and deletions, as well as in reverse order

//list 容器的插入和删除,还有反序排列
void demo5(void) {
	
	list<int> listInt;
	list<int> listIntA(4, 333);

	listInt.push_back(1);
	listInt.push_back(2);
	listInt.push_back(3);
	listInt.push_back(4);
	listInt.push_back(5);
	cout << "-----------------------元素的插入----------------" << endl;
	//这里与deque相似可以看我以前发的deque容器详解,这里只是回顾一下以前的知识.
	//list容器的插入
	//第一个参数常常是需要插入的迭代器位置
	cout << "              第一种 " << endl;
	//第一种: 指定区间插入  注意:左闭右开
	listInt.insert(listInt.begin(), listIntA.begin(), listIntA.end());

	for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++) {
		cout << "listInt的元素:" << (*it) << endl;
	}

	cout << "              第二种 " << endl;
	//第二种:指定元素个数和元素值 在2的位置插入2个666
	listInt.insert(++listInt.begin(),2,666);

	for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++) {
		cout << "listInt的元素:" << (*it) << endl;
	}

	cout << "              第三种 " << endl;
	//第三种:指定元素值 在最后的位置插入一个6
	listInt.insert(listInt.end(), 6);
	for (list<int>::iterator it = listInt.begin(); it != listInt.end(); it++) {
		cout << "listInt的元素:" << (*it) << endl;
	}

	cout << "--------------元素的删除------------------" << endl;
	list<int> listIntB;

	listIntB.push_back(1);
	listIntB.push_back(2);
	listIntB.push_back(3);
	listIntB.push_back(4);
	listIntB.push_back(5);
	listIntB.push_back(6);
	listIntB.push_back(7);


	cout << "              第一种 " << endl;
	//这里与deque容器有些不一样
	//list容器 迭代器只能++或--不能执行+3,+4操作
	list<int>::iterator begin = listIntB.begin();
	list<int>::iterator end = listIntB.end();
	--end;
	--end;
	++begin;
	++begin;
	//第一种: 指定区间删除  注意:左闭右开
	//begin迭代器指向容器的第三个元素end指向容器的倒数第二个元素
	//begin到end的元素删除 注意:左闭右开
	listIntB.erase(begin,end);//erase()函数返回值是下一个位置的迭代器

	for (list<int>::iterator it = listIntB.begin(); it != listIntB.end(); it++) {
        //打印结果: 1,2 ,6,7,
		cout << "listIntB的元素:" << (*it) << endl;
	}

	cout << "           第二种" << endl;
	//第二种: 指定位置删除
	//把容器的第一元素删除
	listIntB.erase(listIntB.begin());
	for (list<int>::iterator it = listIntB.begin(); it != listIntB.end(); it++) {
		//打印结果: 2 ,6,7,
		cout << "listIntB的元素:" << (*it) << endl;
	}

	cout << "            第三种 " << endl;

	listIntB.push_back(4);
	listIntB.push_back(4);
	listIntB.push_back(4);
	listIntB.push_back(4);

	//第三种:删除指定元素
	//会把元素值相同的全部删掉
	listIntB.remove(4);//rmemove()方法

	for (list<int>::iterator it = listIntB.begin(); it != listIntB.end(); it++) {
		//打印结果:还是 2 ,6,7,
		cout << "listIntB的元素:" << (*it) << endl;
	}
	cout << "            第四种 " << endl;
	//第四种:迭代器遍历删除
	//迭代器遍历删除6这个元素
	for (list<int>::iterator it = listIntB.begin(); it != listIntB.end();) {
		if (*it == 6) {
			//erase()函数返回值是下一个位置的迭代器,这时it已经指向了一个位置;
			it = listIntB.erase(it); 
		}
		else {
			cout << "listIntB的元素:" << (*it) << endl;
			//erase()函数返回值是下一个位置的迭代器只有不经过上面的if()
			it++;//it++如果放在上面不行,越界了
		
		}
	
	}

	cout << "--------------反序排列---------------" << endl;
	listIntB.push_back(1);
	listIntB.push_back(2);
	listIntB.push_back(3);
	listIntB.push_back(4);
	//reverse() 函数可以逆转链表如1,2,3,4,5,用reverse()函数之后就是5,4,3,2,1,
	listIntB.reverse(); 
	for (list<int>::iterator it = listIntB.begin(); it != listIntB.end(); it++) {
		
		cout << "listIntB的元素:" << (*it) << endl;
	}
}

The result:
Here Insert Picture Description
seven at the end

deque containers do not forget to write there, and all of this in the comments in the code.

Released six original articles · won praise 0 · Views 61

Guess you like

Origin blog.csdn.net/qq_45569601/article/details/104544645
Recommended