【C++】---STL之list详解
- 一、了解list的基本信息
- 二、成员函数
- 1、构造
- 2、迭代器
- 3、empty()
- 4、size()
- 5、front()
- 6、back()
- 7、push_front()
- 8、pop_front()
- 9、push_back()
- 10、pop_back()
- 11、insert()
- 12、erase()
- 13、swap()
- 14、sort()
- 15、reverse()
一、了解list的基本信息
1、库里面的list是一个带头双向循环链表结构!
2、优点:可以在任意位置进行插入删除,插入删除的效率比较高。
3、缺点:不支持任意位置的随机访问(因为物理空间不连续!)
二、成员函数
1、构造
explicit list (const allocator_type& alloc = allocator_type()); //构造空listexplicit list (size_type n, const value_type& val = value_type(),//构造一个有n个元素值为val的listconst allocator_type& alloc = allocator_type());template <class InputIterator>//构造一个list,值为InputIterator的first到last之间的元素list (InputIterator first, InputIterator last,const allocator_type& alloc = allocator_type());
2、迭代器
#include<list>
#include<iostream>
using namespace std;int main()
{list<int> lt1;lt1.push_back(1);lt1.push_back(2);lt1.push_back(3);lt1.push_back(4);lt1.push_back(5);list<int>::iterator it = lt1.begin();while (it != lt1.end()){cout << *it << " ";it++;}cout << endl;return 0;
}
3、empty()
4、size()
int main()
{list<int> lt1;lt1.push_back(1);lt1.push_back(2);lt1.push_back(3);lt1.push_back(4);lt1.push_back(5);//list<int>::iterator it = lt1.begin();//while (it != lt1.end())//{// cout << *it << " ";// it++;//}//cout << endl;//cout << lt1.empty() << endl;cout << lt1.size()<<endl;return 0;
}
5、front()
int main()
{list<int> lt1;lt1.push_back(1);lt1.push_back(2);lt1.push_back(3);lt1.push_back(4);lt1.push_back(5);cout << lt1.front() << endl;return 0;
}
6、back()
int main()
{list<int> lt1;lt1.push_back(1);lt1.push_back(2);lt1.push_back(3);lt1.push_back(4);lt1.push_back(5);//cout << lt1.front() << endl;cout << lt1.back() << endl;return 0;
}
7、push_front()
int main()
{list<int> lt1;lt1.push_front(1);lt1.push_front(2);lt1.push_front(3);lt1.push_front(4);lt1.push_front(5);for (auto e : lt1){cout << e << " ";}cout << endl;return 0;
}
8、pop_front()
int main()
{list<int> lt1;lt1.push_front(1);lt1.push_front(2);lt1.push_front(3);lt1.push_front(4);lt1.push_front(5);for (auto e : lt1){cout << e << " ";}cout << endl;lt1.pop_front();lt1.pop_front();lt1.pop_front();for (auto e : lt1){cout << e << " ";}cout << endl;return 0;
}
9、push_back()
int main()
{list<int> lt1;lt1.push_back(1);lt1.push_back(2);lt1.push_back(3);lt1.push_back(4);lt1.push_back(5);for (auto e : lt1){cout << e << " ";}cout << endl;return 0;
}
10、pop_back()
int main()
{list<int> lt1;lt1.push_back(1);lt1.push_back(2);lt1.push_back(3);lt1.push_back(4);lt1.push_back(5);for (auto e : lt1){cout << e << " ";}cout << endl;lt1.pop_back();lt1.pop_back();lt1.pop_back();for (auto e : lt1){cout << e << " ";}cout << endl;return 0;
}
11、insert()
int main()
{list<int> lt1;lt1.push_back(1);lt1.push_back(2);lt1.push_back(3);lt1.push_back(4);lt1.push_back(5);list<int>::iterator pos = find(lt1.begin(), lt1.end(), 2);//在链表中查找值为2的元素,并返回它的迭代器lt1.insert(pos, 100);// 在pos位置之前插入100for (auto e : lt1){cout << e << " ";}cout << endl;return 0;
}
12、erase()
改变底层的数据结构,迭代器失效:
int main()
{list<int> lt1;lt1.push_back(1);lt1.push_back(2);lt1.push_back(3);lt1.push_back(4);lt1.push_back(5);list<int>::iterator pos = find(lt1.begin(), lt1.end(), 3);// 擦除元素值为3的lt1.erase(pos);cout << *pos << endl;// pos的位置的元素的值已经被删除了。//此时如果不对pos的位置的迭代器进行重新赋值更新,那么pos就是一个野指针,非法访问。for (auto e : lt1){cout << e << " ";}cout << endl;return 0;
}
erase删除pos位置元素后,pos位置之后的元素会往前搬移,没有导致底层空间的改变,理论上讲迭代 器不应该会失效,但是:如果pos刚好是最后一个元素,删完之后pos刚好是end的位置,而end位置是 没有元素的,那么pos就失效了。因此删除list中任意位置上元素时,vs就认为该位置迭代器失效 了。
13、swap()
int main()
{list<int> lt1;lt1.push_back(1);lt1.push_back(2);lt1.push_back(3);lt1.push_back(4);lt1.push_back(5);list<int> lt2;lt2.push_back(1);lt2.push_back(1);lt2.push_back(1);lt2.push_back(1);lt2.push_back(1);cout << "lt1交换前:";for (auto e : lt1){cout << e << " ";}cout << endl;lt1.swap(lt2);// lt1 和 lt2内容进行交换cout << "lt1交换后:";for (auto e : lt1){cout << e << " ";}cout << endl;return 0;
}
14、sort()
sort()函数默认是:排升序!
int main()
{list<int> lt1;lt1.push_back(4);lt1.push_back(3);lt1.push_back(5);lt1.push_back(1);lt1.push_back(2);for (auto e : lt1){cout << e << " ";}cout << endl;lt1.sort();for (auto e : lt1){cout << e << " ";}cout << endl;return 0;
}
15、reverse()
int main()
{list<int> lt1;lt1.push_back(4);lt1.push_back(3);lt1.push_back(5);lt1.push_back(1);lt1.push_back(2);for (auto e : lt1){cout << e << " ";}cout << endl;lt1.sort();for (auto e : lt1){cout << e << " ";}cout << endl;lt1.reverse();for (auto e : lt1){cout << e << " ";}return 0;
}
好了,今天的分享就到这里了
如果对你有帮助,记得点赞👍+关注哦!
我的主页还有其他文章,欢迎学习指点。关注我,让我们一起学习,一起成长吧!