国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++中list的使用方法及常用list操作總結

2020-01-26 14:06:39
字體:
來源:轉載
供稿:網友

C++中list的使用方法及常用list操作總結

一、List定義:

List是stl實現的雙向鏈表,與向量(vectors)相比, 它允許快速的插入和刪除,但是隨機訪問卻比較慢。使用時需要添加頭文件
#include <list>

二、List定義和初始化:

    list<int>lst1;          //創建空list
    list<int> lst2(5);       //創建含有5個元素的list
    list<int>lst3(3,2);  //創建含有3個元素的list
    list<int>lst4(lst2);    //使用lst2初始化lst4
    list<int>lst5(lst2.begin(),lst2.end());  //同lst4

三、List常用操作函數:

Lst1.assign() 給list賦值
Lst1.back() 返回最后一個元素
Lst1.begin() 返回指向第一個元素的迭代器
Lst1.clear() 刪除所有元素
Lst1.empty() 如果list是空的則返回true
Lst1.end() 返回末尾的迭代器
Lst1.erase() 刪除一個元素
Lst1.front() 返回第一個元素
Lst1.get_allocator() 返回list的配置器
Lst1.insert() 插入一個元素到list中
Lst1.max_size() 返回list能容納的最大元素數量
Lst1.merge() 合并兩個list
Lst1.pop_back() 刪除最后一個元素
Lst1.pop_front() 刪除第一個元素
Lst1.push_back() 在list的末尾添加一個元素
Lst1.push_front() 在list的頭部添加一個元素
Lst1.rbegin() 返回指向第一個元素的逆向迭代器
Lst1.remove() 從list刪除元素
Lst1.remove_if() 按指定條件刪除元素
Lst1.rend() 指向list末尾的逆向迭代器
Lst1.resize() 改變list的大小
Lst1.reverse() 把list的元素倒轉
Lst1.size() 返回list中的元素個數
Lst1.sort() 給list排序
Lst1.splice() 合并兩個list
Lst1.swap() 交換兩個list
Lst1.unique() 刪除list中重復的元素

四、List使用示例:

示例1:遍歷List

    //迭代器法

for(list<int>::const_iteratoriter = lst1.begin();iter != lst1.end();iter++)  {   cout<<*iter;  }  cout<<endl; 

示例2:

#include <iostream> #include <list> #include <numeric> #include <algorithm> #include <windows.h> using namespace std;   typedef list<int> LISTINT; typedef list<int> LISTCHAR;   void main() {   //用LISTINT創建一個list對象   LISTINT listOne;   //聲明i為迭代器   LISTINT::iterator i;     listOne.push_front(3);   listOne.push_front(2);   listOne.push_front(1);     listOne.push_back(4);   listOne.push_back(5);   listOne.push_back(6);     cout << "listOne.begin()--- listOne.end():" << endl;   for (i = listOne.begin(); i != listOne.end(); ++i)     cout << *i << " ";   cout << endl;     LISTINT::reverse_iterator ir;   cout << "listOne.rbegin()---listOne.rend():" << endl;   for (ir = listOne.rbegin(); ir != listOne.rend(); ir++) {     cout << *ir << " ";   }   cout << endl;     int result = accumulate(listOne.begin(), listOne.end(), 0);   cout << "Sum=" << result << endl;   cout << "------------------" << endl;     //用LISTCHAR創建一個list對象   LISTCHAR listTwo;   //聲明i為迭代器   LISTCHAR::iterator j;     listTwo.push_front('C');   listTwo.push_front('B');   listTwo.push_front('A');     listTwo.push_back('D');   listTwo.push_back('E');   listTwo.push_back('F');     cout << "listTwo.begin()---listTwo.end():" << endl;   for (j = listTwo.begin(); j != listTwo.end(); ++j)     cout << char(*j) << " ";   cout << endl;     j = max_element(listTwo.begin(), listTwo.end());   cout << "The maximum element in listTwo is: " << char(*j) << endl;   Sleep(10000); }   


#include <iostream>  #include <list>  #include <windows.h>  using namespace std; typedef list<int> INTLIST;  //從前向后顯示list隊列的全部元素  void put_list(INTLIST list, char *name) {   INTLIST::iterator plist;    cout << "The contents of " << name << " : ";   for (plist = list.begin(); plist != list.end(); plist++)     cout << *plist << " ";   cout << endl; }  //測試list容器的功能  void main(void) {   //list1對象初始為空    INTLIST list1;   INTLIST list2(5, 1);   INTLIST list3(list2.begin(), --list2.end());    //聲明一個名為i的雙向迭代器    INTLIST::iterator i;    put_list(list1, "list1");   put_list(list2, "list2");   put_list(list3, "list3");    list1.push_back(7);   list1.push_back(8);   cout << "list1.push_back(7) and list1.push_back(8):" << endl;   put_list(list1, "list1");    list1.push_front(6);   list1.push_front(5);   cout << "list1.push_front(6) and list1.push_front(5):" << endl;   put_list(list1, "list1");    list1.insert(++list1.begin(), 3, 9);   cout << "list1.insert(list1.begin()+1,3,9):" << endl;   put_list(list1, "list1");    //測試引用類函數    cout << "list1.front()=" << list1.front() << endl;   cout << "list1.back()=" << list1.back() << endl;    list1.pop_front();   list1.pop_back();   cout << "list1.pop_front() and list1.pop_back():" << endl;   put_list(list1, "list1");    list1.erase(++list1.begin());   cout << "list1.erase(++list1.begin()):" << endl;   put_list(list1, "list1");    list2.assign(8, 1);   cout << "list2.assign(8,1):" << endl;   put_list(list2, "list2");    cout << "list1.max_size(): " << list1.max_size() << endl;   cout << "list1.size(): " << list1.size() << endl;   cout << "list1.empty(): " << list1.empty() << endl;    put_list(list1, "list1");   put_list(list3, "list3");   cout << "list1>list3: " << (list1 > list3) << endl;   cout << "list1<list3: " << (list1 < list3) << endl;    list1.sort();   put_list(list1, "list1");    list1.splice(++list1.begin(), list3);   put_list(list1, "list1");   put_list(list3, "list3");   Sleep(10000); } 


感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 衡阳市| 安阳县| 曲周县| 岳池县| 鄯善县| 老河口市| 阳江市| 舟山市| 湄潭县| 牙克石市| 肥西县| 沅江市| 西和县| 称多县| 宿迁市| 临潭县| 钦州市| 巴彦淖尔市| 樟树市| 合川市| 汉源县| 太康县| 丹东市| 长武县| 兰考县| 宿州市| 苍山县| 黄大仙区| 陵水| 长治市| 青阳县| 新民市| 浮梁县| 依兰县| 枣阳市| 安溪县| 德令哈市| 锦州市| 九江市| 长沙县| 霍林郭勒市|