C++STL之list
1.list基本介绍
2.构造函数
list | 构造函数 |
---|
list | 默认构造函数 |
list(size_type n, const value_type& val = value_type()) | 构造n个值为val的元素 |
list (const list& x) | 拷贝构造 |
list (InputIterator first, InputIterator last) | 使用迭代器来构造 |
#include <list>
#include <iostream>
using namespace std;
int main()
{
list<int> lt;
list<int> lt1(lt);
list<int> lt2(5, 20);
for (auto& e : lt2)
{
cout << e << " ";
}
cout << endl;
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
list<int> lt3(arr, arr + 10);
for (auto& e : lt3)
{
cout << e << " ";
}
cout << endl;
return 0;
}

3.迭代器
list | iterator/reverse_iterator |
---|
begin()+end() | 双向迭代器 |
rbegin()+rend() | 反向迭代器 |
#include <list>
#include <iostream>
using namespace std;
int main()
{
list<int> lt(5, 20);
list<int>::iterator it=lt.begin();
while (it != lt.end())
{
cout << *it << " ";
it++;
}
return 0;
}
#include <list>
#include <iostream>
using namespace std;
int main()
{
list<int> lt{ 1,2,3,3,4,5,6,7,8,9,10 };
list<int>::reverse_iterator rit = lt.rbegin();
while (rit != lt.rend())
{
cout << *rit << " ";
rit++;
}
return 0;
}
#include <list>
#include <iostream>
using namespace std;
int main()
{
list<int> lt={1,2,3,4,5,6,7,8,9,10};
list<int>::iterator it = lt.end();
while (it != lt.begin())
{
--it;
cout <<*it << " ";
}
return 0;
}
4.空间成员函数
list | 空间函数 |
---|
size() | 链表大小 |
empty() | 链表是否为空 |
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> lt{1, 2, 3, 4, 5, 6, 7};
cout << lt.size() << endl;
cout << lt.empty() << endl;
return 0;
}
5.元素获取成员函数
list | 元素获取函数 |
---|
back() | 获取最后一个元素的值 |
front() | 获取第一个元素的值 |
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> lt{1, 2, 3, 4, 5, 6, 7};
cout << "The first element:";
cout << lt.front() << endl;
cout << "The end elemeent:";
cout << lt.back() << endl;
return 0;
}

6.元素操作成员函数
list | 元素操作函数 |
---|
push_back( (const value_type& val)) | 尾插 |
push_front( (const value_type& val)) | 头插 |
pop_front() | 头删 |
pop_back() | 尾删 |
iterator insert (iterator position, const value_type& val) | 在positon位置插入val |
void insert (iterator position, size_type n, const value_type& val) | 在position位置插入n个val |
iterator erase (iterator position) | 删除position位置的元素 |
iterator erase (iterator first, iterator last) | 以迭代器的方式删除[first,last)的元素 |
clear() | 清空链表 |
swap(list& lt) | 交换两个链表 |
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> lt;
lt.push_back(10);
lt.push_back(11);
lt.push_back(12);
for (auto& e : lt)
{
cout << e << " ";
}
cout << endl;
lt.push_front(9);
lt.push_front(8);
lt.push_front(7);
for (auto& e : lt)
{
cout << e << " ";
}
cout << endl;
lt.pop_front();
for (auto& e : lt)
{
cout << e << " ";
}
cout << endl;
lt.pop_back();
for (auto& e : lt)
{
cout << e << " ";
}
cout << endl;
return 0;
}

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
list<int> lt;
lt.push_back(10);
lt.push_back(11);
lt.push_back(12);
lt.push_back(13);
cout << "初始元素:";
for (auto& e : lt)
{
cout << e << " ";
}
cout << endl;
list<int>::iterator it=find(lt.begin(), lt.end(), 11);
lt.insert(it, 7);
cout << "insert之后:";
for (auto& e : lt)
{
cout << e << " ";
}
cout << endl;
auto it1 = find(lt.begin(), lt.end(), 12);
lt.erase(it1);
cout << "erase之后:";
for (auto& e : lt)
{
cout << e << " ";
}
cout << endl;
lt.erase(lt.begin(), lt.end());
cout << "insert之后:";
for (auto& e : lt)
{
cout << e << " ";
}
cout << endl;
return 0;
}

#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
cout << "初始元素:";
for (auto& e : lt)
{
cout << e<<" ";
}
cout << endl;
list<int> lt1;
lt.swap(lt1);
cout << "lt和默认构造lt1交换后的元素:";
for (auto& e : lt)
{
cout << e<<" ";
}
cout << endl;
cout << "lt1的元素:";
for (auto& e : lt1)
{
cout << e << " ";
}
cout << endl;
lt1.clear();
cout << "lt1清空元素后:";
for (auto& e : lt1)
{
cout << e << " ";
}
cout << endl;
return 0;
}

7.迭代器失效问题
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
lt.push_back(4);
lt.push_back(5);
auto it = lt.begin();
while (it!=lt.end())
{
lt.erase(it++);
}
if (lt.empty())
{
cout << "list is empty"<<endl;
}
return 0;
}

8.list和vector的对比
