[Characteristics] List STL container (list), the insertion, deletion, assignment, data storage, examples inverted +

list linked list container

Here Insert Picture Description

First, list properties

(1) is a chain composed of a series of nodes, the node contains two fields, a data field, a pointer field.
(2) the list of non-continuous memory, and therefore add or delete elements, the time complexity is a constant term, without moving the element array than the addition of high efficiency deleted.
(3) the list only when needed, just allocate memory.
(4) list, just to get the first node, to get the equivalent of the entire list.
Relationship (5) list additional space is needed to save node: predecessor, successor relationship.

Second, the difference between arrays and linked lists:

(1) must be defined array of fixed length (number of elements), the case can not adapt to dynamic changes in the data. When the data is increased, the number of elements may exceed the previously defined; when the data is reduced, resulting in wasted memory.
(2) list dynamically allocated storage, the data can be dynamically increased or decreased adaptation, and can be easily inserted, deleted data elements. (When you insert an array, deleting data items need to move other items)

Three, list the container (list) various operations

(1) Operation Function Definition:

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture Description
sort of container that supports random access

(2) Key operation example

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

void printlist(list<int>& v) {
	for (list<int>::iterator it = v.begin(); it != v.end(); ++it) {
		cout << *it << " ";
	}cout << endl;
}

//初始化
void test01() {
	list<int>mlist1;
	list<int>mlist2(10, 10);//有参构造用十个10去初始化
	list<int>mlist3(mlist2);//拷贝构造
	list<int>mlist4(mlist2.begin(), mlist2.end());
	printlist(mlist4);
}

//list容器插入、删除
void test02() {
	list<int>mlist;
	mlist.push_back(100);
	mlist.push_front(200);
	mlist.push_front(200);
	mlist.push_front(200);
	//插入
	mlist.insert(mlist.begin(), 300);
	mlist.insert(mlist.begin(), 200);
	mlist.insert(mlist.end(), 400);

	list<int>::iterator it = mlist.begin();
	//it+2  //这样表示是错误,只能说给it两次++;
	it++; it++;
	mlist.insert(it, 500);
	printlist(mlist);

	//删除
	mlist.remove(200);//删除数据中的所有200;
	printlist(mlist);

	mlist.pop_back();//删尾
	mlist.pop_front();//删头
	mlist.erase(mlist.begin(), mlist.end());//从开始到结尾删除相当于 :mlist.clear();
	printlist(mlist);
}
//赋值初始化
void test03() {
	list<int>mlist;
	mlist.assign(10, 10);//用十个10初始化
	printlist(mlist);

	list<int>mlist2;
	mlist2 = mlist;//对mlist来初始化
	printlist(mlist2);//10 10 10 10 10 10 10 10 10 10

	list<int>mlist3;
	mlist3.assign(5, 8); //8 8 8 8 8
	mlist.swap(mlist3);//将mlist3的数据和mlist 的数据互相交换
	printlist(mlist);//8 8 8 8 8
	printlist(mlist3);//10 10 10 10 10 10 10 10 10 10
}

//翻转
void test04() {
	list<int>mlist;
	for (int i = 0; i < 10; ++i) {
		mlist.push_back(i);
	}
	printlist(mlist);//0 1 2 3 4 5 6 7 8 9
	mlist.reverse();//实现链表的翻转
	printlist(mlist);//9 8 7 6 5 4 3 2 1 0
}
//排序
bool mycompare(int v1, int v2);
void test05() {
	list<int>mlist;
	mlist.push_back(1);
	mlist.push_back(7);
	mlist.push_back(5);
	mlist.push_back(3);
	printlist(mlist);//1 7 5 3
	mlist.sort();//默认从小到大
	printlist(mlist);//1 3 5 7

	mlist.sort(mycompare);//要想实现从大到小,那么加入规则即可
	printlist(mlist);//7 5 3 1
}
bool mycompare(int v1, int v2) {
	return v1 > v2;
}
int main(void) {
	//test01();
	//test02();
	//test03();
	//test04();
	test05();
	return 0;
}

The list function is very powerful, if fully mastered, whether it is to achieve some of the functions or we do face questions of time will make us more effective, to make the code look cleaner, clearer logical expression.

Published 57 original articles · won praise 28 · views 4127

Guess you like

Origin blog.csdn.net/weixin_41747893/article/details/102860606