c++ STL 整理20200409

感謝https://www.cnblogs.com/pullself/p/10049657.html#jump


C++ 常用STL整理

容器和配接器

list(鏈表)

list可以認爲是一個線性的雙向鏈表,具有鏈表的特性,不使用連續的內存空間,可以快速的插入和刪除,不支持隨機的內部訪問。使用需包含include<list>頭文件,std命名空間。

常用使用方法:

1.創建實例,迭代器


 

int a[] = { 1,2,3,4,5 }; list<int> lt; list<int>::iterator it;//創建迭代器 list<int> lt(a, a + 5); list<int> lt(2, 100);

2.輸入輸出,插入刪除,迭代器遍歷


 

//push_back,push_front(插入尾,插入頭)
int a;
cin >> a;
lt.push_back(a);
lt.push_front(a);

//pop_back,pop_front(刪除尾,刪除頭)
lt.pop_back();
lt.pop_front();

//assign(插入)
list<int> first;
list<int> second;
first.assgin(2,100);//添加2個100的元素
second.assgin(first.begin(),first.end())//將first拷貝給second

//insert(指定位置插入)
/*
iterator insert (iterator position, const value_type& val);  
//position是要插入的這個list的迭代器,val是要插入的值
void insert (iterator position, size_type n, const value_type& val);  
//從該list容器中的position位置處開始,插入n個值爲val的元素
template <class InputIterator>
void insert (iterator position, InputIterator first, InputIterator last);  
//first,last是我們選擇的把值插入到這個list中的值所在的容器的迭代器
*/
list<int> lt;
list<int>::iterator it;
it = lt.begin();
lt.insert(it, 2);
lt.insert(it, 2, 100);
lt.insert(it, sth.begin(), sth.end());
//在指定位置插入某容器的一個區段

//遍歷
list<int> lt;
list<int>::iterator it;
for (it = lt.begin(); it != lt.end(); it++) cout << *it;

3.常用成員函數

//erase(刪除元素)erase函數是可以有返回值的,注意當刪除元素的同時,迭代器也被銷燬了。
lt.erase(iterator it);//刪除it位置的元素
lt.erase(iterator begin,iterator end);//刪除一定區間的元素

//swap(交換)
list<int> first;
list<int> second;
first.swap(second);

//clear(清空)
lt.clear();

//splice(轉移元素)
/*
void splice (iterator position, list& x); 
//將列表x中的所有元素移到當前list中,從當前列表的position指向的位置開始,此時列表x爲空
void splice (iterator position, list& x, iterator i);  
//將列表x中迭代器 i 指向的元素移到當前list的position指向的位置處,由於i指向的元素從列表x中被移,所以迭代器 i 此時是invalid的;position是當前列表的迭代器,i是列表x的迭代器
void splice (iterator position, list& x, iterator first, iterator last);  
//將列表x中[first, last)的元素移到當前list中,從position指向的位置開始;first,last是列表x的迭代器
*/

//remove(移除指定元素)
/*
void remove (const value_type& val);  
//從list中刪除所有值爲val的元素
*/
lt.remove(100);

//unique(刪除重複值)
/*
void unique();  
//只能刪除相鄰的重複元素,然後保留第一個值,因此這個函數只對排好序的list有用
*/

//sort(排序) 默認升序,可自寫cmp函數
lt.sort(cmp);

//reverse(逆序)
lt.reserve();

//merge(合併有序的list)
list<int> first;
list<int> second;
first.merge(second);

//remove_if(按條件移除元素)
bool single_digit (const int& value) { return (value < 10); }
lt.remove_if (single_digit);

GO TO TOP


stack(棧)

stack就是一個標準的棧,後進先出,不能遍歷。使用需包含include<stack>頭文件,std命名空間。

常用使用方法:

int a;
stack<int> st;//創建實例
cin >> a;
st.push(a);//ru'zhan
a = st.top();//返回入棧
st.pop();出棧
bool b = st.empty();//判斷棧是否爲空
a = st.size();//返回棧長度

GO TO TOP

queue(隊列)

queue先進先出,不能遍歷。使用需包含include<queue>頭文件,std命名空間。

常用使用方法:

int a;
queue<int> que;//創建實例
cin >> a;
que.push(a);//進隊
a = que.front();//返回隊頭
a = que.back();//返回隊尾
que.pop();//出隊
bool b = que.empty();//隊列是否爲空
a = que.size();//返回隊列長度

GO TO TOP

priority_queue(優先隊列)

可以認爲是隊列的一種,但是會按照一種優先規律,將優先級最高的元素始終置於隊頭。底層通過heap(堆)來實現,所以默認爲一個大根堆。

常用使用方法:

struct node
{
    int x,y;
    bool operator < (const node & a) const
    {
        return x<a.x;
    }
};


int a;
priority_queue <int> que;//創建實例,默認降序
priority_queue <int, vector<int>, greater<int> > que2;//升序
priority_queue <node>;//重載小於,可以利用重載小於來自定義優先級
priority_queue
cin >> a;
que.push(a);//進隊
a = que.top();//返回隊頭
que.pop();//出隊
bool b = que.empty();//隊列是否爲空
a = que.size();//返回隊列長度

GO TO TOP


set(集合)

set內部通過紅黑樹實現,實現了一個自動排序,元素值唯一的容器。查找的複雜度爲(logn),set中的元素值不能直接被修改,在其中的查找屬於二分查找。使用需包含include<set>頭文件,std命名空間。

常用使用方法:
1.創建實例,迭代器


 

set <int> se;

set <int>::iterator it;

2.插入刪除(insert依然可以插入一段元素)

//insert(插入)
int a;
set <int> se;
cin >> a;
se.insert(a);
//erase(刪除)
se.erase(iterator it);

3.常用成員函數

//find(查找某個值)
se.find(2);//返回2所在的迭代器,否則返回end()

//lower_bound(查找第一個大於等於key的值)upper_bound(查找第一個大於key的值)
se.lower_bound(2);
se.upper.bound(2);

4.mutiset(可重複插入的set)

mutiset <int> se;
//count(返回某一鍵值出現次數,set中使用此函數只會返回1或0)
int a = se.count(2);

GO TO TOP


vector(向量)

可以將其近似的認爲是一個動態的數組。使用需包含include<vector>頭文件,std命名空間。

常用使用方法:
1.創建實例,迭代器

vector <int> vec;
vector <int> vec(10,1);//創建了有10個元素的向量,並賦初值爲1
int b[7]={1,2,3,4,5,9,8};vector<int> a(b,b+7);
vector <int>::iterator it;

2.插入刪除,訪問

//push_back(尾部插入一個元素)
vec.push_back(a);

//insert(插入一個元素)
vec.insert(position,elem)    //在pos位置插入一個elem拷貝,傳回新數據位置。
vec.insert(position,n,elem)    //在pos位置插入n個elem數據,無返回值。
vec.insert(position,begin,end)    //在pos位置插入在[beg,end)區間的數據,無返回值。

//erase(刪除指定位置的元素)
vec.erase(iterator it);

//[]訪問,由於vector重載了[],所以可以利用[]直接訪問已有元素
cout << vec[1];

//at(返回指定位置的元素)
vec.at(1);//at()函數具有檢測是否越界的功能,如果越界會拋出錯誤,所以安全性高於[]

//迭代器訪問
vector <int> vec;
vector <int>::iterator it;
for (it = vec.begin(); it != vec.end(); it++) cout << *it;

3.常用成員函數

//vec.clear();//清空

//vec.back(),vec.front(),vec.empty()//返回末尾,返回頭,判斷是否爲空

//vec.begin(),vec.end()//傳回對應位置的迭代器

//vector作爲容器,可以使用較多algorithm中的函數,例如sort,reverse,swap。

GO TO TOP


map&&pair(關聯)

map內部也是通過紅黑樹實現的,map的形式爲一個鍵值對,和set一樣查找的複雜度爲(logn)可以修改value值,但不能修改key值。使用需包含include<map>頭文件,std命名空間。

常用使用方法:
1.創建實例,迭代器

map <int,string> mp;//創建了一個以int爲key,string爲value的鍵值對。
map <int,string>::iterator it; 

2.插入刪除

//insert(插入)//注意前兩種只能在map內無此元素的時候插入,而最後一種可以實現覆蓋再賦值
mp.insert(make_pair(1, "one"));//利用make_pair函數構造出一對關聯量插入
mp.insert(map<int, string>::value_type(1, "one"));//插入map的value_type數據
mp[1] = "one";//利用重載[]輸入賦值

//earse(刪除)
mp.erase(iterator it);//通過一個條目對象刪除
mp.erase(iterator first,iterator last)//刪除一個範圍
int n = erase(const Key&key);//通過關鍵字刪除,刪除成功n==1.否則==0

3.常用成員函數

//find(查找某個值)
mp.find(1);//返回key所在的迭代器,否則返回end()

//lower_bound(查找第一個大於等於key的值)upper_bound(查找第一個大於key的值)
mp.lower_bound(1);
mp.upper.bound(1);

//count(返回某一key值出現次數,map中使用此函數只會返回1或0)
int a = mp.count(1);

GO TO TOP


常用算法

#include<algorthm>

//sort(快速排序)stable_sort(穩定排序)
sort(start,end,排序方法);

//reserve(反轉容器)
reserve(vec.begin(),vec.end());

//lower_bound,upper_bound(二分查找)//返回的是位置,前提要有序
int num[6]={1,2,4,7,15,34}; 
sort(num,num+6);
int pos1=lower_bound(num,num+6,7)-num;    //返回數組中第一個大於或等於被查數的值 
int pos2=upper_bound(num,num+6,7)-num;    //返回數組中第一個大於被查數的值

//集合操作(前提容器序列有序)
includes(s1.begin(), s1.end(), s2.begin(), s2.end());
//s1是否包含s2,遞增序列使用less<int>(),遞減序列使用greater<int>()。
set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(s3));
//求並集,並輸入到支持insert操作的s3中,也可以使用back_inserter(s3)輸入到支持push_back操作的s3
set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(s3));
//求交集
set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(s3));
//求差集

//堆操作
make_heap(begin(),end());//對一個序列建立一個堆,默認大根堆,greater<int>()則是小根堆
pop_heap(begin(),end());//將堆頂元素移到序列末尾,一般搭配pop_back();使用
push_heap(begin(), end());//有新元素插入序列末尾後的加入操作。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章