原帖地址:http://hi.baidu.com/yanfei_1/blog/item/a0a538331f5256f91a4cffba.html
C++的標準模板庫(Standard Template Library,簡稱STL)是一個容器和演算法的類庫。容器往往包含同一類型的資料。STL中比較常用的容器是vector,set和map,比較常用的演算法有Sort等。
.
一. vector
1.聲明:
一個vector類似於一個動態一維數組。
vector<int> a; //聲明一個元素為int類型的vector a
vectot<MyType> a; //聲明一個元素為MyType類型的vector a
這裡的聲明的a包含0個元素,既a.size()的值為0,但它是動態,其大小會隨著資料的插入
和刪除改變而改變。
vector<int> a(100, 0); //這裡聲明的是一已經個存放了100個0的整數vector2.向量操作
常用函數:
size_t size(); // 返回vector的大小,即包含的元素個數
void pop_back(); // 刪除vector末尾的元素,vector大小相應減一
void push_back(); //用於在vector的末尾添加元素
T back(); // 返回vector末尾的元素
void clear(); // 將vector清空,vector大小變為0
其他訪問方式:
cout<<a[5]<<endl;
cout<<a.at(5)<<endl;
以上區別在於後者在訪問越界時會拋出異常,而前者不會。例:
int intarray[10];
vector<int> first_vector(intarray, intarray + 10);
vector<int> second_vector(first_vector.begin(),first_vector.end());
class man
{
public:
AnsiStirng id;
AnsiString mc;
}
vector<man> manList;
man thisman;
thisman.id="2001";
thisman.name="yourname";
manList.push_back thisman; //加入第一個元素
thisman.id="2002";
thisman.name="myname";
manList.push_back thisman; //加入第二個元素
manList.clear(); //清空 3.遍曆 (1). for(vector<datatype>::iterator it=a.begin(); it!=a.end();it++) cout<<*it<<endl; (2). for(int i=0;i<a.size;i++) cout<<a[i]<<endl;二. map
Map是STL的一個關聯容器,它提供一對一(其中第一個可以稱為關鍵字,每個關鍵字只能在map中出現一次,第二個可能稱為該關鍵字的值)的資料處理能力,由於這個特性
map內部的實現自建一顆紅/黑樹狀結構(一種非嚴格意義上的平衡二叉樹),這顆樹具有對資料自動排序的功能。
下面舉例說明什麼是一對一的資料對應。比如一個班級中,每個學生的學號跟他的姓名就存在著一一映射的關係,這個模型用map可能輕易描述,
很明顯學號用int描述,姓名用字串描述(本篇文章中不用char *來描述字串,而是採用STL中string來描述),
下面給出map描述代碼:
1. 聲明方式:
Map<int, string> mapStudent;
2. 資料的插入
在構造map容器後,我們就可以往裡面插入資料了。這裡講三種插入資料的方法:
第一種:用insert函數插入pair資料
Map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, “student_one”));第二種:用insert函數插入value_type資料
Map<int, string> mapStudent;
mapStudent.insert(map<int, string>::value_type (1, “student_one”));
第三種:用數組方式插入資料 Map<int, string> mapStudent;
mapStudent[1] = “student_one”;
mapStudent[2] = “student_two”;
3. map的大小
在往map裡面插入了資料,我們怎麼知道當前已經插入了多少資料呢,可以用size函數:
Int nSize = mapStudent.size();
4. 資料的遍曆
第一種:應用前向迭代器
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
Cout<<iter->first<<” ”<<iter->second<<end;
第二種:應用反相迭代器
map<int, string>::reverse_iterator iter;
for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
Cout<<iter->first<<” ”<<iter->second<<end;
第三種:用數組方式
int nSize = mapStudent.size()
for(int nIndex = 1; nIndex <= nSize; nIndex++)
Cout<<mapStudent[nIndex]<<end;
5. 資料的尋找(包括判定這個關鍵字是否在map中出現)
這裡給出三種資料尋找方法
第一種:用count函數來判定關鍵字是否出現,但是無法定位元據出現位置
第二種:用find函數來定位元據出現位置它返回的一個迭代器,
當資料出現時,它返回資料所在位置的迭代器,如果map中沒有要尋找的資料,它返回的迭代器等於end函數返回的迭代器
Int main()
{
Map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, “student_one”));
mapStudent.insert(pair<int, string>(2, “student_two”));
mapStudent.insert(pair<int, string>(3, “student_three”));
map<int, string>::iterator iter;
iter = mapStudent.find(1);
if(iter != mapStudent.end())
{
Cout<<”Find, the value is ”<<iter->second<<endl;
}
Else
{
Cout<<”Do not Find”<<endl;
}
}
第三種:這個方法用來判定資料是否出現
Lower_bound函數用法,這個函數用來返回要尋找關鍵字的下界(是一個迭代器)
Upper_bound函數用法,這個函數用來返回要尋找關鍵字的上界(是一個迭代器)
例如:map中已經插入了1,2,3,4的話,如果lower_bound(2)的話,返回的2,而upper-bound(2)的話,返回的就是3
Equal_range函數返回一個pair,pair裡面第一個變數是Lower_bound返回的迭代器,pair裡面第二個迭代器是Upper_bound返回的迭代器,如果這兩個迭代器相等的話,則說明map中不出現這個關鍵字,程式說明
mapPair = mapStudent.equal_range(2);
if(mapPair.first == mapPair.second)
cout<<”Do not Find”<<endl;
6. 資料的清空與判空
清空map中的資料可以用clear()函數,判定map中是否有資料可以用empty()函數,它返回true則說明是空map
7. 資料的刪除
這裡要用到erase函數,它有三個重載了的函數
迭代器刪除
iter = mapStudent.find(1);
mapStudent.erase(iter);
用關鍵字刪除
Int n = mapStudent.erase(1);//如果刪除了會返回1,否則返回0
用迭代器,成片的刪除
一下代碼把整個map清空
mapStudent.earse(mapStudent.begin(), mapStudent.end());
//成片刪除要注意的是,也是STL的特性,刪除區間是一個前閉後開的集合
8. 其他一些函數用法
這裡有swap,key_comp,value_comp,get_allocator等函數,有興趣的話可以自個研究三. set
set是集合,set中不會包含重複的元素,這是和vector的區別。定義:
定義一個元素為整數的集合a,可以用
set<int> a;基本操作:
對集合a中元素的有
插入元素:a.insert(1);
刪除元素(如果存在):a.erase(1);
判斷元素是否屬於集合:if (a.find(1) != a.end()) ...
返回集合元素的個數:a.size()
將集合清為空白集:a.clear()
集合的並,交和差
set_union(a.begin(),a.end(),b.begin(),b.end(),insert_iterator<set<int> >(c,c.begin()));
set_intersection(a.begin(),a.end(),b.begin(),b.end(),insert_iterator<set<int> >(c,c.begin()));
set_difference(a.begin(),a.end(),b.begin(),b.end(),insert_iterator<set<int> >(c,c.begin()));
(注意在此前要將c清為空白集)。
注意:
很重要的一點,為了實現集合的快速運算,set的實現採用了平衡二叉樹,因此,set中的元素必須是可排序的。如果是自訂的類型,那在定義類型的同時必須給出運算子<的定義四. sort
sort顧名思義就是排序
用法:
單關鍵字:
對於vector a來說
sort(&a[0], &a[N]); //N=a.size() 將a中元素遞增排序。
多關鍵字:
我們也可以利用類pair
vector< pair<int,int> > a; // 注意這裡兩個> >中間必須有一個空格,否則編譯器會當是運算子>>
例如:
int N,x,y;
cin >> N;
for(int i=0;i<N;++i) {
cin >> x >> y;
a.push_back(make_pair(x,y)); // make_pair用於建立pair對象
}
sort(&a[0], &a[N]);
注意:
對於我們自己定義的類或結構,系統一般不能替我做比較運算,需要我們自己定義相應的運算子<
bool operator<(const MyType &x, const MyType &y)
{
// Return true if x<y, false if x>=y
}
list就是一個列表
#include <list>
using namespace std;
typedef list<stUserListNode *> UserList; //儲存使用者資訊
添加
ClientList.push_back(currentuser);
刪除
ClientList.remove(*removeiterator);
尋找
stUserListNode GetUser(char *username) //根據使用者名稱擷取使用者資訊
{
for(UserList::iterator UserIterator=ClientList.begin();
UserIterator!=ClientList.end();
++UserIterator)
{
if( strcmp( ((*UserIterator)->userName), username) == 0 )
return *(*UserIterator);
}
throw Exception("not find this user");
}