下面主要介绍list常用接口,使用方法,操作,以及vector和list的区别。
list的常见接口:assign先清空重新放 resize clear
构造、拷贝和析构
迭代器操作
容量操作
元素访问操作
元素修改操作
特殊操作
头文件:
#include<stdio.h>
#include<iostream>
using namespace std;
包含list类的头文件:
//list类
#include<list>
int main() {
list<int>L1;
list<int>L2(10, 5);
//迭代器类型:vector<int>::iterator
vector<int> v{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
list<int>L3(v.begin(),v.end());
list<int>L4(L3);
system("pause");
return 0;
}
class Date {
public:
Date(int year, int month, int day)
:_year(year)
, _month(month)
, _day(day)
{}
private:
int _year;
int _month;
int _day;
};
int main() {
list<int>L1(10, 5);
//第二个值未提供情况下,编译器默认是0
list<int>L2(10);
auto rit = L2.rbegin();
while (rit != L2.rend()) {
cout << *rit << endl;
++rit;
}
list<Date>L3(10,Date(2019,8,3));
list<int>L4(10);
system("pause");
return 0;
}
vector和list的区别?
vector list
底层结构不同: 动态顺序表 头节点双向循环链表
支持随机访问 不支持
插入元素时可能需要扩容 不需要
有与容量相关的操作 没有与容量相关的操作
空间利用率不同
任意位置插入删除元素效率:O(N) O(1)
迭代器: typedef T* iterator; 对节点类型的指针重新封装