一、stack:http://www.javashuo.com/article/p-xbgfnpjf-gn.htmlhtml
使用該容器時須要包含#include<stack>頭文件;ios
定義stack對象的示例代碼以下:c++
stack<int>s1;面試
stack<string>s2;算法
stack的基本操做有:shell
1.入棧:如s.push(x);api
2.出棧:如 s.pop().注意:出棧操做只是刪除棧頂的元素,並不返回該元素。數組
3.訪問棧頂:如s.top();less
4.判斷棧空:如s.empty().當棧空時返回true。函數
5.訪問棧中的元素個數,如s.size();
#include<iostream> #include<stack> using namespace std; int main(void) { stack<double>s;//定義一個棧 for(int i=0;i<10;i++) s.push(i); while(!s.empty()) { printf("%lf\n",s.top()); s.pop(); } cout<<"棧內的元素的個數爲:"<<s.size()<<endl; return 0; }
二、排序sort函數:http://www.javashuo.com/article/p-beuozbxx-gg.html
須要頭文件<algorithm>,sort 使用時得註明:using namespace std; 或直接打 std::sort()
語法描述:sort(begin,end,cmp),cmp參數能夠沒有,若是沒有默認升序排序。
sort是一個改進版的qsort,能用sort儘可能用sort
#include<iostream> #include<algorithm> #include<cstring> using namespace std; int main() { int a[5]={1,3,4,2,5}; sort(a,a+5); for(int i=0;i<5;i++) cout<<a[i]<<' '; return 0; } 由於沒有cmp參數,默認爲非降序排序,結果爲: 1 2 3 4 5
可自定義compare函數設定排序規則, 以下按降序排序
#include<iostream> #include<algorithm> #include<cstring> using namespace std; bool cmp(int a,int b) { return a>b; } int main() { int a[5]={1,3,4,2,5}; sort(a,a+5); for(int i=0;i<5;i++) cout<<a[i]<<' '; return 0; } 輸出: 5 4 3 2 1
快排Qsort:https://blog.csdn.net/forckgcs/article/details/2291489
定義在頭文件<algorithm>中
void qsort ( void * base, size_t nmem, size_t size, int ( * comp) ( const void * , const void * ) ) ;
它根據comp所指向的函數所提供的順序對base所指向的數組進行排序,nmem爲參加排序的元素個數,size爲每一個元素所佔的字節數。如:對一個長爲1000的數組進行排序時,int a[1000]; 那麼base應爲a,num應爲 1000,width應爲 sizeof(int),cmp函數隨本身的命名
如要對元素進行升序排列,則定義comp所指向的函數爲:若是其第一個參數比第二個參數小,則返回一個小於0的值,反之則返回一個大於0的值,若是相等,則返回0。
# include < stdio. h> # include < stdlib. h> int comp( const void * , const void * ) ; int main( int argc, char * argv[ ] ) { int i; int array[ ] = { 6, 8, 2, 9, 1, 0} ; qsort ( array, 6, sizeof ( int ) , comp) ; for ( i = 0; i < 6; i + + ) { printf ( "%d/t" , array[ i] ) ; } printf ( "/n" ) ; return 0; } int comp( const void * p, const void * q) { return ( * ( int * ) p - * ( int * ) q) ; } 運行結果以下: 0 1 2 6 8 9
三、vector:http://www.cnblogs.com/Nonono-nw/p/3462183.html
vector可直接做爲數組調用,可是若是要在函數中修改其值,那麼函數的形參必定要爲引用,即vector<type>&array,不然值可能不會被修改
vector 是向量類型,能夠容納許多類型的數據,如若干個整數,因此稱其爲容器。使用它時須要包含頭文件:#include<vector>;
初始化: (1) vector<int> a(10); //定義了10個整型元素的向量(尖括號中爲元素類型名,它能夠是任何合法的數據類型),但沒有給出初值,其值是不肯定的。 (2)vector<int> a(10,1); //定義了10個整型元素的向量,且給出每一個元素的初值爲1 (3)vector<int> a(b); //用b向量來建立a向量,總體複製性賦值 (4)vector<int> a(b.begin(),b.begin+3); //定義了a值爲b中第0個到第2個(共3個)元素 (5)int b[7]={1,2,3,4,5,9,8}; vector<int> a(b,b+7); //從數組中得到初值
重要操做: (1)a.assign(b.begin(), b.begin()+3); //b爲向量,將b的0~2個元素構成的向量賦給a (2)a.assign(4,2); //是a只含4個元素,且每一個元素爲2 (3)a.back(); //返回a的最後一個元素 (4)a.front(); //返回a的第一個元素 (5)a[i]; //返回a的第i個元素,當且僅當a[i]存在2013-12-07 (6)a.clear(); //清空a中的元素 (7)a.empty(); //判斷a是否爲空,空則返回ture,不空則返回false (8)a.pop_back(); //刪除a向量的最後一個元素 (9)a.erase(a.begin()+1,a.begin()+3); //刪除a中第1個(從第0個算起)到第2個元素,也就是說刪除的元素從a.begin()+1算起(包括它)一直到a.begin()+ 3(不包括它) (10)a.push_back(5); //在a的最後一個向量後插入一個元素,其值爲5 (11)a.insert(a.begin()+1,5); //在a的第1個元素(從第0個算起)的位置插入數值5,如a爲1,2,3,4,插入元素後爲1,5,2,3,4 (12)a.insert(a.begin()+1,3,5); //在a的第1個元素(從第0個算起)的位置插入3個數,其值都爲5 (13)a.insert(a.begin()+1,b+3,b+6); //b爲數組,在a的第1個元素(從第0個算起)的位置插入b的第3個元素到第5個元素(不包括b+6),如b爲1,2,3,4,5,9,8 ,插入元素後爲1,4,5,9,2,3,4,5,9,8 (14)a.size(); //返回a中元素的個數; (15)a.capacity(); //返回a在內存中總共能夠容納的元素個數 (16)a.resize(10); //將a的現有元素個數調至10個,多則刪,少則補,其值隨機 (17)a.resize(10,2); //將a的現有元素個數調至10個,多則刪,少則補,其值爲2 (18)a.reserve(100); //將a的容量(capacity)擴充至100,也就是說如今測試a.capacity();的時候返回值是100.這種操做只有在須要給a添加大量數據的時候才 顯得有意義,由於這將避免內存屢次容量擴充操做(當a的容量不足時電腦會自動擴容,固然這必然下降性能) (19)a.swap(b); //b爲向量,將a中的元素和b中的元素進行總體性交換 (20)a==b; //b爲向量,向量的比較操做還有!=,>=,<=,>,<
從現有向量中選擇元素向向量中添加: int a[6]={1,2,3,4,5,6}; vector<int> b; vector<int> c(a,a+4); for(vector<int>::iterator it=c.begin();it<c.end();it++) b.push_back(*it);
#include<iostream> #include<vector> using namespace std; int main() { //array用來保存一個3*3的二維數組,array的每一個元素都是vector<int>類型 vector <vector<int> >array; std::vector<int> v; for (int i = 0; i <3; i++){ for (int j = 0; j <3; j++){ int value; cin >> value; v.push_back(value); } array.push_back(v); //保存array的每一個元素 v.clear(); } for (int i = 0; i <array.size(); i++) { for (int j = 0; j <3; j++) cout <<array[i][j]; cout<<endl; } return 0; }
#include<iostream> #include<vector> using namespace std; int main() { vector <vector<int> >array(3);//首先給array開闢了三個空間 for (int i = 0; i <3; i++){ array[i].resize(3);//給array中每一個元素開闢了三個空間 for (int j = 0; j <3; j++){ cin >> array[i][j];//直接對開闢的空間賦值便可 } } for (int i = 0; i <array.size(); i++) { for (int j = 0; j <3; j++) cout <<array[i][j]; cout<<endl; } cout << array.size(); return 0; }
四、queue:https://blog.csdn.net/fengzhizi76506/article/details/54809949
C++隊列queue模板類的定義在<queue>頭文件中,queue 模板類須要兩個模板參數,一個是元素類型,一個容器類型,元素類型是必要的,容器類型是可選的,默認爲deque 類型(兩端均可以進出的隊列類型)
queue 的基本操做舉例以下:
queue入隊,如例:q.push(x); 將x 接到隊列的末端。
queue出隊,如例:q.pop(); 彈出隊列的第一個元素,注意,並不會返回被彈出元素的值。
訪問queue隊首元素,如例:q.front(),即最先被壓入隊列的元素。
訪問queue隊尾元素,如例:q.back(),即最後被壓入隊列的元素。
判斷queue隊列空,如例:q.empty(),當隊列空時,返回true。
訪問隊列中的元素個數,如例:q.size()
#include <cstdlib> #include <iostream> #include <queue> using namespace std; int main() { int e,n,m; queue<int> q1; for(int i=0;i<10;i++) q1.push(i); if(!q1.empty()) cout<<"not empty\n"; n=q1.size(); cout<<n<<endl; m=q1.back(); cout<<m<<endl; for(int j=0;j<n;j++) { e=q1.front(); cout<<e<<" "; q1.pop(); } cout<<endl; system("PAUSE"); return 0; }
五、map 使用
map的基本操做函數:
begin() 返回指向map頭部的迭代器
clear() 刪除全部元素
count() 返回指定元素出現的次數
empty() 若是map爲空則返回true
end() 返回指向map末尾的迭代器
equal_range() 返回特殊條目的迭代器對
erase() 刪除一個元素
find() 查找一個元素
get_allocator() 返回map的配置器
insert() 插入元素
key_comp() 返回比較元素key的函數
lower_bound() 返回鍵值>=給定元素的第一個位置
max_size() 返回能夠容納的最大元素個數
rbegin() 返回一個指向map尾部的逆向迭代器
rend() 返回一個指向map頭部的逆向迭代器
size() 返回map中元素的個數
swap() 交換兩個map
upper_bound() 返回鍵值>給定元素的第一個位置
value_comp() 返回比較元素value的函數
1. map最基本的構造函數;
map<string , int >mapstring; map<int ,string >mapint;
map<sring, char>mapstring; map< char ,string>mapchar;
map<char ,int>mapchar; map<int ,char >mapint;
2. map添加數據;
map<int ,string> maplive;
1.maplive.insert(pair<int,string>(102,"aclive"));
2.maplive.insert(map<int,string>::value_type(321,"hai"));
3, maplive[112]="April";//map中最簡單最經常使用的插入添加
3,map中元素的查找:
find()函數返回一個迭代器指向鍵值爲key的元素,若是沒找到就返回指向map尾部的迭代器。
map<int ,string >::iterator l_it;
l_it=maplive.find(112);
if(l_it==maplive.end())
cout<<"we do not find 112"<<endl;
else cout<<"wo find 112"<<endl;
4,map中元素的刪除:
若是刪除112;
map<int ,string >::iterator l_it;
l_it=maplive.find(112);
if(l_it==maplive.end())
cout<<"we do not find 112"<<endl;
else maplive.erase(l_it); //delete 112;
六、一個fork的面試題 :關於fork和緩衝區(\n會刷出緩衝區)
七、set集合
set集合是c++ stl庫中自帶的一個容器,set具備如下兩個特色:一、set中的元素都是排好序的 二、set集合中沒有重複的元素
multiset和set的區別是:multiset中能夠有相同的元素,set和multiset都是基於紅黑樹實現
經常使用操做:
begin() 返回set容器的第一個元素的地址
end() 返回set容器的最後一個元素地址
clear() 刪除set容器中的全部的元素
empty() 判斷set容器是否爲空
max_size() 返回set容器可能包含的元素最大個數
size() 返回當前set容器中的元素個數
erase(it) 刪除迭代器指針it處元素,注意這裏刪除multiset中的某個元素時須要使用迭代器it, 不然直接erase(x)表示刪除multiset集合中全部的x
insert(a) 插入某個元素
#include<stdio.h> #include<set> using namespace std; int main() { set<int>s; s.insert(3); s.insert(1); s.insert(2); s.insert(1); set<int>::iterator it; for(it=s.begin();it!=s.end();it++) //使用迭代器進行遍歷 { printf("%d\n",*it); } return 0; } //輸出結果 : 1 2 3 一共插入了4個數,可是集合中只有3個數而且是有序的,可見以前說過的set集合的兩個特色,有序和不重複。
八、stringstream:分割被空格、製表符等分割的字符串
#include <string> #include <iostream> #include <sstream> using namespace std; int main() { string input; getline(cin, input);//讀入一行,默認遇到\n回車時中止 stringstream ss(input); string str1, str2; ss >> str1;//遇到空格中止 ss >> str2; //空格後賦值給str2 } 輸入hello world str1爲hello str2爲world
九、string : 用法
使用string首先要在頭文件當中加入< string >
string s;//默認初始化,一個空字符串 string s1("ssss");//s1是字面值「ssss」的副本 string s2(s1);//s2是s1的副本 string s3=s2;//s3是s2的副本 string s4(10,'c');//把s4初始化 string s5="hiya";//拷貝初始化 string s6=string(10,'c');//拷貝初始化,生成一個初始化好的對象,拷貝給s6 //string s(cp,n) char cs[]="12345"; string s7(cs,3);//複製字符串cs的前3個字符到s當中 //string s(s2,pos2) string s8="asac"; string s9(s8,2);//從s2的第二個字符開始拷貝,不能超過s2的size //string s(s2,pos2,len2) string s10="qweqweqweq"; string s11(s10,3,4);//s4是s3從下標3開始4個字符的拷貝,超過s3.size出現未定義
char*和string的轉換:使用
to_string包含在# include<string>, 做用是把數值類型如int、double、long等轉化爲string
兩個string的比較能夠直接使用> < ==.
十、堆 用法
C++中堆的應用:make_heap, pop_heap, push_heap, sort_heap
函數說明:
std::make_heap將[start, end)範圍進行堆排序,默認使用less, 即最大元素放在第一個。
std::pop_heap將front(即第一個最大元素)移動到end的前部,同時將剩下的元素從新構形成(堆排序)一個新的heap。
std::push_heap對剛插入的(尾部)元素作堆排序。
std::sort_heap將一個堆作排序,最終成爲一個有序的系列,能夠看到sort_heap時,必須先是一個堆(兩個特性:一、最大元素在第一個 二、添加或者刪除元素以對數時間),所以必須先作一次make_heap.
make_heap, pop_heap, push_heap, sort_heap都是標準算法庫裏的模板函數,用於將存儲在vector/deque 中的元素進行堆操做。
11 list 用法
assign() 給list賦值 back() 返回最後一個元素 begin() 返回指向第一個元素的迭代器 clear() 刪除全部元素 empty() 若是list是空的則返回true end() 返回末尾的迭代器 erase() 刪除一個元素 front() 返回第一個元素 get_allocator() 返回list的配置器 insert() 插入一個元素到list中 max_size() 返回list能容納的最大元素數量 merge() 合併兩個list pop_back() 刪除最後一個元素 pop_front() 刪除第一個元素 push_back() 在list的末尾添加一個元素 push_front() 在list的頭部添加一個元素 rbegin() 返回指向第一個元素的逆向迭代器 remove() 從list刪除元素 remove_if() 按指定條件刪除元素 rend() 指向list末尾的逆向迭代器 resize() 改變list的大小 reverse() 把list的元素倒轉 size() 返回list中的元素個數 sort() 給list排序 splice() 合併兩個list swap() 交換兩個list unique() 刪除list中重複的元素