C++中 STL list詳解及簡單實例
C++中 STL list詳解
1、List: 內(nèi)部實現(xiàn)是一個雙向鏈表,可以高效的進(jìn)行插入刪除,但不能夠進(jìn)行隨機(jī)訪問
2.、示例程序:
#include "stdafx.h"
#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>
using namespace std;
const int num[5] = {1,3,2,4,5};
bool status(const int & value)
{
return value>6?true:false;
}
int _tmain(int argc, _TCHAR* argv[])
{
list<int> list1;
copy(num,num+5,back_insert_iterator<list<int>>(list1));
copy(list1.begin(),list1.end(),ostream_iterator<int>(cout," "));
cout<<endl;
list1.sort(greater<int>());//5 4 3 2 1
copy(list1.begin(),list1.end(),ostream_iterator<int>(cout," "));
cout<<endl;
list<int>::iterator it = list1.begin();
while (it != list1.end())
{
(*it) += 2;
it++;
}
//7 6 5 4 3
list<int>::reverse_iterator re_it = list1.rbegin();
cout<<"從后向前輸出: ";
while (re_it != list1.rend())
{
cout<<*re_it<<" ";
re_it++;
}
cout<<endl;
list1.reverse();// 3 4 5 6 7
list1.push_back(8);//3 4 5 6 7 8
list1.pop_front();//4 5 6 7 8
list1.remove(6);//4 5 7 8
list1.remove_if(status);// 4 5
list1.resize(4);// 4 5 0 0
list1.resize(6,1);// 4 5 0 0 1 1
list1.unique();//4 5 0 1
copy(list1.begin(),list1.end(),ostream_iterator<int>(cout," "));
cout<<endl;
list1.clear();
cout<<"當(dāng)前l(fā)ist1含有元素個數(shù):"<<list1.size()<<endl;
list1.push_back(7);//list1:7
list<int> list2(3,2);//2 2 2
list2.merge(list1,greater<int>());//list2: 7 2 2 2
list2.insert(++list2.begin(),3);//list2: 7 3 2 2 2
list2.swap(list1);//list1:7 3 2 2 2 list2:empty
list1.erase(++list1.begin(),list1.end());// 7
copy(list1.begin(),list1.end(),ostream_iterator<int>(cout," "));
cout<<endl;
system("pause");
}
運行結(jié)果圖片:

3、List 方法
|
list成員 |
說明 |
|
constructor |
構(gòu)造函數(shù) |
|
destructor |
析構(gòu)函數(shù) |
|
operator= |
賦值重載運算符 |
|
assign |
分配值 |
|
front |
返回第一個元素的引用 |
|
back |
返回最后一元素的引用 |
|
begin |
返回第一個元素的iterator |
|
end |
返回最后一個元素的下一位置的iterator |
|
rbegin |
返回鏈表最后一元素的后向指針reverse_iterator |
|
rend |
返回鏈表第一元素的下一位置的reverse_iterator |
|
push_back |
增加一個數(shù)據(jù)到鏈表尾 |
|
push_front |
增加一個數(shù)據(jù)到鏈表頭 |
|
pop_back |
刪除鏈表尾的一個元素 |
|
pop_front |
刪除鏈表頭的一元素 |
|
clear |
刪除所有元素 |
|
erase |
刪除一個元素或一個區(qū)域的元素(兩個重載) |
|
remove |
刪除鏈表中匹配值的元素(匹配元素全部刪除) |
|
remove_if |
刪除條件滿足的元素(遍歷一次鏈表),參數(shù)為自定義的回調(diào)函數(shù) |
|
empty |
判斷是否鏈表為空 |
|
max_size |
返回鏈表最大可能長度 |
|
size |
返回鏈表中元素個數(shù) |
|
resize |
重新定義鏈表長度(兩重載函數(shù)) |
|
reverse |
反轉(zhuǎn)鏈表 |
|
sort |
對鏈表排序,默認(rèn)升序 |
|
merge |
合并兩個有序鏈表并使之有序 |
|
splice |
對兩個鏈表進(jìn)行結(jié)合(三個重載函數(shù)) 結(jié)合后第二個鏈表清空 |
|
insert |
在指定位置插入一個或多個元素(三個重載函數(shù)) |
|
swap |
交換兩個鏈表(兩個重載) |
|
unique |
刪除相鄰重復(fù)元素 |
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
C++實現(xiàn)對輸入數(shù)字組進(jìn)行排序
這里給大家介紹的是通過某個方法實現(xiàn)判斷命令行中輸入的數(shù)字是幾個,這樣再用冒泡法排序的時候就不用擔(dān)心輸入的是幾個數(shù)字,用到的知識主要是冒泡法排序2015-11-11
C++實現(xiàn)LeetCode(208.實現(xiàn)字典樹(前綴樹))
這篇文章主要介紹了C++實現(xiàn)LeetCode(208.實現(xiàn)字典樹(前綴樹)),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
c++中l(wèi)og4cplus日志庫使用的基本步驟和示例代碼
這篇文章主要給大家介紹了關(guān)于c++中l(wèi)og4cplus日志庫使用的相關(guān)資料,log4cplus是一款開源的c++日志庫,具有線程安全,靈活,以及多粒度控制的特點,log4cplus可以將日志按照優(yōu)先級進(jìn)行劃分,使其可以面向程序的調(diào)試,運行,測試,后期維護(hù)等軟件全生命周期,需要的朋友可以參考下2024-06-06
C++使struct對象擁有可變大小的數(shù)組(詳解)
下面小編就為大家?guī)硪黄狢++使struct對象擁有可變大小的數(shù)組(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12

