0
点赞
收藏
分享

微信扫一扫

C++动态规划算法:最多可以参加的会议数目

敬亭阁主 2023-11-25 阅读 24
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 的详细介绍

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

1.1list 的优势和劣势

优势:
劣势:

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()

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

3.2. end()

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 容器的一个成员函数,用于判断容器是否为空。它返回一个布尔值,表示容器是否不包含任何元素。函数声明如下:

例子 

#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 容器的一个成员函数,用于返回容器中元素的数量。它返回一个无符号整数类型,表示容器中的元素数量。函数声明如下:

#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 容器的一个成员函数,用于返回容器可能容纳的最大元素数量,通常受到系统内存限制的影响。它返回一个无符号整数类型,表示容器的最大大小。函数签名如下:

#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)容器的对象。函数的签名如下:

#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)容器的对象。函数的签名如下:

#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 容器的成员函数,用于在容器的开头插入一个新元素。

#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;
}


本文章借鉴了「爱学习的鱼佬」的原创文章,原文在下面链接

举报

相关推荐

0 条评论