程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C++容器與迭代器

C++容器與迭代器

編輯:關於C++

* 容器的迭代器還有幾種:

+ iterator:正常迭代器(常用)

+ reverse_iterator:反向迭代器(有時也用)

- rbegin(),rend()//返回反向迭 代器

+ const_iterator:常量迭代器

+ const_reverse_iterator:

 iterator find(數據){
 for( 從beg;!=end;it ++)
  if(*it==數據)
   return it;
 return end;//未找到,返回無效迭代器
}//查詢
*it = new_data;//更新迭代器

--------------------------------

所有的容器在插入數據時會自動加長,程序員不必關心空間問題。

容器的分類:

+ (sequence)

- vector

- list

- deque

序列式容器 共性

構造函數:constructor(int n);constructor(int n,T val)

調整 大小:resize(int n),resize(int n,T val)一般用來加長,不一定能縮短

賦 值:assign(n,val),放入n個相同的值

assign(區間(beg——end) ,val),把指定區間的內容放入容器

插入:insert(pos/*迭代器*/,n,val), insert(pos,區間)

毛插:push_back(val);//在末尾插入新數據

取得首 尾元素:front(),back()

尾刪:pop_back();//刪除尾部元素

----- ------------------------------

序列式容器不同點vector 類似於數組,也支持 '[]',不做越界檢查,但更安全;能自動增長。

vector.at(下標);//如 果越界會throw異常,返回的是引用

vector.reserve(n)//保留空間(沒必要用)

vector.capacity()//目前容量(沒必要用),用size()

vector只適合在 末尾插入和刪除數據

vector的查找效率高而list低

list對於頻繁的插入與刪 除做了優化。

凡是標准庫拋出的異常都可以用exception類來catch

 //vector例子
#include <iostream>
using namespace std;
#include <vector>

int main()
{
        vector<int> vi;
        cout << "Input scores,end by -1:";
        int s;
        int m=0;
        for(;;){
                cin >> s;
                if(s==-1) break;
                vi.push_back(s);
                if(s>m)
                        m = s;
        }
        int inc = 100-m;
        for(int i=0; i<vi.size(); i++)
                vi[i] += inc;
        for(int i=0; i<vi.size(); i++)
                cout << vi[i] << ' ';
        cout << endl;
        try{
        cout << "vi[1000]=" << vi[1000] << endl;
        cout << "vi.at(1000)=" << vi.at(1000) << endl;
        }catch(exception& e){
                cout << e.what() << endl;
        }
}

------------------------------------

list支持push_front() ,push_back(),pop_front(),pop_back()

list不支持'[]'和at() ,因為它不是連續存放的。

標准模板庫裡比大小都用<,比等於用 ==

  c1.splice(pos,c2)//轉移,就是挪走了
c1.splice(pos,c2,it)//把c2中it位置的元素轉移到c1的pos處
c1.splice(pos,c2,區間)//把c2裡的區間轉移到c1的pos
c1.merge(c2)//自動歸並排序到合適的位置
remove(val)//刪除指定值的元素

 //list.cc
#include <iostream>
using namespace std;
#include <list>

int main()
{
        int cpp[5] = {3,5,6,8,9};
        int java[8] = {1,2,3,4,5,6,7,8};
        int Unix[4] = {3,4,5,6};
        list<int> li;
        li.insert(li.begin(),cpp,cpp+5);
        li.insert(li.begin(),java,java+8);
        li.insert(li.begin(),unix,unix+4);
        li.sort();//排序
        li.unique();//刪除相鄰的重復的元素,只留一份
        list<int>::iterator it = li.begin();
        while(it!=li.end())
                cout << *it++ << ' ';
        cout << endl;
}

 deque []  .at()
deque.push_front(val);
deque.pop_front();

---------------------------------

+ 關聯式

關聯式迭代器的共性:

- 插入不用指定位置:insert(val),會自動排序

- 查找一個指定的數據:find(val)返回指向元素的迭代器

如未找到會返回 end()無效迭代器

multimap,multiset有一個統計函數:count(val)//返回val的 個數

以下兩個函數只對允許重復才會有意義

ib = lower_bound(val)//返回 val在容器中的第一個位置

ie = upper_bound(val)//返回val在容器中的最後一個 位置的下一個位置

while(ib!=ie)

cout << *ib++;

還有一 些人喜歡另一種方式:equal_range(val)//返回一個pair<迭,迭>

重復   元素    不同點

-----------------------------------------------------

map   N    pair<key,value>    支持[key]

multimap   Y    pair<key,value>      N

set   N key     N

multiset   Y key     N

------------------------------------- ----------------

 //map.cc
#include <iostream>
#include <map>
using namespace std;
#include <string>

int main()
{
 map<int, string> m;
 m.insert(make_pair(1001,"李明"));
 m.insert(make_pair(1002,"王國"));
 m.insert(make_pair(1005,"失小"));
 m[1006] = "劉華";
 m.insert(make_pair(1001,"娜娜"));
 map::iterator it;
 it = m.begin();
 while(it!=it.end()){
  cout << it->first << ':' << it->second << endl;
  ++ it;//會比it++性能高
 }
}

對於set,map來說重復的插入被忽略.

//multimap
//一切操作都是對key而言的
//key一定要支持小於運算符,不然是不能進行排序的
#include <iostream>
using namespace std;
#include <map>
#include <string>

int main()
{
        typedef multimap<string,string> M;
        M mss;
        M::iterator ib,ie;
        mss.insert(make_pair("a","b"));
        mss.insert(make_pair("c","d"));
        mss.insert(make_pair("e","f"));
        mss.insert(make_pair("c","p"));
        mss.insert(make_pair("a","m"));
        mss.insert(make_pair("c","d"));
        mss.insert(make_pair("a","p"));
        ib = mss.begin();
        ie = mss.end();
        while(ib!=ie){
                cout << ib->first <<',' << ib->second << endl;
                ++ ib;
        }//end while
        cout << "a count : " << mss.count ("a") << endl;
        cout << "c count : " << mss.count ("c") << endl;
        ib = mss.lower_bound("c");
        ie = mss.upper_bound("c");
        while(ib!=ie){
                cout << ib->first <<',' << ib->second << endl;
                ++ ib;
        }//end while

}//end main

//set
//同樣的插入同樣被忽略
#include <iostream>
using namespace std;
#include <set>

int main()
{
        set<int> si;
        int userid[5] = {1,3,3,4,5};
        for(int i=0;i<5; i++)
                si.insert(userid[i]);
        set<int>::iterator it;
        it = si.begin();
        while(it!=si.end())
                cout << *it++ << ' ';
        cout << endl;
        cout << "user 3 : " << (si.find(3)!=si.end ()) << endl;
        cout << "user 9 : " << (si.find(9)!=si.end ()) << endl;
}

//multiset
//用的是平衡二叉樹,所以它的查找效率是相當高的.
#include <iostream>
using namespace std;
#include <set>

template<typename Iter>
void show(Iter ib, Iter ie)
{
        while(ib!=ie)
                cout << *ib++ << ' ';
        cout << endl;
}
int main()
{
        int a[5] = {5,1,7,5,1};
        multiset<int> pids(a,a+5);
        show(pids.begin(),pids.end());
        pids.insert(7);
        pids.insert(7);
        pids.insert(7);
        pids.erase(pids.find(5));
        show(pids.begin(),pids.end());
        cout << "end process 7..." << endl;
        pids.erase(7);
        show(pids.begin(),pids.end());
}//end main

=================================================

* STL Algorithms

+ 幾乎所用的算法都工作在某個區間

+ Search:for_each(beg, end,fun),find(beg,end,data),find_first_of

(),find_end() ……

+ Sort:sort(),reverse()……

+ Copy :copy(),copy_backward()……

+ Modify:replace(beg,end, oldv,newv),merge(),remove()/*假刪除,與erase()

一起用來實現真正的 刪除*/,c.erase(remove(……),c.end());//都這麼用+ Numeric: min(,max(),count(),swap(),accumulate()

容器適配器

+ stack<類型>(push,pop,size,empty,clear,top)

+ queue<類型> (push,pop,size,empty,clear,front,back)

+ priority_queue<類型> (優先隊列)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved