雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

C++中l(wèi)ist的使用方法及常用list操作總結(jié)

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊:

C++中l(wèi)ist的使用方法及常用list操作總結(jié)

一、List定義:

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

二、List定義和初始化:

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

三、List常用操作函數(shù):

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能容納的最大元素數(shù)量
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的元素倒轉(zhuǎn)
Lst1.size() 返回list中的元素個數(shù)
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創(chuàng)建一個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創(chuàng)建一個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"); 
 
  //測試引用類函數(shù)  
  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); 
} 


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

上一篇:C++ 中回文數(shù)判斷簡單實例

欄    目:C語言

下一篇:c++冒泡排序詳解

本文標題:C++中l(wèi)ist的使用方法及常用list操作總結(jié)

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1510.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有