快速上手STL中list的使用
目录
1.list的构造函数
2.list的赋值运算符重载
3.list的容量操作
4.list的元素访问
5.list的插入删除
insert和erase
头插头删和尾插尾删
6.list的其他操作
交换两个list
改变list的size
清空list
转移链表中的元素
1.list的构造函数
函数原型:
- 默认构造:explicit list (const allocator_type& alloc = allocator_type());
- 用n个相同的元素构造:explicit list (size_type n, const value_type& val = value_type(),const allocator_type& alloc = allocator_type());
- 迭代器区间构造:
template <class InputIterator>
list(InputIterator first, InputIterator last,const allocator_type & alloc = allocator_type()); - 拷贝构造:list (const list& x);
使用示例:
// list的构造函数
void test_constructor()
{
// 默认构造:explicit list (const allocator_type& alloc = allocator_type());
list<int> lt1;
// 用n个相同的元素构造:explicit list (size_type n, const value_type& val = value_type(),const allocator_type& alloc = allocator_type());
list<int> lt2(10, 1);
cout << "lt2: ";
for (auto e : lt2)
{
cout << e << ' ';
}
cout << endl;
// 迭代器区间构造:
// template <class InputIterator>
// list(InputIterator first, InputIterator last,const allocator_type & alloc = allocator_type());
list<int> lt3(lt2.begin(), lt2.end());
cout << "lt3: ";
for (auto e : lt3)
{
cout << e << ' ';
}
cout << endl;
// 拷贝构造:list (const list& x);
list<int> lt4(lt3);
cout << "lt4: ";
for (auto e : lt4)
{
cout << e << ' ';
}
cout << endl;
}
int main()
{
test_constructor();
return 0;
}
运行结果:
lt2: 1 1 1 1 1 1 1 1 1 1
lt3: 1 1 1 1 1 1 1 1 1 1
lt4: 1 1 1 1 1 1 1 1 1 1
2.list的赋值运算符重载
函数原型:
- 赋值运算符重载函数:list& operator= (const list& x);
使用示例:
// list的赋值运算符重载
void test_1()
{
list<int> lt1(5, 1);
list<int> lt2(8, 2);
cout << "lt1:";
for (auto e : lt1)
{
cout << e << ' ';
}
cout << endl;
cout << "lt2:";
for (auto e : lt2)
{
cout << e << ' ';
}
cout << endl << endl;
lt1 = lt2;
cout << "after \"lt1 = lt2\"" << endl;
cout << "lt1:";
for (auto e : lt1)
{
cout << e << ' ';
}
cout << endl;
cout << "lt2:";
for (auto e : lt2)
{
cout << e << ' ';
}
cout << endl;
}
int main()
{
test_1();
return 0;
}
运行结果:
lt1:1 1 1 1 1
lt2:2 2 2 2 2 2 2 2
after "lt1 = lt2"
lt1:2 2 2 2 2 2 2 2
lt2:2 2 2 2 2 2 2 2
3.list的容量操作
函数原型:
- 获取list中元素的大小: size_type size() const;
- 判断是否为空: bool empty() const;
使用示例:
// list的容量操作
void test_capacity()
{
list<int> lt(5, 1);
// 获取list中元素的大小: size_type size() const;
int size = lt.size();
cout << "lt.size: " << size << endl;
// 判断是否为空: bool empty() const;
if (!lt.empty())
{
cout << "lt不为空" << endl;
}
}
int main()
{
test_capacity();
return 0;
}
运行结果:
lt.size: 5
lt不为空
4.list的元素访问
访问方式:
- 范围for
- 迭代器
- 获取第一个元素
reference front(); 非const对象调用非const版本的函数,返回非const修饰的引用
const_reference front() const; const对象调用const版本的函数,返回const修饰的引用 - 获取最后一个元素
reference back(); 非const对象调用非const版本的函数,返回非const修饰的引用
const_reference back() const; const对象调用const版本的函数,返回const修饰的引用
使用示例:
// list的元素访问
void test_element_access()
{
int arr[] = { 0,1,2,3,4,5,6,7,8,9 };
list<int> lt(arr, arr+10);
// 通过范围for访问
for (auto e : lt)
{
cout << e << " ";
}
cout << endl << endl;
// 通过迭代器访问
auto it = lt.begin();
while (it != lt.end())
{
cout << *it << ' ';
++it;
}
cout << endl << endl;
// 获取第一个元素
// reference front(); 非const对象调用非const版本的函数,返回非const修饰的引用
// const_reference front() const; const对象调用const版本的函数,返回const修饰的引用
cout << "lt.front: " << lt.front() << endl;
// 获取最后一个元素
// reference back(); 非const对象调用非const版本的函数,返回非const修饰的引用
// const_reference back() const; const对象调用const版本的函数,返回const修饰的引用
cout << "lt.back: " << lt.back() << endl;
}
int main()
{
test_element_access();
return 0;
}
运行结果:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
lt.front: 0
lt.back: 9
5.list的插入删除
insert和erase
insert函数原型:
- 指定迭代器位置插入:iterator insert (iterator position, const value_type& val);
- 指定迭代器位置插入n个元素:void insert (iterator position, size_type n, const value_type& val);
- 在指定迭代器位置插入一段迭代器区间的值:
template <class InputIterator>
void insert(iterator position, InputIterator first, InputIterator last);
erase函数原型:
- 删除指定迭代器位置的值:iterator erase (iterator position);
- 删除一段迭代器区间的值:iterator erase (iterator first, iterator last);
使用示例:
// list的插入和删除
void test_inser_erase()
{
/*插入操作*/
list<int> lt(2, 0);
cout << "lt: ";
for (auto e : lt)
{
cout << e << ' ';
}
cout << endl << endl;
// 指定迭代器位置插入:iterator insert (iterator position, const value_type& val
list<int>::iterator it = lt.begin();
lt.insert(it, 100);
cout << "lt: ";
for (auto e : lt)
{
cout << e << ' ';
}
cout << endl << endl;
// 指定迭代器位置插入n个元素:void insert (iterator position, size_type n, const value_type& val);
it = lt.begin();
lt.insert(it, 2, 200);
cout << "lt: ";
for (auto e : lt)
{
cout << e << ' ';
}
cout << endl << endl;
// 在指定迭代器位置插入一段迭代器区间的值:
// template <class InputIterator>
// void insert(iterator position, InputIterator first, InputIterator last);
it = lt.end();
lt.insert(it, lt.begin(), lt.end());
cout << "lt: ";
for (auto e : lt)
{
cout << e << ' ';
}
cout << endl << endl << endl;
/*删除操作*/
// 删除指定迭代器位置的值:iterator erase (iterator position);
lt.erase(lt.begin());
cout << "lt: ";
for (auto e : lt)
{
cout << e << ' ';
}
cout << endl << endl;
// 删除一段迭代器区间的值:iterator erase (iterator first, iterator last);
lt.erase(lt.begin(), --lt.end());
cout << "lt: ";
for (auto e : lt)
{
cout << e << ' ';
}
cout << endl << endl;
}
int main()
{
test_inser_erase();
return 0;
}
运行结果:
lt: 0 0
lt: 100 0 0
lt: 200 200 100 0 0
lt: 200 200 100 0 0 200 200 100 0 0
lt: 200 100 0 0 200 200 100 0 0
lt: 0
头插头删和尾插尾删
函数原型:
- 头插:void push_front (const value_type& val);
- 头删:void pop_front();
- 尾插:void push_back (const value_type& val);
- 尾删:void pop_back();
使用示例:
// list的头插头删和尾插尾删
void test_2()
{
list<int> lt(2, 1);
cout << "lt: ";
for (auto e : lt)
{
cout << e << ' ';
}
cout << endl << endl;
// 头插:void push_front (const value_type& val);
lt.push_front(5);
cout << "after \"lt.push_front(5)\"" << endl;
cout << "lt: ";
for (auto e : lt)
{
cout << e << ' ';
}
cout << endl << endl;
// 头删:void pop_front();
lt.pop_front();
cout << "after \"lt.pop_front();\"" << endl;
cout << "lt: ";
for (auto e : lt)
{
cout << e << ' ';
}
cout << endl << endl;
// 尾插:void push_back (const value_type& val);
lt.push_back(6);
cout << "after \"lt.push_back(6);\"" << endl;
cout << "lt: ";
for (auto e : lt)
{
cout << e << ' ';
}
cout << endl << endl;
// 尾删:void pop_back();
lt.pop_back();
cout << "after \"lt.pop_back();\"" << endl;
cout << "lt: ";
for (auto e : lt)
{
cout << e << ' ';
}
cout << endl << endl;
}
int main()
{
test_2();
return 0;
}
运行结果:
lt: 1 1
after "lt.push_front(5)"
lt: 5 1 1
after "lt.pop_front();"
lt: 1 1
after "lt.push_back(6);"
lt: 1 1 6
after "lt.pop_back();"
lt: 1 1
6.list的其他操作
交换两个list
函数原型:
- 交换两个链表:void swap (list& x);
使用示例:
// list的交换
void test_swap()
{
list<int> lt1(5, 1);
list<int> lt2(5, 2);
cout << "lt1: ";
for (auto e : lt1)
{
cout << e << ' ';
}
cout << endl << endl;
cout << "lt2: ";
for (auto e : lt2)
{
cout << e << ' ';
}
cout << endl << endl;
// 交换两个list:void swap (list& x);
lt1.swap(lt2);
cout << "lt1: ";
for (auto e : lt1)
{
cout << e << ' ';
}
cout << endl << endl;
cout << "lt2: ";
for (auto e : lt2)
{
cout << e << ' ';
}
cout << endl << endl;
}
int main()
{
test_swap();
return 0;
}
运行结果:
lt1: 1 1 1 1 1
lt2: 2 2 2 2 2
lt1: 2 2 2 2 2
lt2: 1 1 1 1 1
改变list的size
函数原型:
- 改变size:void swap (list& x);
使用示例:
// 改变链表的size
void test_resize()
{
list<int> lt1;
for (int i = 1; i < 10; ++i)
lt1.push_back(i);
cout << "lt1: ";
for (auto e : lt1)
{
cout << e << ' ';
}
cout << endl << endl;
lt1.resize(5);
cout << "lt1: ";
for (auto e : lt1)
{
cout << e << ' ';
}
cout << endl << endl;
// 改变size:void swap (list& x);
lt1.resize(8, 100);
cout << "lt1: ";
for (auto e : lt1)
{
cout << e << ' ';
}
cout << endl << endl;
lt1.resize(12);
cout << "lt1: ";
for (auto e : lt1)
{
cout << e << ' ';
}
cout << endl << endl;
}
int main()
{
test_resize();
return 0;
}
运行结果:
lt1: 1 2 3 4 5 6 7 8 9
lt1: 1 2 3 4 5
lt1: 1 2 3 4 5 100 100 100
lt1: 1 2 3 4 5 100 100 100 0 0 0 0
清空list
函数原型:
- 清空list:void clear();
使用示例:
// 清空list
void test_clear()
{
list<int> lt(5, 0);
cout << "lt: ";
for (auto e : lt)
{
cout << e << ' ';
}
cout << endl;
cout << "size: " << lt.size() << endl << endl;
// 清空list
lt.clear();
for (auto e : lt)
{
cout << e << ' ';
}
cout << endl;
cout << "size: " << lt.size() << endl << endl;
}
int main()
{
test_clear();
return 0;
}
运行结果:
lt: 0 0 0 0 0
size: 5
size: 0
转移链表中的元素
函数原型:
- 将链表中的一个元素转移到另一个链表中的指定位置:void splice (iterator position, list& x, iterator i);
- 将一个链表的迭代器区间中的值转移到另一个链表:void splice (iterator position, list& x, iterator first, iterator last);
- 将整个链表转移到另一个链表的指定位置:void splice (iterator position, list& x);
使用示例:
// 链表中元素的转移
void test_splice()
{
list<int> lt1(5, 1);
list<int> lt2(10, 2);
cout << "lt1: ";
for (auto e : lt1)
{
cout << e << ' ';
}
cout << endl << endl;
cout << "lt2: ";
for (auto e : lt2)
{
cout << e << ' ';
}
cout << endl << endl;
// 将链表中的一个元素转移到另一个链表中的指定位置:void splice (iterator position, list& x, iterator i);
lt1.splice(lt1.begin(), lt2, lt2.begin());
cout << "lt1: ";
for (auto e : lt1)
{
cout << e << ' ';
}
cout << endl << endl;
cout << "lt2: ";
for (auto e : lt2)
{
cout << e << ' ';
}
cout << endl << endl;
// 将一个链表的迭代器区间中的值转移到另一个链表:void splice (iterator position, list& x, iterator first, iterator last);
lt1.splice(lt1.begin(), lt2, lt2.begin(), ++lt2.begin());
cout << "lt1: ";
for (auto e : lt1)
{
cout << e << ' ';
}
cout << endl << endl;
cout << "lt2: ";
for (auto e : lt2)
{
cout << e << ' ';
}
cout << endl << endl;
// 将整个链表转移到另一个链表的指定位置:void splice (iterator position, list& x);
lt1.splice(lt1.begin(), lt2);
cout << "lt1: ";
for (auto e : lt1)
{
cout << e << ' ';
}
cout << endl << endl;
cout << "lt2: ";
for (auto e : lt2)
{
cout << e << ' ';
}
cout << endl << endl;
}
int main()
{
test_splice();
return 0;
}
运行结果:
lt1: 1 1 1 1 1
lt2: 2 2 2 2 2 2 2 2 2 2
lt1: 2 1 1 1 1 1
lt2: 2 2 2 2 2 2 2 2 2
lt1: 2 2 1 1 1 1 1
lt2: 2 2 2 2 2 2 2 2
lt1: 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1
lt2: