下載PDF:STL容器的基本使用[資料]
幾種STL容器的基本用法[資料]
常興龍
天津大學計算機學院
QQ:286259397 MSN:cxl82116@msn.com
一、原型與構造函數
Vector的原型可定義為
vector<T, allocator <T> >
其構造函數為
vector() //空的
vector(al) //指定一種allocator
vector(n) //用默認T()初始化n個元素
vector(n, val) //用Val初始化n個元素
vector(n,val,al) //用val初始化n個元素,用al做分配器
vector(first,last) //從己有的first到last復制生成
vector(first,last,al) //從己有的first到last復制生成,用al做分配器
二、操作
1.開辟N個空間
vecobj.reserve(N);
2.當前(重新分配內存前)得到最大容量
capacity();
3.重新分配內存為N
resize(N)
如果變小,則刪除多余。如果變大,則用T()添充
4.清空
clear();
注意,clear()和resize()都不一定使得vector變小,若欲釋放內存,請使用vecobj.swap(vector<T, A>())
5.存取首尾元素
front()與back()操作,取后一個和最前一個元素,注意其返回是引用,其而是左值(l_value),因此可以賦值. 做類似于vecobj.front() = 3;的操作,但要保證front空間有效,否則形為無法預測。
6.取值
[]與at可以做此操作,at會檢查,如果越界有會out_of_range的異常被throw
7.push_back, pop_back
要保證不為空
8.使用assign
assign可以改變大小和初值,大小是隨意的,不受開始時大小的限制,如果設置為0,則清空。
assign(5,0)把vector改為5個大小,并用0添充
assign(iax+3,iax+5); 從數組第4到5個填充,注意左閉右開,即可取到iax[3]與iax[4]
9.使用insert
insert(it, x),在it前插入一個元素x
insert(it,first,last),在it前插入一個序列[first,last)左閉右開
10.使用erase
erase(it)刪除在it處的元素,返回值為下一元素。如果intVec.erase(intVec.end());并不會報錯,如果刪除一個序列[first,last),使用erase(first,last)
11.BVector是vector<bool>的特化版,具體的用途有待查證
12.flip()把某一元素,求反。如vecObj[i].flip();
13.swap. vecObj.swap(vecObj[i],vecObj[j]);
若要在容器中裝一個對象并且能并檢索,需要重載operator == ,如下:
#include <vector>
#include <iostream>
#include <stdlib.h>
#include <time.h>
//#include <getopt.h>
using namespace std;
class Obj
{
public:
Obj(int x, int y, int z)
{
this->x = x;
this->y = y;
this->z = z;
}
bool operator == (const Obj & obj)
{
if(obj.x == x && obj.y == y && obj.z == z)
return true;
return false;
}
int getX()
{
return this -> x;
}
private:
int x;
int y;
int z;
};
int main(int argc, char * argv[])
{
vector<Obj> vecObj;
Obj obj1(2,3,4);
Obj obj2(4,5,6);
vecObj.push_back(obj1);
vecObj.push_back(obj2);
vector<Obj>::iterator it =find(vecObj.begin(),vecObj.end(),Obj(2,3,4));
if(it != vecObj.end())
cout << (*it).getX() << endl;
return 0;
}
list的基本用法
與vector的用法基本相同,其中需要強調一點的是splice()函數,是指把指定段的另一個List插入到指定位置的前面。
splice(iterator it , list &x)
splice(iterator it, list &x, iterator first)
splice(iterator it,list &x, iterator first, iterator last)
一、原型與構造函數
typdef list<T, allocator<T> > listObj;
構造函數
list() //空
list(al) //指定allocator的空表
list(n)//n個元素,所有元素都是T()出來的
list(n,val)//n個元素,所有元素都是T(val)出來的
list(n,val,al)//同上,并指定allocator為al
list(first, last) //復制構造
list(first,last,al) //指定allocator構造
二、操作
1.resize & clear
使用resize(n)改變大小,使用resize(n, val)如果需要用T(val) 來填滿空閑值。
2.front ()& back()
如果listObj非常量對象,返回是一個左值函數
3.插入操作
insert(iterator it , val)
insert(iterator it, first, last)
insert(iteratot it, n, x)//插入n個x
4.移除
remove(x); //vector.erase(integrator it)
按值刪
int iax[] ={3,4,5,6,6,7,8};
list<int> lObj;
lObj.insert(lObj.begin(),iax, iax + 7);
lObj.remove(6); //
按函數條件刪
#include <iostream>
#include <list>
using namespace std;
// a predicate implemented as a function:
bool single_digit (const int& value) { return (value<10); }
// a predicate implemented as a class:
class is_odd
{
public:
bool operator() (const int& value) {return (value%2)==1; }
};
int main ()
{
int myints[]= {15,36,7,17,20,39,4,1};
list<int> mylist (myints,myints+8); // 15 36 7 17 20 39 4 1
mylist.remove_if (single_digit); // 15 36 17 20 39
mylist.remove_if (is_odd()); // 36 20
cout << "mylist contains:";
for (list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
cout << " " << *it;
cout << endl;
return 0;
}
當然,對于class is_odd,也可以寫成
template <class T>
class is_odd
{
};
調用時,則要改成
mylist.remove_if(is_odd<int>());
5.unique操作
// list::unique
#include <iostream>
#include <cmath>
#include <list>
using namespace std;
// a binary predicate implemented as a function:
bool same_integral_part (double first, double second)
{ return ( int(first)==int(second) ); }
// a binary predicate implemented as a class:
class is_near
{
public:
bool operator() (double first, double second)
{ return (fabs(first-second)<5.0); }
};
int main ()
{
double mydoubles[]={ 12.15, 2.72, 73.0, 12.77, 3.14,
12.77, 73.35, 72.25, 15.3, 72.25 };
list<double> mylist (mydoubles,mydoubles+10);
//UNIQUE以前必須要Sort,切記,它的內部實現是I,i+1的方式。
mylist.sort(); // 2.72, 3.14, 12.15, 12.77, 12.77,
// 15.3, 72.25, 72.25, 73.0, 73.35
mylist.unique(); // 2.72, 3.14, 12.15, 12.77
// 15.3, 72.25, 73.0, 73.35
mylist.unique (same_integral_part); // 2.72, 3.14, 12.15
// 15.3, 72.25, 73.0
mylist.unique (is_near()); // 2.72, 12.15, 72.25
cout << "mylist contains:";
for (list<double>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
cout << " " << *it;
cout << endl;
return 0;
}
6.排序操作
sort(); //默認按operator <排序,從小到大
sort(pr); //pr為Functional函數
7.Merge操作
在merge操作前,需要對兩個序列都用operator <排序,當然,也可以指定pr排序函數
merge(s2)
merge(s2,pr);
8.reverse()
翻轉操作,把整個list翻轉
deque的基本操作
一、原型與構造函數
typedef deque<T, allocator<T> > deqObj;
構造函數
deque();
deque(al);
deque(n);
deque(n,x);
deque(n,x,al);
deque(first,last);
deque(first,last,al);
二、操作
1.resize & clear
使用resize(n)改變大小,使用resize(n, val)如果需要用T(val) 來填滿空閑值。
2.clear操作
在clear后調用deqObj.swap(deque<T,A>())是好習慣,而且也一定要這么做。
3.font(),back(),operator [],(如出邊界,形為未定)at()(如出邊界,拋異常),push_back(),push_front(),pop_back(),pop_front(),insert(iterator it,x),insert(iterator it,n,x),insert(iterator first,iterator last),(插入后指向剛插入的值),erase(it),刪除在it指定位置的值,erase(iterator first,iterator last)刪除指定區間的值(左閉右開)。這些操作與上面的操作雷同。
Set與multiset的基本操作
一、原型與構造函數
typedef set<Key, less<Key>, allocator<key> > setObj;
構造函數
set(); //空set,按pred()排序
set(pr); //聲明一個空的按pr排序的set
set(pr,al); //聲明一個按pr排序的集合用al分配
set(first,last)
set(first,last,pr)
set(first,last,pr,al)
操作
1.clear()
2.erase(it); erase(first, last)
3.insert(key),返回值為pair<iterator, bool> 類型,沒有與插入元素相同的元素時,second為true,此時first指向新插入的元素。否則為False,first仍指向原來的元素
4.find(key)
5.lower_bound(key)
6.upper_bound(key)
7.equal_range(key),返回一個pair<iterator , iterator >(lower_bound(key), upper_bound(key))
8.count, equal_range的長度
9.key_comp,如果k1排在k2的前面,那么key_comp()(key1,key2)就為true
10.value_comp,對于set<key>對象,它與key_comp一樣。
multiset
1.insert,由于insert總能成功,那么它返回的就是新元素的迭代器,而并非pair<iteraor, bool>對象.
2.find返回第一個與key相等的迭代器。
3.equal_range將返回 [0,setObj.size())的任意長度.
4.count()將返回[0,setObj.size())的任意值。