list的一些基本函数
list底层是一个双向循环链表,链表不支持随机访问。
int main() {
list<int> L; //默认构造
list<int> L1(L); //拷贝构造
list<int> L2(L.begin(), L.end());//迭代器之间的数据初始化
list<int> L3(2, 5); //将2个5个L3;
L = L1; //重载运算符赋值
L.assign(2, 5); //将2个5赋值给L
L.assign(L1.begin(), L1.end());//将迭代器位置的数给L
L.swap(L1); //交换L和L1的元素
L.size(); //返回L的大小
L.empty(); //判空
L.resize(10); //重新指定L的大小,大了就补自动0,小了就截取L的前多少个数
L.resize(10,2);//重新指定L的大小,大了就补 2 ,小了就截取L的前多少个数
system("pause");
return 0;
}
list的插入删除和数据获取
因为list是一个链表形式,其不支持随机访问,所以迭代器只能使用iterator++或者–操作。
int main() {
list<int> L; //默认构造
L.push_back(10);//尾插
L.push_back(20);
L.push_front(1);//头插
L.push_front(2);
L.pop_back(); //尾删
L.pop_front(); //头删
L.insert(L.begin(), 100); // 在迭代器位置插入100
L.insert(L.end(), 2, 6); // 在迭代器位置插入2个6
list<int> L1;
L1.insert(L1.begin(), L.begin(), L.end()); //(A,B,C)在迭代器A位置插入两个迭代器(B-C)之间的数据。
L1.clear(); //清空链表数据
L.erase(++L.begin(), L.end()); //清空两个迭代器位置之间的数据,只能++因为不支持随机访问
//L.erase(L.begin() + 1, L.end()); 非法操作!不支持随机访问
list<int>::iterator it = L.begin();
for (int i = 0; i < L.size() - 2; i++) {
++it;
}
L.insert(it, 1); //如果想实现随机访问可以通过这样的方式来插入,这是在倒数第二个位置出入一个1
L.erase(L1.begin()); //清楚迭代器位置的数据
L.remove(10); //清楚链表中所有等于10的数据
int num = L.front();//获取链表表头元素
num = L.back(); //获取链表表头元素 没有at()和重载[]的方式,因为不支持随机访问
system("pause");
return 0;
}
list内置的反转和排序
凡是不支持随机访问迭代器的容器,不可以使用标准的算法,内部都会提供一些算法。
list<int> L;
L.sort(); //按升序排序
L.reverse(); //反转链表元素
如果想要实现降序,可以先使用sort来实现升序排序,在利用reverse实现反转,就得到了降序的序列。
还有一种方法就是利用仿函数来实现降序排列,具体就是提供一个布尔类型的函数,将函数名作为参数传到sort()中去。
特别是对于我们自己定义的类,对于存在list容器中的类的属性比较大小,一定要利用仿函数来比较大小。
实现降序
bool DownSortList(int a, int b) {//指定排序规则
return a > b;//让第一个数大于等于第二个数,就是降序排列
}
int main(){
list<int> L;
L.sort(DownSortList);//利用仿函数,实现降序排列
}
实现自定义类数据比较大小
class Dog {
public:
Dog(string name, int age, double weight) {
this->dog_name = name;
this->dog_age = age;
this->dog_weight = weight;
}
string dog_name;
int dog_age;
double dog_weight;
};
void printfList(const list<Dog>& l) {
for (list<Dog>::const_iterator it = l.begin(); it != l.end(); it++) {
cout << "修狗的名字: " << it->dog_name<<" \t修狗的年龄:" << it->dog_age <<"\t修狗的体重:"<<it->dog_weight << endl;
}
cout << endl;
}
bool UpSortDogForAgeList(Dog &d1, Dog &d2) {
if (d1.dog_age == d2.dog_age) {
return d1.dog_weight > d2.dog_weight;//如果年龄相同就按体重降序
}
return d1.dog_age < d2.dog_age;
}
bool DownSortDogForAgeList(Dog &d1, Dog &d2) {
if (d1.dog_age == d2.dog_age) {
return d1.dog_weight < d2.dog_weight;//如果年龄相同就按体重升序
}
return d1.dog_age > d2.dog_age;
}
int main() {
Dog dog1("哈士奇", 11, 1.5);
Dog dog2("柯基", 5, 2.6);
Dog dog3("泰迪", 2, 3.6);
Dog dog4("斗牛", 6, 5.8);
Dog dog5("贵宾", 2, 0.7);
Dog dog6("京巴狗", 12, 3.6);
Dog dog7("藏獒", 7, 7.8);
list<Dog> L; //默认构造
L.push_back(dog1);//尾插
L.push_back(dog2);
L.push_front(dog3);//头插
L.push_front(dog4);//头插
L.push_front(dog5);//头插
L.push_back(dog6);
L.push_back(dog7);
printfList(L);
L.sort(UpSortDogForAgeList);
printfList(L);
L.sort(DownSortDogForAgeList);
printfList(L);
system("pause");
return 0;
}
执行如下:可以看出第一组是没有排序的结果,第二组是按年龄升序排列,如果年龄相同,就按体重降序,第三组是按年龄降序,如果年龄相同,就按体重升序。