list简析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Zhang_1218/article/details/79144515

list

  1. 简介

上篇博客简单解析了vector的应用,这篇则要对list进行解析。

list与vector一样,都是C++标准函数库的内容,其两者可以说都是容器。

API中对与list的定义如下:

Lists are a kind of sequence container. As such, their elements are ordered following a linear sequence.

翻译如下:列表是一种序列容器。 因此,它们的元素按照线性顺序排列。

简单来说,list就是一个链表,一个双向循环链表不同于其他链表的是,list是一个序列容器,即其可包含任意类型的数据。

list中提供的成员函数与vector类似,不过list提供了对表于首元素的操作push_front、pop_front,这是vector不具备的。

来看一下list中的成员函数:

image.png

从上图中,可以看到对于list中数据的操作,包含有迭代器(iterator),数据修改,容量获取等等。下面我就针对list的使用以及上面的部分成员函数进行简单的解析。

  1. 2.应用

  2. list与vector一样,在应用时,都要引入头文件,如果要应用list的话,就需要引入

  3. #include<list>
    

    同时,list属于std命名空间,因此运用是需要进行命名限定,以下即为限定方式:

std::list<type> test;
using namespace std;

    接下来,我简单介绍一下list其内部接口的作用:

    begin():返回list首部元素迭代器

    end():返回list尾部元素下一个位置的迭代器

    rbegin():反向迭代器,返回的是list尾部元素的迭代器

    rend():反向迭代器,返回的是list首部元素前一个位置的迭代器

    empty():判断list是否为空

    size():获取list中的数据容量

    resize():更新list的内存容量,并且改变其内的数据容量 

    reverse():逆置list中的元素

    front():返回list的首元素

    back():返回list的尾元素

    push_front():头插元素

    pop_front():删除头元素

    push_back():尾插元素

    pop_back():删除尾部元素

    insert():指定位置插入元素

    erase():指定删除元素

    swap():交换list中数据

    clear():清空list中的数据

    assign():给list赋值

    sort():给list中的数据进行排序

    splice():将两个list进行合并

    unique():删除list中重复出现的元素

 以下为测试代码:

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

void print(const list<int> l)
{
	list<int>::const_iterator it = l.begin();

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

	cout << endl;
}

void EraseTest(list<int> l)
{
	//删除单个节点erase
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		if (*it % 2 == 0)
			it = l.erase(it);//迭代器失效,需要接受返回值
		else
		{
			it++;
		}
	}
	print(l);
}

void ResizeTest(list<int>& l)
{
	l.resize(5);
	cout << "链表中的字符数量:" << l.size() << endl;
	print(l);

	//resize 增容时还会用缺省值进行初始化
	l.resize(10);
	cout << "链表中的字符数量:"<< l.size() << endl;
	print(l);

	//resize增容之后,再进行尾插时会直接连接在最后面,因为前面的已经被resize进行了初始化
	for (size_t i = 1; i < 4; i++)
	{
		l.push_back(i);
	}
	cout << "链表中的字符数量:" << l.size() << endl;
	print(l);

	l.resize(15, 100);
	cout << "链表中的字符数量:" << l.size() << endl;
	print(l);


}

void InsertTest(list<int>& l)
{
	list<int>::iterator it = l.begin();
	l.insert(it, -1);
	print(l);
	l.insert(it, 3,-2);
	print(l);
}

void listTest()
{
	list<int> l1;
	l1.push_back(1);
	l1.push_back(2);
	l1.push_back(3);
	l1.push_back(4);
	l1.push_back(5);
	l1.push_back(6);
	cout << "链表中的字符数量:" << l1.size() << endl;
	print(l1);

	list<int>::const_iterator it = l1.begin();


	EraseTest(l1);
	ResizeTest(l1);
	cout << l1.empty() << endl;
	cout << l1.front() << endl;
	cout << l1.back() << endl;
	
	l1.push_front(0);
	print(l1);
	cout << "链表中的字符数量:" << l1.size() << endl;
	l1.pop_back();
	cout << "链表中的字符数量:" << l1.size() << endl;
	l1.pop_front();
	cout << "链表中的字符数量:" << l1.size() << endl;
	
	InsertTest(l1);
	l1.sort();
	print(l1);
}

程序运行结果如下所示:

image.png




猜你喜欢

转载自blog.csdn.net/Zhang_1218/article/details/79144515