List容器(双向链表)

List 容器

list是C++标准模版库(STL,Standard Template Library)中的部分内容。实际上,list容器就是一个双向链表,可以高效地进行插入删除元素。

使用list容器之前必须加上头文件:#include<list>;

list属于std命名域的内容,因此需要通过命名限定:using std::list;也可以直接使用全局的命名空间方式:using namespace std;

构造函数

    list<int> c0; //空链表

  list<int> c1(3); //建一个含三个默认值是0的元素的链表

  list<int> c2(5,2); //建一个含五个元素的链表,值都是2

  list<int> c4(c2); //建一个c2的copy链表

  list<int> c5(c1.begin(),c1.end()); c5含c1一个区域的元素[_First, _Last)。

成员函数

c.push_back(num)      在末尾增加一个元素。

c.pop_back()      删除末尾的元素。

c.push_front(num)      在开始位置增加一个元素。//这是双向链表比vector容器多的函数

c.pop_front()      删除第一个元素。

c.begin()      返回指向链表第一个元素的迭代器。

c.end()      返回指向链表最后一个元素之后的迭代器。

c.rbegin()      返回逆向链表的第一个元素,即c链表的最后一个数据。

c.rend()      返回逆向链表的最后一个元素的下一个位置,即c链表的第一个数据再往前的位置。


c.assign(n,num)      将n个num拷贝赋值给链表c。

c.assign(beg,end)      将[beg,end)区间的元素拷贝赋值给链表c。


c.front()      返回链表c的第一个元素。

c.back()      返回链表c的最后一个元素。


c.empty()  判断链表是否为空。


c.size()      返回链表c中实际元素的个数。

c.erase(pos)    删除pos位置的元素。

c.clear()      清除链表c中的所有元素。

c.insert(pos,num)      在pos位置插入元素num。

c.insert(pos,n,num)      在pos位置插入n个元素num。

c.insert(pos,beg,end)      在pos位置插入区间为[beg,end)的元素。

resize(n)      从新定义链表的长度,超出原始长度部分用0代替,小于原始部分删除。

resize(n,num)            从新定义链表的长度,超出原始长度部分用num代替。


c1.swap(c2);      将c1和c2交换。

swap(c1,c2);      同上。


c1.merge(c2)      合并2个有序的链表并使之有序,从新放到c1里,释放c2。

c1.merge(c2,comp)      合并2个有序的链表并使之按照自定义规则排序之后从新放到c1中,释放c2。


c1.splice(c1.beg,c2)      将c2连接在c1的beg位置,释放c2

c1.splice(c1.beg,c2,c2.beg)      将c2的beg位置的元素连接到c1的beg位置,并且在c2中施放掉beg位置的元素

c1.splice(c1.beg,c2,c2.beg,c2.end)      将c2的[beg,end)位置的元素连接到c1的beg位置并且释放c2的[beg,end)位置的元素


remove(num)             删除链表中匹配num的元素。

remove_if(comp)       删除条件满足的元素,参数为自定义的回调函数。

reverse()       反转链表

unique()       删除相邻的元素


c.sort()       将链表排序,默认升序

c.sort(comp)       自定义回调函数实现自定义排序

重载运算符

operator==

operator!=

operator<

operator<=

operator>

operator>=


各个函数具体用法举例:

c.begin()      返回指向链表第一个元素的迭代器。

c.end()      返回指向链表最后一个元素之后的迭代器。

1     list<int> a1{1,2,3,4,5};
2     list<int>::iterator it;
3     for(it = a1.begin();it!=a1.end();it++){
4         cout << *it << "\t";
5     }
6     cout << endl;

c.rbegin()      返回逆向链表的第一个元素,即c链表的最后一个数据。

c.rend()      返回逆向链表的最后一个元素的下一个位置,即c链表的第一个数据再往前的位置。

1     list<int> a1{1,2,3,4,5};
2     list<int>::reverse_iterator it;
3     for(it = a1.rbegin();it!=a1.rend();it++){
4         cout << *it << "\t";
5     }
6     cout << endl;

operator=      重载赋值运算符。

1     list<int> a1 {1,2,3,4,5},a2;
2     a2 = a1;
3     list<int>::iterator it;
4     for(it = a2.begin();it!=a2.end();it++){
5         cout << *it << endl;
6     }

c.assign(n,num)      将n个num拷贝赋值给链表c。

c.assign(beg,end)      将[beg,end)区间的元素拷贝赋值给链表c。

复制代码
 1     int a[5] = {1,2,3,4,5};
 2     list<int> a1;
 3     list<int>::iterator it;
 4     a1.assign(2,10);
 5     for(it = a1.begin();it!=a1.end();it++){
 6         cout << *it << " ";
 7     }
 8     cout << endl;
 9     a1.assign(a,a+5);
10     for(it = a1.begin();it!=a1.end();it++){
11         cout << *it << " ";
12     }
13     cout << endl;
复制代码

c.front()      返回链表c的第一个元素。

c.back()      返回链表c的最后一个元素。

1     list<int> a1{1,2,3,4,5};
2     if(!a1.empty()){
3         cout << "the first number is:" << a1.front() << endl;
4         cout << "the last number is:" << a1.back() << endl;
5     }

c.empty()  判断链表是否为空。

1     list<int> a1{1,2,3,4,5};
2     if(!a1.empty())
3         cout << "a1 is not empty" << endl;
4     else
5         cout << " a1 is empty" << endl;

c.size()      返回链表c中实际元素的个数。

1     list<int> a1{1,2,3,4,5};
2     cout << a1.size() << endl;

c.max_size()      返回链表c可能容纳的最大元素数量。

1     list<int> a1{1,2,3,4,5};
2     cout << a1.max_size() << endl;

c.clear()      清除链表c中的所有元素。

复制代码
 1     list<int> a1{1,2,3,4,5};
 2     list<int>::iterator it;
 3     cout << "clear before:";
 4     for(it = a1.begin();it!=a1.end();it++){
 5         cout << *it << "\t";
 6     }
 7     cout << endl;
 8     a1.clear();
 9     cout << "clear after:";
10     for(it = a1.begin();it!=a1.end();it++){
11         cout << *it << "\t";
12     }
13     cout << endl;
复制代码

c.insert(pos,num)      在pos位置插入元素num。

c.insert(pos,n,num)      在pos位置插入n个元素num。

c.insert(pos,beg,end)      在pos位置插入区间为[beg,end)的元素。

复制代码
 1     list<int> a1{1,2,3,4,5};
 2     list<int>::iterator it;
 3     cout << "insert before:";
 4     for(it = a1.begin();it!=a1.end();it++){
 5         cout << *it << " ";
 6     }
 7     cout << endl;
 8     
 9     a1.insert(a1.begin(),0);
10     cout << "insert(pos,num) after:";
11     for(it = a1.begin();it!=a1.end();it++){
12         cout << *it << " ";
13     }
14     cout << endl;
15     
16     a1.insert(a1.begin(),2,88);
17     cout << "insert(pos,n,num) after:";
18     for(it = a1.begin();it!=a1.end();it++){
19         cout << *it << " ";
20     }
21     cout << endl;
22 
23     int arr[5] = {11,22,33,44,55};
24     a1.insert(a1.begin(),arr,arr+3);
25     cout << "insert(pos,beg,end) after:";
26     for(it = a1.begin();it!=a1.end();it++){
27         cout << *it << " ";
28     }
29     cout << endl;
复制代码

c.erase(pos)    删除pos位置的元素。

复制代码
 1     list<int> a1{1,2,3,4,5};
 2     list<int>::iterator it;
 3     cout << "erase before:";
 4     for(it = a1.begin();it!=a1.end();it++){
 5         cout << *it << " ";
 6     }
 7     cout << endl;
 8     a1.erase(a1.begin());
 9     cout << "erase after:";
10     for(it = a1.begin();it!=a1.end();it++){
11         cout << *it << " ";
12     }
13     cout << endl;
复制代码

c.push_back(num)      在末尾增加一个元素。

c.pop_back()      删除末尾的元素。

c.push_front(num)      在开始位置增加一个元素。

c.pop_front()      删除第一个元素。

复制代码
 1     list<int> a1{1,2,3,4,5};
 2     a1.push_back(10);
 3     list<int>::iterator it;
 4     cout << "push_back:";
 5     for(it = a1.begin();it!=a1.end();it++){
 6         cout << *it << " ";
 7     }
 8     cout << endl;
 9     
10     a1.pop_back();
11     cout << "pop_back:";
12     for(it = a1.begin();it!=a1.end();it++){
13         cout << *it << " ";
14     }
15     cout << endl;
16     
17     a1.push_front(20);
18     cout << "push_front:";
19     for(it = a1.begin();it!=a1.end();it++){
20         cout << *it << " ";
21     }
22     cout << endl;
23     
24     a1.pop_front();
25     cout << "pop_front:";
26     for(it = a1.begin();it!=a1.end();it++){
27         cout << *it << " ";
28     }
29     cout << endl;
复制代码

resize(n)      从新定义链表的长度,超出原始长度部分用0代替,小于原始部分删除。

resize(n,num)            从新定义链表的长度,超出原始长度部分用num代替。

复制代码
 1     list<int> a1{1,2,3,4,5};
 2     a1.resize(8);
 3     list<int>::iterator it;
 4     cout << "resize(n):";
 5     for(it = a1.begin();it!=a1.end();it++){
 6         cout << *it << " ";
 7     }
 8     cout << endl;
 9     
10     a1.resize(10, 10);
11     cout << "resize(n,num):";
12     for(it = a1.begin();it!=a1.end();it++){
13         cout << *it << " ";
14     }
15     cout << endl;
复制代码

c1.swap(c2);      将c1和c2交换。

swap(c1,c2);      同上。

复制代码
 1     list<int> a1{1,2,3,4,5},a2,a3;
 2     a2.swap(a1);
 3     list<int>::iterator it;
 4     cout << "a2.swap(a1):";
 5     for(it = a2.begin();it!=a2.end();it++){
 6         cout << *it << " ";
 7     }
 8     cout << endl;
 9     
10     swap(a3,a2);
11     cout << "swap(a3,a2):";
12     for(it = a3.begin();it!=a3.end();it++){
13         cout << *it << " ";
14     }
15     return 0;
复制代码

c1.merge(c2)      合并2个有序的链表并使之有序,从新放到c1里,释放c2。

c1.merge(c2,comp)      合并2个有序的链表并使之按照自定义规则排序之后从新放到c1中,释放c2。

复制代码
 1     list<int> a1{1,2,3},a2{4,5,6};
 2     a1.merge(a2);
 3     list<int>::iterator it;
 4     cout << "a1.merge(a2):";
 5     for(it = a1.begin();it!=a1.end();it++){
 6         cout << *it << " ";
 7     }
 8     cout << endl;
 9     
10     a2.merge(a1,[](int n1,int n2){return n1>n2;});
11     cout << "a2.merge(a1,comp):";
12     for(it = a2.begin();it!=a2.end();it++){
13         cout << *it << " ";
14     }
15     cout << endl;
复制代码

c1.splice(c1.beg,c2)      将c2连接在c1的beg位置,释放c2

复制代码
1     list<int> a1{1,2,3},a2{4,5,6};
2     a1.splice(a1.begin(), a2);
3     list<int>::iterator it;
4     cout << "a1.merge(a2):";
5     for(it = a1.begin();it!=a1.end();it++){
6         cout << *it << " ";
7     }
8     cout << endl;
复制代码

c1.splice(c1.beg,c2,c2.beg)      将c2的beg位置的元素连接到c1的beg位置,并且在c2中施放掉beg位置的元素

复制代码
1     list<int> a1{1,2,3},a2{4,5,6};
2     a1.splice(a1.begin(), a2,++a2.begin());
3     list<int>::iterator it;
4     cout << "a1.merge(a2):";
5     for(it = a1.begin();it!=a1.end();it++){
6         cout << *it << " ";
7     }
8     cout << endl;
9     return 0;
复制代码

c1.splice(c1.beg,c2,c2.beg,c2.end)      将c2的[beg,end)位置的元素连接到c1的beg位置并且释放c2的[beg,end)位置的元素

复制代码
1     list<int> a1{1,2,3},a2{4,5,6};
2     a1.splice(a1.begin(),a2,a2.begin(),a2.end());
3     list<int>::iterator it;
4     cout << "a1.merge(a2):";
5     for(it = a1.begin();it!=a1.end();it++){
6         cout << *it << " ";
7     }
8     cout << endl;
9     return 0;
复制代码

remove(num)             删除链表中匹配num的元素。

复制代码
1     list<int> a1{1,2,3,4,5};
2     a1.remove(3);
3     list<int>::iterator it;
4     cout << "remove():";
5     for(it = a1.begin();it!=a1.end();it++){
6         cout << *it << " ";
7     }
8     cout << endl;
复制代码

remove_if(comp)       删除条件满足的元素,参数为自定义的回调函数。

复制代码
1     list<int> a1{1,2,3,4,5};
2     a1.remove_if([](int n){return n<3;});
3     list<int>::iterator it;
4     cout << "remove_if():";
5     for(it = a1.begin();it!=a1.end();it++){
6         cout << *it << " ";
7     }
8     cout << endl;
复制代码

reverse()       反转链表

复制代码
1     list<int> a1{1,2,3,4,5};
2     a1.reverse();
3     list<int>::iterator it;
4     cout << "reverse:";
5     for(it = a1.begin();it!=a1.end();it++){
6         cout << *it << " ";
7     }
8     cout << endl;
复制代码

unique()       删除相邻的元素

复制代码
1     list<int> a1{1,1,3,3,5};
2     a1.unique();
3     list<int>::iterator it;
4     cout << "unique:";
5     for(it = a1.begin();it!=a1.end();it++){
6         cout << *it << " ";
7     }
8     cout << endl;
9     return 0;
复制代码

c.sort()       将链表排序,默认升序

c.sort(comp)       自定义回调函数实现自定义排序

复制代码
 1     list<int> a1{1,3,2,5,4};
 2     a1.sort();
 3     list<int>::iterator it;
 4     cout << "sort():";
 5     for(it = a1.begin();it!=a1.end();it++){
 6         cout << *it << " ";
 7     }
 8     cout << endl;
 9     
10     a1.sort([](int n1,int n2){return n1>n2;});
11     cout << "sort(function point):";
12     for(it = a1.begin();it!=a1.end();it++){
13         cout << *it << " ";
14     }
15     cout << endl;
复制代码

重载运算符

operator==

operator!=

operator<

operator<=

operator>

operator>=

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在信号处理领域,DOA(Direction of Arrival)估计是一项关键技术,主要用于确定多个信号源到达接收阵列的方向。本文将详细探讨三种ESPRIT(Estimation of Signal Parameters via Rotational Invariance Techniques)算法在DOA估计中的实现,以及它们在MATLAB环境中的具体应用。 ESPRIT算法是由Paul Kailath等人于1986年提出的,其核心思想是利用阵列数据的旋转不变性来估计信号源的角度。这种算法相比传统的 MUSIC(Multiple Signal Classification)算法具有较低的计算复杂度,且无需进行特征值分解,因此在实际应用中颇具优势。 1. 普通ESPRIT算法 普通ESPRIT算法分为两个主要步骤:构造等效旋转不变系统和估计角度。通过空间平移(如延时)构建两个子阵列,使得它们之间的关系具有旋转不变性。然后,通过对子阵列数据进行最小二乘拟合,可以得到信号源的角频率估计,进一步转换为DOA估计。 2. 常规ESPRIT算法实现 在描述中提到的`common_esprit_method1.m`和`common_esprit_method2.m`是两种不同的普通ESPRIT算法实现。它们可能在实现细节上略有差异,比如选择子阵列的方式、参数估计的策略等。MATLAB代通常会包含预处理步骤(如数据归一化)、子阵列构造、旋转不变性矩阵的建立、最小二乘估计等部分。通过运行这两个文件,可以比较它们在估计精度和计算效率上的异同。 3. TLS_ESPRIT算法 TLS(Total Least Squares)ESPRIT是对普通ESPRIT的优化,它考虑了数据噪声的影响,提高了估计的稳健性。在TLS_ESPRIT算法中,不假设数据噪声是高斯白噪声,而是采用总最小二乘准则来拟合数据。这使得算法在噪声环境下表现更优。`TLS_esprit.m`文件应该包含了TLS_ESPRIT算法的完整实现,包括TLS估计的步骤和旋转不变性矩阵的改进处理。 在实际应用中,选择合适的ESPRIT变体取决于系统条件,例如噪声水平、信号质量以及计算资源。通过MATLAB实现,研究者和工程师可以方便地比较不同算法的效果,并根据需要进行调整和优化。同时,这些代也为教学和学习DOA估计提供了一个直观的平台,有助于深入理解ESPRIT算法的工作原理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值