c++中list容器
生活随笔
收集整理的這篇文章主要介紹了
c++中list容器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
list概念
注意:
1 采用動態存儲分配,不會造成內存浪費和溢出
2 鏈表執行插入和刪除操作十分方便,修改指針即可,不需要移動大量元素
3 鏈表靈活,但是空間和時間額外耗費較大
4 list容器不僅是一個鏈表,而且是一個循環的雙向鏈表
list 對象的默認構造
list 增添數據操作
list 的數據存取
list 與迭代器
list 對象的帶參數構造
list 的賦值
list 的大小
list.size(); //返回容器中元素的個數
list.empty(); //判斷容器是否為空
list.resize(num); //重新指定容器的長度為 num,若容器變長,則以默認值填充新 位置。如果容器變短,則末尾超出容器長度的元素被刪除。
list.resize(num,elem); //重新指定容器的長度為 num,若容器變長,則以 elem 值填 充新位置。如果容器變短,則末尾超出容器長度的元素被刪除。
void test03() {list<int>L3;L3.push_back(10);L3.push_back(30);L3.push_back(20);L3.push_front(100);L3.push_front(300);L3.push_front(200);cout << "大小:" << L3.size() << endl;if (L3.empty()){cout << "L3為空" << endl;}else{cout << "L3不為空" << endl;}L3.resize(10);printList(L3);L3.resize(3);printList(L3);list<int>L4;L4.assign(L3.begin(), L3.end());cout << "front" << L4.front() << endl;cout << "back" << L4.back() << endl; }list 的插入
list 的刪除
list.clear(); //移除容器的所有數據
list.erase(beg,end); //刪除[beg,end)區間的數據,返回下一個數據的位置。
list.erase(pos); //刪除 pos 位置的數據,返回下一個數據的位置。
lst.remove(elem); //刪除容器中所有與 elem 值匹配的元素。
void test02() {list<int>L(10,10);list<int>L2(L.begin(), L.end());printList(L);printList(L2);L2.push_back(100);//逆序打印for (list<int>::reverse_iterator it = L2.rbegin(); it != L2.rend(); it++){cout << *it << " ";}cout << endl;//list迭代器不支持隨機訪問list<int>::iterator itBegin = L2.begin();//itBegin = itBegin + 1;//插入數據list<int>L3;L3.push_back(10);L3.push_back(30);L3.push_back(20);L3.push_front(100);L3.push_front(300);L3.push_front(200);printList(L3); //200 300 100 10 30 20//刪除兩端數據L3.pop_front();//頭刪L3.pop_back();//尾刪printList(L3);//打印后結果300 100 10 30L3.insert(L3.begin(), 1000);//1000 300 100 10 30printList(L3);L3.push_back(10);L3.remove(10);//參數,直接放值,所有值等于參數的值都被刪除printList(L3);//1000 300 100 30}list 的反序排列
lst.reverse(); //反轉鏈表,比如 lst 包含 1,3,5 元素,運行此方法后,lst 就包含 5,3,1 元素。
bool myCompare(int v1, int v2){return v1 > v2;}void test04(){list<int>L;L.push_back(10);L.push_back(20);L.push_back(40);L.push_back(30);L.reverse();printList(L);//30 40 20 10//sort(L.begin(), L.end());//所有不支持隨機訪問的迭代器,不可以用系統提供的算法//如果不支持用系統提供算法,那么這個類內部會提供L.sort();printList(L);//從小到大排序//從大到小排序,用回調函數L.sort(myCompare);printList(L);}//自定義數據類型的處理class Person{public:Person(string name, int age,int height){this->m_Name = name;this->m_Age = age;this->m_Height = height;}string m_Name;int m_Age;int m_Height;//身高};//Person的排序規則,如果年齡相同 按照身高的升序排序bool myComparePerson(Person &p1, Person &p2){/*if (p1.m_Age > p2.m_Age){return true;}return false;*///如果年齡相同按身高排序if (p1.m_Age == p2.m_Age){return p1.m_Height < p2.m_Height;}else{return p1.m_Age>p2.m_Age;}}void test05(){list<Person>L;Person p1("曹操", 10,165);Person p2("銳雯", 20,170);Person p3("女槍", 17,177);Person p4("德雷克斯", 19,120);Person p5("MT", 18,200);Person p6("狗蛋", 18, 166);Person p7("狗剩", 18, 210);L.push_back(p1);L.push_back(p2);L.push_back(p3);L.push_back(p4);L.push_back(p5);L.push_back(p6);L.push_back(p7);//要求:按照年齡降序輸出//對于自定義數據類型,必須指定排序規則//排序時,還要用到回調函數L.sort(myComparePerson);for (list<Person>::iterator it = L.begin(); it != L.end(); it++){cout << "姓名:" << it->m_Name << " 年齡:" << it->m_Age <<"身高: "<<it->m_Height<< endl;}}list下remove刪除自定義數據類型數據
remove函數的原型
==號兩邊連接的時普通數據類型
class Person { public:Person(string name, int age,int height){this->m_Name = name;this->m_Age = age;this->m_Height = height;}//重載==讓remove可以刪除自定義的Person類型bool operator==(const Person &p){if (this->m_Name == p.m_Name&&this->m_Age == p.m_Age&&this->m_Height == p.m_Height){return true;}return false;}string m_Name;int m_Age;int m_Height;//身高 }; //刪除 狗蛋L.remove(p6);for (list<Person>::iterator it = L.begin(); it != L.end(); it++){cout << "姓名:" << it->m_Name << " 年齡:" << it->m_Age << "身高: " << it->m_Height << endl;}總結
以上是生活随笔為你收集整理的c++中list容器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么使用js将json文件转成csv文件
- 下一篇: c++中的set容器和multiset容