list的总结

目录

1.什么是list

1.1list 的优势和劣势

优势:

劣势:

2.构造函数 

2.1  default (1)

2.2  fill (2)

2.3  range (3)

2.4  copy (4)

3.list iterator的使用

3.1. begin()

3.2. end()

3.3迭代器遍历

4. list容量函数

4.1. empty()

4.2. size()

4.3. max_size()

4.4. front()

4.5. back()

5.list增删查改函数 

5.1 push_front

5.2 pop_front 

5.3  push_back

5.4 pop_back

 5.5 insert

5.6. erase

5.7  swap 

5.8 clear 

 6.list操作函数

6.1. splice

6.2. remove

6.3. unique

6.4. reverse

 7.list的迭代器失效


 追随光靠近光成为光

1.什么是list

在C++标准库中,list 是一个双向链表容器,用于存储一系列元素。与 vector 和 deque 等容器不同,list 使用链表的数据结构来组织元素,因此在某些操作上具有独特的优势和性能特点。以下是关于 list 的详细介绍

1. list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
2. list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向 其前一个元素和后一个元素。
3. list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高 效。
4. 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率 更好。
5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list 的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间 开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这 可能是一个重要的因素)

在使用list 时,需要权衡其优势和劣势,根据实际场景来选择合适的容器。当需要频繁插入和删除元素,且不需要随机访问时,list 可能是一个很好的选择。但需要注意的是,由于链表的特性,list 并不适用于需要快速随机访问元素的场景,因为访问某个位置的元素需要遍历链表

1.1list 的优势和劣势

优势:

插入和删除效率高: 由于 std::list 是双向链表,插入和删除操作在常数时间内完成,不需要涉及内存的重新分配和元素的复制。这使得 std::list 在大量插入和删除操作时非常高效。
迭代器的稳定性: std::list 的插入和删除操作不会使迭代器失效,除非删除的正是迭代器所指向的元素。这使得在遍历过程中进行插入和删除操作更加方便和安全。
空间占用相对稳定: std::list 的空间占用相对稳定,插入和删除操作不会影响其他元素的空间占用。

劣势:

不支持随机访问: 由于链表的结构,list 不支持像数组一样的随机访问。访问某个位置的元素需要从链表的开头或结尾开始遍历。
额外的指针开销:list 中的每个元素都需要存储指向前后元素的指针,这使得空间占用相对其他容器更高。
缓存效率低: 由于链表中元素在内存中的存储位置不连续,导致在访问链表元素时,缓存命中率较低,可能影响性能。
迭代器的使用限制:list 的迭代器不支持与普通指针类似的算术操作(如 + 和 -),因此无法像 vector 那样灵活地进行迭代器操作

2.构造函数 

2.1  default (1)

这个构造函数用于创建一个空的 std::list 容器。它可以接受一个可选的分配器参数,用于指定内存分配策略。

list<int> v; // 创建一个空的 list 容器

2.2  fill (2)

这个构造函数用于创建一个包含 n 个元素的list 容器,并将这些元素初始化为 val。你可以通过传递不同的 val 值来创建一个包含相同值的容器。同样,也可以传递一个可选的分配器参数。

list<int> v(10, 20); // 创建一个包含 10个元素,每个元素都是 20 的list 容器

2.3  range (3)

这个构造函数使用迭代器范围 [first, last) 中的元素创建一个list 容器。这使你可以通过一个迭代器范围来初始化容器。同样,它也接受一个可选的分配器参数。

vector<int> v = {1, 2, 3, 4, 5};
list<int> my(v.begin(), v.end()); // 从迭代器范围内的元素创建list 容器

2.4  copy (4)

这个构造函数用于创建一个与已存在的 list 容器 x 相同的副本。它会将 x中的所有元素拷贝到新的容器中。这是一个拷贝构造函数。

list<int> v = {1, 2, 3, 4, 5};
list<int> my(v); // 创建一个原容器的副本

3.list iterator的使用

3.1. begin()

iterator begin() noexcept;
这个版本的 begin() 返回一个迭代器,可以用于修改容器内的元素。noexcept 表示这个函数不会抛出异常。

list<int> m = {1, 2, 3, 4, 5};
list<int>::iterator it = m.begin(); // 获取可修改元素的迭代器
*it = 10; // 修改第一个元素的值为 10

3.2. end()

iterator end() noexcept;
这个版本的 end() 返回一个迭代器,可以用于修改容器内的元素。noexcept 表示这个函数不会抛出异常。这个迭代器指向的位置实际上是容器的末尾位置之后一个虚拟的位置,所以它并不指向容器内的任何元素。

list<int> m = {1, 2, 3, 4, 5};
list<int>::iterator it = m.end(); // 获取可修改元素的迭代器
--it; // 将迭代器前移一个位置,指向最后一个元素
*it = 20; // 修改最后一个元素的值为 20

3.3迭代器遍历

(list不支持[  ],  只能用迭代器遍历(范围for也可以底层是迭代器))

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


void mylist()
{
	list<int> m;

	m.push_back(1);
	m.push_back(2);
	m.push_back(3);
 
     //迭代器
	list<int>::iterator it = m.begin();
	while (it != m.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;

    //范围for
	for (auto e : m)
	{
		cout << e << " ";
	}
	cout << endl;

}


int main()
{
	mylist();
	return 0;
}

4. list容量函数

4.1. empty()


empty() 是 list 容器的一个成员函数,用于判断容器是否为空。它返回一个布尔值,表示容器是否不包含任何元素。函数声明如下:

bool empty() const noexcept;

返回值:如果容器为空,则返回 true,否则返回 false。

例子 

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

int main() {
    list<int> m;

    if (m.empty()) {
        cout << "The list is empty." << endl;
    }
    else {
        cout << "The list is not empty." << endl;
    }

    m.push_back(10);

    if (m.empty()) {
       cout << "The list is empty." << endl;
    }
    else {
        cout << "The list is not empty." << endl;
    }

    return 0;
}

4.2. size()

size() 是 list 容器的一个成员函数,用于返回容器中元素的数量。它返回一个无符号整数类型,表示容器中的元素数量。函数声明如下:

size_type size() const noexcept;

返回值:返回容器中元素的数量,即大小。

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

int main() {
   list<int> m;

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


    cout << "Size of the list: " << m.size() << endl;

    return 0;
}

4.3. max_size()


max_size() 是 list 容器的一个成员函数,用于返回容器可能容纳的最大元素数量,通常受到系统内存限制的影响。它返回一个无符号整数类型,表示容器的最大大小。函数签名如下:

size_type max_size() const noexcept;

返回值:返回容器可能容纳的最大元素数量。

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

int main() {
   list<int> m;

    cout << "Size of the list: " << m.max_size() << endl;

    return 0;
}

 

4.4. front()

front() 是 list 容器的成员函数,用于返回容器中第一个元素的引用。这个函数有两个版本,一个用于可修改容器的对象,另一个用于只读(const)容器的对象。函数的签名如下:

reference front();

reference:返回一个对容器中第一个元素的非常引用。

加了const是只读的不能被修改

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

int main() 
{
    list<int> m = {9,2,3,4,5,6};

   int& firstElement = m.front();

    cout << "First element: " << m.front() << endl;

    return 0;
}

 

4.5. back()


back() 是 list 容器的成员函数,用于返回容器中最后一个元素的引用。这个函数有两个版本,一个用于可修改容器的对象,另一个用于只读(const)容器的对象。函数的签名如下:

reference back();
reference:返回一个对容器中最后一个元素的非常引用。

加了const是只读的不能被修改

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

int main() 
{
    list<int> m = {9,2,3,4,5,6};

   int& firstElement = m.back();

    cout << "Last element: " << m.back() << endl;

    return 0;
}

 

5.list增删查改函数 

函数说明接口说明
push_front
在list首元素前插入值为val的元素
pop_front
删除list中第一个元素
push_back
在list尾部插入值为val的元素
pop_back
删除list中最后一个元素
insert
在list position 位置中插入值为val的元素
erase
删除list position位置的元素
swap
交换两个list中的元素
clear
清空list中的有效元素

5.1 push_front

push_front 是 list 容器的成员函数,用于在容器的开头插入一个新元素。

这个函数有两个版本:

void push_front (const value_type& val);:接受一个常量引用参数,会创建一个新元素并将参数的值拷贝到新元素中。

void push_front (value_type&& val);:接受一个右值引用参数,用于移动构造一个新元素。这样可以避免额外的拷贝操作,提高了效率。

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

int main()
{
    list<int> m;

    int value = 10;

    m.push_front(value); // Copy insert

    cout << "List contents:" << endl;
    for (const auto& num : m) {
        cout << num << " ";
    }
    cout << endl;

    
    m.push_front(20); // Move insert,右值引用,更简单高效

    cout << "List contents after move insert:" << endl;
    for (const auto& num : m) {
        cout << num << " ";
    }
    cout << endl;

    return 0;
}

 

5.2 pop_front 

void pop_front(); 是用于从 ist 的开头移除一个元素的成员函数。它会删除列表中的第一个元素,并且将列表的大小减小一个单位。

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

int main()
{
    list<int> m = {10,20,30,40,50};

    m.pop_front();

    for (auto e : m)
    {
        cout << e << " ";
    }
    cout << endl;


    return 0;
}

5.3  push_back

void push_back (const value_type& val); 是 list 容器的成员函数,用于在列表的末尾插入一个新元素。它接受一个常量引用作为参数,将传入的值插入到列表末尾

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

int main()
{
    list<int> m;

    m.push_back(2);
    m.push_back(4);
    m.push_back(6);
    m.push_back(8);
    m.push_back(10);


    for (auto e : m)
    {
        cout << e << " ";
    }
    cout << endl;


    return 0;
}

5.4 pop_back

void pop_back(); 是 list 容器的成员函数,用于删除列表中的最后一个元素。它会将列表的最后一个元素从容器中移除,同时释放相应的内存资源。

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

int main()
{
    list<int> m;

    m.push_back(2);
    m.push_back(4);
    m.push_back(6);
    m.push_back(8);
    m.push_back(10);

    m.pop_back();
    m.pop_back();


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

    return 0;
}

 

 5.5 insert

在list position 位置中插入值为val的元素

iterator insert (iterator position, const value_type& val); 是 list 容器的成员函数,用于在指定位置插入一个新元素,新元素的值由 val 参数确定。

参数说明:

position:要插入新元素的位置的迭代器。
val:要插入的元素的值。
该函数返回一个迭代器,指向插入的元素。

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

int main()
{
    list<int> m = {1,2,3,4,5};

    auto a = m.begin();

    a++;   //begin的下一个位置,第二个位置

    m.insert(a,10);



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

    return 0;
}


 

5.6. erase

删除list position位置的元素


iterator erase (iterator position); 和 iterator erase (iterator first, iterator last); 是 std::list 容器的成员函数,用于从列表中删除一个或多个元素。

iterator erase (iterator position); 删除指定位置的元素,并返回指向下一个元素的迭代器。

参数说明:

position:要删除的元素的位置的迭代器。
返回值:指向被删除元素之后的元素的迭代器。

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

int main()
{
    list<int> m = {1,2,3,4,5};

    auto a = m.begin();

    a++;   //begin的下一个位置,第二个位置

    m.erase(a);
}

5.7  swap 

交换两个list中的元素

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

int main()
{
    list<int> m = {1,2,3,4,5};
    list<int> n = { 6,7,8,9,0};

    m.swap(n);


    list<int>::iterator it = m.begin();
    cout << "m:";;

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


    list<int>::iterator is = n.begin();
    cout << "n:";

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


    return 0;
}

5.8 clear 

清空list中的有效元素

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

#include <iostream>
#include <list>

int main() {
   list<int> m = { 1, 2, 3, 4, 5 };

    cout << "m before clear: ";
    for (int num : m) {
        cout << num << " ";
    }
    cout << endl;

    m.clear(); // 清空列表

  cout << "m after clear: ";
    for (int num : m) {
        cout << num << " ";
    }
    cout << endl;

    return 0;
}

 6.list操作函数

6.1. splice

void splice (iterator position, list& x);
该成员函数用于将另一个列表 x 中的所有元素移动到当前列表中,插入到指定位置 position 前。x 列表在移动后会变为空列表。

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


int main() {
    list<int> m1 = { 1, 2, 3 };
    list<int> m2 = { 4, 5, 6 };

    auto it = m1.begin();
    advance(it,1); 

    m1.splice(it, m2); // 将 m2 的元素插入到 m1 中

    cout << "m1 after splice: ";
    for (int num : m1) {
        cout << num << " ";
    }
    cout << endl;

    cout << "m2 after splice: ";
    for (int num : m2) {
        cout << num << " ";
    }
     cout << endl;

    return 0;
}

6.2. remove

void remove (const value_type& val);
该成员函数用于从列表中移除所有等于给定值 val 的元素。

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


int main() {
    list<int> m = { 1, 2, 3, 2, 4, 2, 5 };

    m.remove(2); // 移除列表中所有值为 2 的元素

    cout << "m after remove: ";
    for (int num : m) {
        cout << num << " ";
    }
    cout << endl;

    return 0;
}

 

6.3. unique

void unique();
这个成员函数用于移除列表中相邻的重复元素。它只保留第一个出现的重复元素,移除后续的重复元素。

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


int main() {
    list<int> m = { 1, 2, 2, 2, 3, 4,4, 5 };

    m.unique( ); // 移除列表中所有值为 2 的元素

    cout << "m after remove: ";
    for (int num : m) {
        cout << num << " ";
    }
    cout << endl;

    return 0;
}

 

6.4. reverse

void reverse(); 函数用于将列表中的元素逆序排列。

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


int main() {
    list<int> m = { 1, 2,3,4, 5 };

    m.reverse( ); // 移除列表中所有值为 2 的元素

    cout << "m after remove: ";
    for (int num : m) {
        cout << num << " ";
    }
    cout << endl;

    return 0;
}

 7.list的迭代器失效

迭代器失效即迭代器所指向的节点的无效,即该节点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入时是不会导致list的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。

当使用 std::list 进行删除操作时,可能会导致迭代器失效。下面是一个示例:

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


int main() {
    list<int> m = { 1, 2, 3, 4, 5 };

    auto it = m.begin();
    ++it; // Move the iterator to the second element

    m.erase(it); // Erase the second element

    for (auto n : m) {
        cout << n << " ";
    }

    return 0;
}

在上面的示例中,当我们在第二个元素位置处使用 erase 函数删除元素后,迭代器 it 就会失效,因为它指向的元素已经被删除。如果我们尝试使用失效的迭代器,可能会导致未定义的行为

要修正这个问题,可以使用 erase 函数的返回值,它会返回一个指向下一个有效元素的迭代器:

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


int main() {
    list<int> m = { 1, 2, 3, 4, 5 };

    auto it = m.begin();
    ++it; // Move the iterator to the second element

    it=m.erase(it); // Erase the second element

    for (auto n : m) {
        cout << n << " ";
    }

    return 0;
}


本文章借鉴了「爱学习的鱼佬」的原创文章,原文在下面链接
原文链接:https://blog.csdn.net/kingxzq/article/details/132225841

  • 40
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值