1.list基本概念
2.list構造函數
#include <iostream>
using namespace std;#include<list>
//鏈表list容器構造函數//輸出list鏈表
void printList(const list<int>& L)
{for (list<int>::const_iterator it = L.begin(); it != L.end(); it++){cout << *it << " ";}cout << endl; //換行
}void test01()
{//創建list容器list<int>L1; //默認構造//添加數據L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);//遍歷容器printList(L1);//區間方式構造list<int>L2(L1.begin(), L1.end());printList(L2);//拷貝構造list<int>L3(L2);printList(L3);//n個elemlist<int>L4(10,1000);printList(L4);
}
int main()
{ test01();//**************************************system("pause");return 0;
}
3.list賦值和交換
#include <iostream>
using namespace std;//list容器的賦值和交互
#include<list>
//遍歷list容器
void printList(const list<int>& L)
{for (list<int>::const_iterator it = L.begin(); it != L.end(); it++){cout << *it << " ";}cout << endl;
}//賦值
void test01()
{//創建list容器list<int>L1;//給L1賦值L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);//遍歷printList(L1);list<int>L2;L2 = L1; //operator= 賦值printList(L2);list<int>L3;L3.assign(L2.begin(), L2.end()); //按區間賦值printList(L3);list<int>L4;L4.assign(10, 1000); //n個elemprintList(L4);
}
//交換
void test02()
{//創建list容器list<int>L1;//給L1賦值L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);list<int>L2;//給L2賦值L2.assign(10, 1000); //n個elemcout << "交換前:" << endl;printList(L1);printList(L2);L1.swap(L2); //交換cout << "交換后:" << endl;printList(L1);printList(L2);
}
int main()
{ test01();cout << endl;test02();//**************************************system("pause");return 0;
}
4.list大小操作
#include <iostream>
using namespace std;//list容器大小操作
#include <list>
void printList(const list<int>& L)
{for (list<int>::const_iterator it = L.begin(); it != L.end(); it++){cout << *it << " ";}cout << endl;
}void test01()
{list<int>L1;L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);printList(L1);//判斷L1是否為空if (L1.empty()){cout << "L1為空" << endl;}else{cout << "L1不為空" << endl;cout << "L1的大小為:" << L1.size() << endl;}//重新指定大小L1.resize(10);printList(L1);//默認值定為了1000L1.resize(15, 1000);printList(L1);L1.resize(5); //把多余的值刪除printList(L1);
}int main()
{ test01();//**************************************system("pause");return 0;
}
5.list插入和刪除
#include <iostream>
using namespace std;//list容器的插入和刪除
#include<list>
void printList(const list<int>& L)
{for (list<int>::const_iterator it = L.begin(); it != L.end(); it++){cout << *it << " ";}cout << endl;
}void test01()
{list<int>L1;//尾插L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);//頭插L1.push_front(10);L1.push_front(20);L1.push_front(30);//30 20 10 10 20 30 40printList(L1);//尾刪L1.pop_back();//30 20 10 10 20 30printList(L1);//頭刪L1.pop_front();//20 10 10 20 30printList(L1);//insert插入L1.insert(L1.begin(), 1000);//1000 20 10 10 20 30printList(L1);//利用it來插入指定位置list<int>::iterator it = L1.begin();L1.insert(++it, 5, 20000);//1000 20000 20000 20000 20000 20000 20 10 10 20 30printList(L1);//刪除it = L1.begin();L1.erase(++it);//1000 20000 20000 20000 20000 20 10 10 20 30printList(L1);//移除L1.remove(20000); //移除所有值為20000的數據//1000 20 10 10 20 30printList(L1);//清空//L1.erase(L1.begin(), L1.end()); 這種區間刪除的方法也可以L1.clear();printList(L1);
}int main()
{ test01();//**************************************system("pause");return 0;
}
6.list數據存取
#include <iostream>
using namespace std;//list容器的數據存取
#include<list>
void test01()
{list<int>L1;L1.push_back(10);L1.push_back(20);L1.push_back(30);//L1[0]; //錯誤 不可以用[]訪問list容器中的元素//L1.at(0); //錯誤 不可以用.at()方式訪問list容器中的元素//原因是list本質鏈表,不是用連續性空間存儲數據,迭代器也是不支持隨機訪問的cout << "第一個元素為:" << L1.front() << endl;cout << "最后一個元素為:" << L1.back() << endl;//list容器的迭代器是雙向迭代器,不支持隨機訪問的list<int>::iterator it = L1.begin();it++;cout << *it << endl; //輸出第二個元素:20it--;cout << *it << endl; //輸出第一個元素:10//it = it + 1; //錯誤 不可以跳躍訪問,即使是+1
}
int main()
{ test01();//**************************************system("pause");return 0;
}
7.list反轉和排序
#include <iostream>
using namespace std;//list容器反轉和排序
#include <list>void printList(const list<int>& L)
{for (list<int>::const_iterator it = L.begin(); it != L.end(); it++){cout << *it << " ";}cout << endl; //換行
}bool myCompare(int v1, int v2)
{// 降序 讓第一個數 > 第二個數return v1 > v2;
}void test01()
{list<int>L;L.push_back(10);L.push_back(30);L.push_back(20);L.push_back(50);L.push_back(40);cout << "反轉前:" << endl;//10 30 20 50 40printList(L);//反轉L.reverse();cout << "反轉后:" << endl;//40 50 20 30 10printList(L);//排序// sort(L.begin(), L.end()); 錯誤// 所有不支持隨機訪問迭代器的容器,不可以用標準算法algorithm// 不支持隨機訪問迭代器的容器,內部會提供對應一些算法//默認升序L.sort(); // 默認排序規則 從小到大 升序cout << "排序后:" << endl;printList(L);//降序L.sort(myCompare); // 降序,指定規則,從大到小printList(L);
}
int main()
{ test01();//**************************************system("pause");return 0;
}
8.排序案例
#include <iostream>
using namespace std;//排序案例
#include <list>
#include <string>//Person類 三國人物
class Person
{
public:Person(string name, int age, float height){this->m_Name = name;this->m_Age = age;this->m_Height = height;}string m_Name;int m_Age;float m_Height;
}; //忘記加 ; 導致一直報錯!! 相對于類的聲明 例如: int a;//遍歷list
void printList(const list<Person>& L)
{for (list<Person>::const_iterator it = L.begin(); it != L.end(); it++){cout << "姓名:" << (*it).m_Name << "\t年齡:"<< (*it).m_Age << "\t身高:" << it->m_Height << endl;}
}//制定排序規則
bool comparePerson(Person& p1, Person& p2)
{//按照年齡 升序if (p1.m_Age == p2.m_Age){//年齡相同 按照身高降序return p1.m_Height > p2.m_Height;}else{return p1.m_Age < p2.m_Age;}
}void test01()
{list<Person>L1; // 創建容器//準備數據Person p1("劉備", 35, 175);Person p2("曹操", 45, 180);Person p3("孫權", 40, 170);Person p4("趙云", 25, 190);Person p5("張飛", 35, 160);Person p6("關羽", 35, 200);//插入數據L1.push_back(p1);L1.push_back(p2);L1.push_back(p3);L1.push_back(p4);L1.push_back(p5);L1.push_back(p6);cout << "排序前:" << endl;printList(L1);cout << "-------------------------" << endl;cout << "排序后:" << endl;//排序:L1.sort(comparePerson);printList(L1);
}int main()
{ test01();//**************************************system("pause");return 0;
}