STL中vector、list、deque和map的區別

vector

    向量 相當於一個數組
    在內存中分配一塊連續的內存空間進行存儲。支持不指定vector大小的存儲。STL內部實現時,首先分配一個非常大的內存空間預備進行存儲,即capacituy()函數返回的大小,
當超過此分配的空間時再整體重新放分配一塊內存存儲,這給人以vector可以不指定vector即一個連續內存的大小的感覺。通常此默認的內存分配能完成大部分情況下的存儲。
   優點:(1) 不指定一塊內存大小的數組的連續存儲,即可以像數組一樣操作,但可以對此數組
               進行動態操作。通常體現在push_back() pop_back()
               (2) 隨機訪問方便,即支持[ ]操作符和vector.at()
               (3) 節省空間。
   缺點:(1) 在內部進行插入刪除操作效率低。
               (2) 只能在vector的最後進行push和pop,不能在vector的頭進行
push和pop。
               (3) 當動態添加的數據超過vector默認分配的大小時要進行整體的重新分配、拷貝與釋
                     放 

list
    雙向鏈表
    每一個結點都包括一個信息快Info、一個前驅指針Pre、一個後驅指針Post。可以不分配必須的內存大小方便的進行添加和刪除操作。使用的是非連續的內存空間進行存儲。
   優點:(1) 不使用連續內存完成動態操作。
               
(2) 在內部方便的進行插入和刪除操作
               (3) 可在兩端進行push、pop
   缺點:(1) 
不能進行內部的隨機訪問,即不支持[ ]操作符和vector.at()
               (2) 相對於verctor佔用內存多

deque

   雙端隊列 double-end queue
   deque是在功能上合併了vector和list。
   優點:(1) 隨機訪問方便,即支持[ ]操作符和vector.at()
               
(2) 在內部方便的進行插入和刪除操作
               (3) 可在兩端進行push、pop
   缺點:
(1) 佔用內存多

使用區別:

     1 如果你需要高效的隨即存取,而不在乎插入和刪除的效率,使用vector 
     2 如果你需要大量的插入和刪除,而不關心隨即存取,則應使用list 
     3 如果你需要隨即存取,而且關心兩端數據的插入和刪除,則應使用deque






C++STL中vector容器的用法 

http://xiamaogeng.blog.163.com/blog/static/1670023742010102494039234/

vector是C++標準模板庫中的部分內容,它是一個多功能的,能夠操作多種數據結構和算法的模板類和函數庫。vector之所以被認爲是一個容器,是因爲它能夠像容器一樣存放各種類型的對象,簡單地說vector是一個能夠存放任意類型的動態數組,能夠增加和壓縮數據。爲了可以使用vector,必須在你的頭文件中包含下面的代碼:

#include <vector>

vector屬於std命名域的,因此需要通過命名限定,如下完成你的代碼:

using std::vector;     vector<int> v;

或者連在一起,使用全名:

std::vector<int> v;

建議使用全局的命名域方式:

using namespace std;

1.vector的聲明

   vector<ElemType> c;   創建一個空的vector

   vector<ElemType> c1(c2); 創建一個vector c1,並用c2去初始化c1

   vector<ElemType> c(n) ; 創建一個含有n個ElemType類型數據的vector;

   vector<ElemType> c(n,elem); 創建一個含有n個ElemType類型數據的vector,並全部初始化爲elem;

   c.~vector<ElemType>(); 銷燬所有數據,釋放資源;

2.vector容器中常用的函數。(c爲一個容器對象)

    c.push_back(elem);   在容器最後位置添加一個元素elem

    c.pop_back();            刪除容器最後位置處的元素

    c.at(index);                返回指定index位置處的元素

    c.begin();                   返回指向容器最開始位置數據的指針

    c.end();                      返回指向容器最後一個數據單元的指針+1

    c.front();                     返回容器最開始單元數據的引用

    c.back();                     返回容器最後一個數據的引用

    c.max_size();              返回容器的最大容量

    c.size();                      返回當前容器中實際存放元素的個數

    c.capacity();               同c.size()

     c.resize();                   重新設置vector的容量

    c.reserve();                同c.resize()

    c.erase(p);               刪除指針p指向位置的數據,返回下指向下一個數據位置的指針(迭代器)

    c.erase(begin,end)     刪除begin,end區間的數據,返回指向下一個數據位置的指針(迭代器)

    c.clear();                    清除所有數據

    c.rbegin();                  將vector反轉後的開始指針返回(其實就是原來的end-1)

    c.rend();                     將vector反轉後的結束指針返回(其實就是原來的begin-1)

    c.empty();                   判斷容器是否爲空,若爲空返回true,否則返回false

    c1.swap(c2);               交換兩個容器中的數據

    c.insert(p,elem);          在指針p指向的位置插入數據elem,返回指向elem位置的指針       

    c.insert(p,n,elem);      在位置p插入n個elem數據,無返回值

    c.insert(p,begin,end) 在位置p插入在區間[begin,end)的數據,無返回值

3.vector中的操作

    operator[] 如: c.[i];

    同at()函數的作用相同,即取容器中的數據。

在上大致講述了vector類中所含有的函數和操作,下面繼續討論如何使用vector容器;

1.數據的輸入和刪除。push_back()與pop_back()

C++STL中vector容器的用法 - 夏茂庚 - 夏茂庚
2.元素的訪問

C++STL中vector容器的用法 - 夏茂庚 - 夏茂庚
3.排序和查詢

C++STL中vector容器的用法 - 夏茂庚 - 夏茂庚

4.二維容器

C++STL中vector容器的用法 - 夏茂庚 - 夏茂庚


C++ STL List隊列用法(實例)

http://www.cnblogs.com/madlas/articles/1364503.html

C++ STL List隊列用法(實例)
2007-12-15 12:54

#include <iostream>
#include <list>
#include <numeric>
#include <algorithm>

using namespace std;

//創建一個list容器的實例LISTINT
typedef list<int> LISTINT;

//創建一個list容器的實例LISTCHAR
typedef list<char> LISTCHAR;

void main(void)
{
    //--------------------------
    //用list容器處理整型數據
    //--------------------------
    //用LISTINT創建一個名爲listOne的list對象
    LISTINT listOne;
    //聲明i爲迭代器
    LISTINT::iterator i;

    //從前面向listOne容器中添加數據
    listOne.push_front (2);
    listOne.push_front (1);

    //從後面向listOne容器中添加數據
    listOne.push_back (3);
    listOne.push_back (4);

    //從前向後顯示listOne中的數據
    cout<<"listOne.begin()--- listOne.end():"<<endl;
    for (i = listOne.begin(); i != listOne.end(); ++i)
        cout << *i << " ";
    cout << endl;

    //從後向後顯示listOne中的數據
LISTINT::reverse_iterator ir;
    cout<<"listOne.rbegin()---listOne.rend():"<<endl;
    for (ir =listOne.rbegin(); ir!=listOne.rend();ir++) {
        cout << *ir << " ";
    }
    cout << endl;

    //使用STL的accumulate(累加)算法
    int result = accumulate(listOne.begin(), listOne.end(),0);
    cout<<"Sum="<<result<<endl;
    cout<<"------------------"<<endl;

    //--------------------------
    //用list容器處理字符型數據
    //--------------------------

    //用LISTCHAR創建一個名爲listOne的list對象
    LISTCHAR listTwo;
    //聲明i爲迭代器
    LISTCHAR::iterator j;

    //從前面向listTwo容器中添加數據
    listTwo.push_front ('A');
    listTwo.push_front ('B');

    //從後面向listTwo容器中添加數據
    listTwo.push_back ('x');
    listTwo.push_back ('y');

    //從前向後顯示listTwo中的數據
    cout<<"listTwo.begin()---listTwo.end():"<<endl;
    for (j = listTwo.begin(); j != listTwo.end(); ++j)
        cout << char(*j) << " ";
    cout << endl;

    //使用STL的max_element算法求listTwo中的最大元素並顯示
    j=max_element(listTwo.begin(),listTwo.end());
    cout << "The maximum element in listTwo is: "<<char(*j)<<endl;
}

#include <iostream>
#include <list>

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;  
    //list2對象最初有10個值爲6的元素
    INTLIST list2(10,6);
    //list3對象最初有3個值爲6的元素
    INTLIST list3(list2.begin(),--list2.end());

    //聲明一個名爲i的雙向迭代器
    INTLIST::iterator i;

    //從前向後顯示各list對象的元素
    put_list(list1,"list1");
    put_list(list2,"list2");
    put_list(list3,"list3");
   
//從list1序列後面添加兩個元素
list1.push_back(2);
list1.push_back(4);
cout<<"list1.push_back(2) and list1.push_back(4):"<<endl;
    put_list(list1,"list1");

//從list1序列前面添加兩個元素
list1.push_front(5);
list1.push_front(7);
cout<<"list1.push_front(5) and list1.push_front(7):"<<endl;
    put_list(list1,"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序列的前後各移去一個元素
list1.pop_front();
list1.pop_back();
cout<<"list1.pop_front() and list1.pop_back():"<<endl;
    put_list(list1,"list1");

//清除list1中的第2個元素
list1.erase(++list1.begin());
cout<<"list1.erase(++list1.begin()):"<<endl;
    put_list(list1,"list1");

//對list2賦值並顯示
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;

//list序列容器的運算
    put_list(list1,"list1");
    put_list(list3,"list3");
cout<<"list1>list3: "<<(list1>list3)<<endl;
cout<<"list1<list3: "<<(list1<list3)<<endl;

//對list1容器排序
list1.sort();
    put_list(list1,"list1");
   
//結合處理
list1.splice(++list1.begin(), list3);
    put_list(list1,"list1");
    put_list(list3,"list3");
}






http://www.cppblog.com/vontroy/archive/2010/05/16/115501.html

map映照容器的元素數據是一個鍵值和一個映照數據組成的,鍵值與映照數據之間具有一一映照的關係。
        map映照容器的數據結構是採用紅黑樹來實現的,插入鍵值的元素不允許重複,比較函數只對元素的鍵值進行比較,元素的各項數據可通過鍵值檢索出來。
        使用map容器需要頭文件包含語句“#include<map>”, map文件也包含了對multimap多重映照容器的定義。
        
1、map創建、元素插入和遍歷訪問
        
創建map對象,鍵值與映照數據的類型由自己定義。在沒有指定比較函數時,元素的插入位置是按鍵值由小到大插入到黑白樹中去的,下面這個程序詳細說明了如何操作map容器。
 1#include <map>
 2#include <string>
 3#include <iostream>
 4
 5using std :: cout ;
 6using std :: endl ;
 7using std :: string ;
 8using std :: map ;
 9
10int main()
11{
12     //定義map對象,當前沒有任何元素
13     map<string,float> m ;
14     
15     //插入元素,按鍵值的由小到大放入黑白樹中
16     m["Jack"] = 98.5 ;
17     m["Bomi"] = 96.0 ;
18     m["Kate"] = 97.5 ;
19     
20     //先前遍歷元素
21     map<string,float> :: iterator it ;
22     for(it = m.begin() ; it != m.end() ; it ++)
23     {
24          cout << (*it).first << " : " << (*it).second << endl ;
25     }

26     
27     return 0 ;
28}

29
        運行結果:
                          Bomi :96
                          Jack  :98.5
                          Kate  :97.5
        程序編譯試,會產生代號爲“warning C4786” 的警告, “4786” 是標記符超長警告的代號。可以在程序的頭文件包含代碼的前面使用"#pragma waring(disable:4786)" 宏語句,強制編譯器忽略該警告。4786號警告對程序的正確性和運行並無影響。
2、刪除元素
        map映照容器的 erase() 刪除元素函數,可以刪除某個迭代器位置上的元素、等於某個鍵值的元素、一個迭代器區間上的所有元素,當然,也可使用clear()方法清空map映照容器。
        下面這個程序演示了刪除map容器中鍵值爲28的元素:
 1#include <map>
 2#include <string>
 3#include <iostream>
 4
 5using std :: cout ;
 6using std :: endl ;
 7using std :: string ;
 8using std :: map ;
 9
10int main()
11{
12    //定義map對象,當前沒有任何元素
13    map<intchar> m ;
14    //插入元素,按鍵值的由小到大放入黑白樹中
15    m[25] = 'm' ;
16    m[28] = 'k' ;
17    m[10] = 'x' ;
18    m[30] = 'a' ;
19    //刪除鍵值爲28的元素
20    m.erase(28) ;
21    //向前遍歷元素
22    map<intchar> :: iterator it ;
23    for(it = m.begin() ; it != m.end() ; it ++)
24    {
25        //輸出鍵值與映照數據
26        cout << (*it).first << " : " << (*it).second << endl ;
27    }

28    return 0 ;
29}

30
運行結果:
                     10 : x
                     25 : m
                     30 : a
3、元素反向遍歷
      可以用反向迭代器reverse_iterator反向遍歷map映照容器中的數據,它需要rbegin()方法和rend()方法指出反向遍歷的起始位置和終止位置。
 1#include <map>
 2#include <string>
 3#include <iostream>
 4
 5using std :: cout ;
 6using std :: endl ;
 7using std :: string ;
 8using std :: map ;
 9
10int main()
11{
12    //定義map對象,當前沒有任何元素
13    map<intchar> m ;
14    //插入元素,按鍵值的由小到大放入黑白樹中
15    m[25] = 'm' ;
16    m[28] = 'k' ;
17    m[10] = 'x' ;
18    m[30] = 'a' ;
19    //反向遍歷元素
20    map<intchar> :: reverse_iterator rit ;
21    for( rit = m.rbegin() ; rit != m.rend() ; rit ++)
22    {
23        //輸入鍵值與映照數據
24        cout << (*rit).first << " : " << (*rit).second << endl ;
25    }

26    return 0 ;
27}

28
運行結果:
                  30 : a
                  28 : k
                  25 : m
                  10 : x
4、元素的搜索
       
使用find()方法來搜索某個鍵值,如果搜索到了,則返回該鍵值所在的迭代器位置,否則,返回end()迭代器位置。由於map採用黑白樹數據結構來實現,所以搜索速度是極快的。
       下面這個程序搜索鍵值爲28的元素:
 1#include <map>
 2#include <string>
 3#include <iostream>
 4
 5using std :: cout ;
 6using std :: endl ;
 7using std :: string ;
 8using std :: map ;
 9
10int main()
11{
12    //定義map對象,當前沒有任何元素
13    map<intchar> m ;
14    //插入元素,按鍵值的由小到大放入黑白樹中
15    m[25] = 'm' ;
16    m[28] = 'k' ;
17    m[10] = 'x' ;
18    m[30] = 'a' ;
19    map<intchar> :: iterator it ;
20    it = m.find(28) ;
21    if(it != m.end())  //搜索到該鍵值
22            cout << (*it).first << " : " << ( *it ).second << endl ;
23    else
24            cout << "not found it" << endl ;
25    return 0 ;
26}

27
5、自定義比較函數
        
將元素插入到map中去的時候,map會根據設定的比較函數將該元素放到該放的節點上去。在定義map的時候,如果沒有指定比較函數,那麼採用默認的比較函數,即按鍵值由小到大的順序插入元素。在很多情況下,需要自己編寫比較函數。
        編寫方法有兩種。
        (1)如果元素不是結構體,那麼,可以編寫比較函數。下面這個程序編寫的比較規則是要求按鍵值由大到小的順序將元素插入到map中
 1#include <map>
 2#include <string>
 3#include <iostream>
 4
 5using std :: cout ;
 6using std :: endl ;
 7using std :: string ;
 8using std :: map ;
 9
10//自定義比較函數 myComp
11struct myComp
12{
13    bool operator() (const int &a, const int &b)
14    {
15        if(a != b) return a > b ;
16        else  return a > b ;
17    }

18}
 ;
19
20int main()
21{
22    //定義map對象,當前沒有任何元素
23    map<intchar> m ;
24    //插入元素,按鍵值的由小到大放入黑白樹中
25    m[25] = 'm' ;
26    m[28] = 'k' ;
27    m[10] = 'x' ;
28    m[30] = 'a' ;
29    //使用前向迭代器中序遍歷map
30    map<intchar,myComp> :: iterator it ;
31    for(it = m.begin() ; it != m.end() ; it ++)
32            cout << (*it).first << " : " << (*it).second << endl ;
33    return 0 ;
34}

35
運行結果:
                  30 :a
                  28 :k
                  25 :m
                  10 :x
       (2)如果元素是結構體,那麼,可以直接把比較函數寫在結構體內。下面的程序詳細說明了如何操作:
 1#include <map>
 2#include <string>
 3#include <iostream>
 4
 5using std :: cout ;
 6using std :: endl ;
 7using std :: string ;
 8using std :: map ;
 9
10struct Info
11{
12    string name ;
13    float score ;
14    //重載 “<”操作符,自定義排列規則
15    bool operator < (const Info &a) const
16    {
17        //按score由大到小排列。如果要由小到大排列,使用“>”號即可
18        return a.score < score ;
19    }

20}
 ;
21
22int main()
23{
24    //定義map對象,當前沒有任何元素
25    map<Info, int> m ;
26    //定義Info結構體變量
27    Info info ;
28    //插入元素,按鍵值的由小到大放入黑白樹中
29    info.name = "Jack" ;
30    info.score = 60 ;
31    m[info] = 25 ;
32    info.name = "Bomi" ;
33    info.score = 80 ;
34    m[info] = 10 ;
35    info.name = "Peti" ;
36    info.score = 66.5 ;
37    m[info] = 30 ;
38    //使用前向迭代器中序遍歷map
39    map<Info,int> :: iterator it ;
40    for(it = m.begin() ; it != m.end() ; it ++)
41    {
42            cout << (*it).second << " : " ;
43            cout << ((*it).first).name << " : " << ((*it).first).score << endl ;
44    }

45    return 0 ;
46}

47
運行結果:
                  10 :Bomi   80
                  30 :Peti     66.5
                  25 :Jack    60
6、用map實現數字分離
      對數字的各位進行分離,採用取餘等數學方法是很耗時的。而把數字當成字符串,使用map的映照功能,很方便地實現了數字分離。下面這個程序將一個字符串中的字符當成數字,並將各位的數值相加,最後輸出各位的和。
 1#include <string>
 2#include <map>
 3#include <iostream>
 4
 5using std :: cout ;
 6using std :: endl ;
 7using std :: string ;
 8using std :: map ;
 9
10int main()
11{
12    //定義map對象,當前沒有任何元素
13    map<charint> m ;
14
15    //賦值:字符映射數字
16    m['0'] = 0 ;
17    m['1'] = 1 ;
18    m['2'] = 2 ;
19    m['3'] = 3 ;
20    m['4'] = 4 ;
21    m['5'] = 5 ;
22    m['6'] = 6 ;
23    m['7'] = 7 ;
24    m['8'] = 8 ;
25    m['9'] = 9 ;
26    /**//*上面的10條賦值語句可採用下面這個循環簡化代碼編寫
27    for(int j = 0 ; j < 10 ; j++)
28    {
29            m['0' + j] = j ;
30    }
31    */

32    string sa, sb ;
33    sa = "6234" ;
34    int i ;
35    int sum = 0 ;
36    for ( i = 0 ; i < sa.length() ; i++ )
37            sum += m[sa[i]] ;
38    cout << "sum = " << sum << endl ;
39    return 0 ;
40}

41
7、數字映照字符的map寫法
      
在很多情況下,需要實現將數字映射爲相應的字符,看看下面的程序:
 1#include <string>
 2#include <map>
 3#include <iostream>
 4
 5using std :: cout ;
 6using std :: endl ;
 7using std :: string ;
 8using std :: map ;
 9
10int main()
11{
12    //定義map對象,當前沒有任何元素
13    map<intchar> m ;
14
15    //賦值:字符映射數字
16    m[0] = '0' ;
17    m[1] = '1' ;
18    m[2] = '2' ;
19    m[3] = '3' ;
20    m[4] = '4' ;
21    m[5] = '5' ;
22    m[6] = '6' ;
23    m[7] = '7' ;
24    m[8] = '8' ;
25    m[9] = '9' ;
26    /**//*上面的10條賦值語句可採用下面這個循環簡化代碼編寫
27    for(int j = 0 ; j < 10 ; j++)
28    {
29            m[j] = '0' + j ;
30    }
31    */

32    int n = 7 ;
33    string s = "The number is " ;
34    cout << s + m[n] << endl ;
35    return 0 ;
36}

37
運行結果:
                  The number is 7
發佈了216 篇原創文章 · 獲贊 11 · 訪問量 30萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章