目录
1.list基本概念
2.list构造函数
3.list的赋值和交换
4.list大小操作
5.list的插入的删除
6.list数据存取
7.list反转和排序
排序案例
1.list基本概念
2.list构造函数
#include<bits/stdc++.h>
using namespace std;void print(const list<int> &lk)
{for(list<int>::const_iterator it = lk.begin(); it != lk.end(); it++){cout << *it << " ";}cout << endl;}int main()
{list<int> lk1;lk1.push_back(78);lk1.push_back(98);lk1.push_back(59);print(lk1);list<int>lk2(lk1.begin(),lk1.end());print(lk2);list<int>lk3(lk2);print(lk3);list<int>lk4(3,99);print(lk4);return 0;
}
3.list的赋值和交换
#include<bits/stdc++.h>
using namespace std;void print(const list<int> &lk)
{for(list<int>::const_iterator it = lk.begin(); it != lk.end(); it++){cout << *it << " ";}cout << endl;}int main()
{list<int> lk1;lk1.push_back(78);lk1.push_back(98);lk1.push_back(59);print(lk1);list<int>lk2;lk2 = lk1;print(lk2);list<int>lk3;lk3.assign(lk2.begin(),lk2.end());print(lk3);list<int>lk4;lk4.assign(3,999);print(lk4);cout << "交换前:lk3 ";print(lk3);lk3.swap(lk4);cout << "交换后:lk3 ";print(lk3);return 0;
}
4.list大小操作
#include<bits/stdc++.h>
using namespace std;void print(const list<int> &lk)
{for(list<int>::const_iterator it = lk.begin(); it != lk.end(); it++){cout << *it << " ";}cout << endl;}int main()
{list<int> lk1;lk1.push_back(78);lk1.push_back(98);lk1.push_back(59);print(lk1);cout << lk1.empty() << endl;cout << lk1.size() << endl;lk1.resize(5,100);print(lk1);//指定长度比原来长默认用0来填充//指定长度比原来短会删除多余的部分return 0;
}
5.list的插入的删除
#include<bits/stdc++.h>
using namespace std;void print(const list<int> &lk)
{for(list<int>::const_iterator it = lk.begin(); it != lk.end(); it++){cout << *it << " ";}cout << endl;}int main()
{list<int> lk1;lk1.push_back(78);lk1.push_back(98);lk1.push_back(59);lk1.push_front(334);lk1.push_front(45);print(lk1);lk1.pop_back();print(lk1);lk1.pop_front();print(lk1);list<int>::iterator it = lk1.begin();it++;lk1.insert(it,1000);print(lk1);lk1.erase(lk1.begin());print(lk1);lk1.push_back(78);lk1.remove(78);print(lk1);//刪除所有的78lk1.clear();print(lk1);cout << "結束" << endl;return 0;
}
6.list数据存取
#include<bits/stdc++.h>
using namespace std;void print(const list<int> &lk)
{for(list<int>::const_iterator it = lk.begin(); it != lk.end(); it++){cout << *it << " ";}cout << endl;}int main()
{list<int> lk1;lk1.push_back(78);lk1.push_back(98);lk1.push_back(59);lk1.push_front(334);lk1.push_front(45);print(lk1);//不支持用[]和at方式訪問//也不能用it = it + 1,但可以it++和it--//因為不支持迭代器的隨機訪問cout << lk1.front() << endl;cout << lk1.back() << endl;return 0;
}
7.list反转和排序
#include<bits/stdc++.h>
using namespace std;void print(const list<int> &lk)
{for(list<int>::const_iterator it = lk.begin(); it != lk.end(); it++){cout << *it << " ";}cout << endl;}bool cmp(int v1,int v2)
{return v1 > v2;//降序就讓第一個數大於第二個數
}int main()
{list<int> lk1;lk1.push_back(78);lk1.push_back(98);lk1.push_back(59);lk1.push_front(334);lk1.push_front(45);print(lk1);lk1.reverse();print(lk1);//所有不支持隨機訪問迭代器的容器,不可以用標準算法//它內部會提供算法lk1.sort();cout << "默認從小到大:" << endl;print(lk1);lk1.sort(cmp);cout << "更改後變為從大到小:" << endl;print(lk1);return 0;
}
排序案例
#include<bits/stdc++.h>
using namespace std;class person
{
public:person(string name,int age,int height){this -> name = name;this -> age = age;this -> height = height;}string name;int age;int height;
};bool cmp(person &p1,person &p2)
{if(p1.age == p2.age) return p1.height > p2.height;else return p1.age < p2.age;
}int main()
{list<person> l;person p1("熊貓",3,180);person p2("企鵝",5,160);person p3("老虎",3,200);l.push_back(p1);l.push_back(p2);l.push_back(p3);for(list<person>::iterator it = l.begin(); it != l.end(); it++){//小括號這裡必須加cout << (*it).name << ' ' << (*it).age << ' ' << (*it).height << endl;}cout << "排序後" << endl;cout << "--------------------------" << endl;l.sort(cmp);//自定义数据类型必须指定排序规则for(list<person>::iterator it = l.begin(); it != l.end(); it++){//小括號這裡必須加cout << (*it).name << ' ' << (*it).age << ' ' << (*it).height << endl;}return 0;
}