1.容器概念
容器类别(简称容器,container),是用来管理一类对象的集合。每一类容器都有不同的优化点,STL细分提供了如图1五种类型的容器.。
图 1 STL 的容器种类
从整体上看,容器可以分为两类:
1、序列式容器 Sequence containers,该类容器是可序(orderd)群集,每个元素均有固定的位置->取决于插入的时机和地点,和元素的值没有关系。此类序列式容器STL中为Vector、Deque、list;
2、关联式容器 Associative containers,该类容器是已序(sorted)容器,元素的位置取决于特定的排序准则,此类关联上容器STL中包括Set,Multiset,Map,MultiMap。
序列式容器
序列式容器包括vector,deques,list;
Vectors
vector将其元素置于一个动态的数组中加以管理,允许随机存取,可以利用下标直接存取任何一个元素。
优势:在vector的尾部添加或删除元素效率高。
劣势:在vector的中间或头部插入元素效率低,原因是插入点或删除点之后的元素需要移动位置。
int _tmain(int argc, _TCHAR* argv[]) { const int nNum = 6; //vector 容器 cout << "*****vector example*****" << endl; vector<int> vecCollection; for (int i = 0; i < nNum; ++i) { vecCollection.push_back(i); } for (unsigned int i = 0; i < vecCollection.size(); i++) { cout <<vecCollection[i] << ' '; } cout << endl; }
Deques
deque 是“double-ended queue”的缩写,它也是一个动态的数组,可以向两端发展,在头部安插元素时,元素实际的排放顺序和安插顺序恰好相反。
优势:不论在数组的尾部还是头部安插元素都十分的迅速,效率高。
劣势:在中间安插元素则比较费时,因为需要移动其他的元素。
int _tmain(int argc, _TCHAR* argv[]) { const int nNum = 6; //deque 容器 cout << "*****deque example*****" << endl; deque<int> nCollection; for (int i = 0; i < nNum; ++i) { nCollection.push_front(i); } cout << "insert data int the front : 0 1 2 3 4 5 " << endl; cout << "print deque data:"; for (unsigned int i = 0; i < nCollection.size(); i++) { cout <<nCollection[i] << ' '; } cout << endl; nCollection.clear(); for (int i = 0; i < nNum; ++i) { nCollection.push_back(i); } cout << "insert data int the back : 0 1 2 3 4 5 " << endl; cout << "print deque data:"; for (unsigned int i = 0; i < nCollection.size(); i++) { cout <<nCollection[i] << ' '; } cout << endl; }
List
list由双向链表构成的,list内的每个元素都以一部分的内存指示其前驱元素和后继元素,list不具备随机存取能力;
优势:在任何位置上安插或删除一个元素效率都非常迅速。
劣势:由于不具备随机存取能力,访问某个位置的元素需要遍历其前面位置的元素。
int _tmain(int argc, _TCHAR* argv[]) { const int nNum = 6; //list 容器 cout << "*****list example*****" << endl; list<char> lsCollection; for (char c = 'a'; c <= 'z'; c++) { lsCollection.push_back(c); } //也可以使用迭代器进行循环打印 while (!lsCollection.empty()) { cout << lsCollection.front() << ' '; lsCollection.pop_front();//删除打印出来的字母 } cout << endl; if (lsCollection.empty()) { cout << "list is empty!" << endl; } }
关联式容器
关联式容器依据特定的排序准则,自动为容器内的元素排序,排序准则以函数形式呈现,用来比较元素值或者键值,默认情况下以operattor<进行比较,该排序准则也可以由用户自行定义。
通常情况下,关联式容器由二叉树构成。在二叉树中,每个元素(节点)都有一个父节点和两个子节点;左子树的所有元素都比自己小,右子树的所有元素都比自己大。
set
set的内部元素依据其值自动排序,每个元素都只能出现一次,不能重复;主要用于一个给定的关键词是否存在。
int _tmain(int argc, _TCHAR* argv[]) { cout << "*****set example*****" << endl; set<string> exclude; //set<string> exclude ={"the"},VS2008不支持列表初始化方式 exclude.insert("the"); exclude.insert("The"); exclude.insert("xu"); exclude.insert("Xu"); pair< set<string>::iterator, bool > pr; string strWordInput; cout << "pls input word:" << endl; cin.clear();//清空输入缓冲区 while (cin >> strWordInput) { pr = exclude.insert(strWordInput); if (true == pr.second) { cout << """ << strWordInput << """ << " is inserted in text successfully!" << endl; } else { cout << """ << strWordInput << """ << " is already in text !" << endl; } } return 0; }
Multiset
multiset 和set相同,只不过multiset允许有多个相同的元素。
Map
map的元素都是“键值/实值”所形成的一个对组;每个元素都以一个键值,键值是排序准则的基础,每一个键值只能出现一次,不允许重复;同时map具有任意型别的数组,即可以通过下标进行任意存取,索引类型不受限制。
map允许使用operator[]安插元素:
coll["pi"] = 3.14;
coll["null"] = 0;
coll["null"] = 0;
在这里,以键值为索引,键值可以为任意型别。这正是关联数组的接口,所谓的关联式数组就是:索引可以采用任何型别。
int _tmain(int argc, _TCHAR* argv[]) { //map example cout << "*****map example*****" << endl; //第三个参数是可变的:默认是重载<,也可是事其他运算符或自定义比较函数; typedef map<string, unsigned int, greater<string> > GreaterMap;//关键字以从大到小排序 GreaterMap WordCount; string strWord; cout << "pls input words:" << endl; while (cin >> strWord) { ++WordCount[strWord];//使用下标安插元素 } GreaterMap::iterator itMap = WordCount.begin(); for (; itMap != WordCount.end(); itMap++) { cout << itMap->first << " occuer: " << itMap->second << ((itMap->second > 1) ? " times;" : " time;") << endl; } }
Multimap
multimap和map相同,但允许重复元素,即multimap中可以包含多个相同键值相同的元素,但是multimap不能使用下标运算符“[]”,因为下标操作运算符只能处理单一实值。
int _tmain(int argc, _TCHAR* argv[]) { cout << "*****Multimap example*****" << endl; //第三个参数是可变的:默认是重载<,也可是事其他运算符或自定义比较函数; typedef multimap<string, unsigned int, greater<string> > GreaterMultiMap;//关键字以从大到小排序 GreaterMultiMap MulWordCount; string strMulWord; cout << "pls input words:" << endl; while (cin >> strMulWord) { //multimap不允许使用下标操作符,因为multimap允许单一索引对应到多个不同的元素, //只能先产生一个“键值/实值”对组,然后再插入到multimap中 MulWordCount.insert(make_pair(strMulWord, atoi(strMulWord.c_str()))); } GreaterMultiMap::iterator itMulMap = MulWordCount.begin(); for (; itMulMap != MulWordCount.end(); itMulMap++) { cout << """<< itMulMap->first<< """<< " atoi " << itMulMap->second << endl; } }