【20090824】C++培训日记-STL扫盲
今天是几个常用容器的基本使用。。于是放几个DEMO上来:
包括 vector, deque, list, map, multimap, set, multiset, stack, queue,以及较少用到的valarray, bitset, priority_queue
- vector
-
//////////////////////////////////////////////////////////////////////////
-
// CopyRight(c) 2009, YOYO, All Rights Reserved.
-
// Author: LIN YiQian
-
// Created: 2009/08/24
-
// Describe: STL vector 使用DEMO
-
//////////////////////////////////////////////////////////////////////////
-
#include <iostream>
-
#include <vector>
-
-
using namespace std;
-
-
typedef vector<int> INT_VEC;
-
-
// 打印Vector
-
void PrintVector(INT_VEC vecInt)
-
{
-
for (INT_VEC::iterator vecIter = vecInt.begin(); vecIter != vecInt.end(); ++vecIter)
-
{
-
cout << *vecIter ;
-
}
-
cout << endl;
-
}
-
-
// 逆向打印Vector
-
void PrintVectorReversse(INT_VEC vecInt)
-
{
-
for (INT_VEC::reverse_iterator vecRIter = vecInt.rbegin(); vecRIter != vecInt.rend(); ++vecRIter)
-
{
-
cout << *vecRIter;
-
}
-
cout << endl;
-
}
-
-
void main(void)
-
{
-
INT_VEC vecInt1;
-
cout << "vecInt1(): "; PrintVector(vecInt1);
-
-
// insert()
-
{
-
vecInt1.insert(vecInt1.begin(), 5);
-
vecInt1.insert(vecInt1.begin()+1, 4, 6);
-
cout << "vecInt1() after Insert: "; PrintVector(vecInt1);
-
}
-
-
INT_VEC vecInt2(10, 3);
-
cout << "vecInt2(10, 3): "; PrintVector(vecInt2);
-
-
// erase()
-
{
-
vecInt2.erase(vecInt2.begin()+5);
-
cout << "vecInt2 after Erase: "; PrintVector(vecInt2);
-
-
cout << "vecInt2 size: " << vecInt2.size() << endl;
-
-
vecInt2.erase(vecInt2.begin()+3, vecInt2.end()-3);
-
cout << "vecInt2 after Erase: "; PrintVector(vecInt2);
-
}
-
-
// size()
-
{
-
cout << "vecInt2 size: " << vecInt2.size() << endl;
-
}
-
-
// capacity()
-
{
-
cout << "vecInt2 Capacity: " << vecInt2.capacity() << endl;
-
}
-
-
// max_size()
-
{
-
cout << "vecInt2 Max size: " << vecInt2.max_size() << endl;
-
}
-
-
INT_VEC vecInt3(vecInt2);
-
cout << "vecInt3(vecInt2): "; PrintVector(vecInt3);
-
-
// insert()
-
{
-
vecInt3.insert(vecInt3.begin(), vecInt1.begin(), vecInt1.end()-1);
-
cout << "vecInt1 after Insert: "; PrintVector(vecInt1);
-
cout << "vecInt3 after Insert: "; PrintVector(vecInt3);
-
}
-
-
// reverse()
-
{
-
cout << "vecInt3 Reverse: "; PrintVectorReversse(vecInt3);
-
}
-
-
INT_VEC vecInt4(vecInt3.begin()+2, vecInt3.end()-5);
-
cout << "vecInt4(vecInt3.begin()+2, vecInt3.end()-5): "; PrintVector(vecInt4);
-
-
// assign()
-
{
-
vecInt4.assign(4, 7);
-
cout << "vecInt4 after Assign: "; PrintVector(vecInt4);
-
}
-
-
// pop_back() & push_back()
-
{
-
vecInt4.pop_back();
-
cout << "vecInt4 after Pop_Back: "; PrintVector(vecInt4);
-
-
vecInt4.push_back(8);
-
cout << "vecInt4 after Push_Back: "; PrintVector(vecInt4);
-
}
-
-
// at() & operator[] - Get Value
-
{
-
cout << "vecInt4 first elem: " << vecInt4.at(0) << endl;
-
cout << "vecInt3 first elem: " << vecInt3[0] << endl;
-
}
-
-
// operator cmp
-
{
-
cout << "vecInt4 == vecInt3?: " << boolalpha << (vecInt4 == vecInt3) << endl;
-
cout << "vecInt4 < vecInt3?: " << boolalpha << (vecInt4 < vecInt3) << endl;
-
}
-
-
// swap()
-
{
-
vecInt4.swap(vecInt3);
-
cout << "vecInt4 after Swap: "; PrintVector(vecInt4);
-
cout << "vecInt3 after Swap: "; PrintVector(vecInt3);
-
}
-
-
// front() & back()
-
{
-
cout << "vecInt4 front: " << vecInt4.front() << endl;
-
cout << "vecInt4 back: " << vecInt4.back() << endl;
-
}
-
-
// empty()
-
{
-
cout << "vecInt4 Empty?: " << boolalpha << vecInt4.empty() << endl;
-
}
-
-
// clear()
-
{
-
vecInt4.clear();
-
cout << "vecInt4 after Clear: "; PrintVector(vecInt4);
-
cout << "vecInt4 size: " << vecInt4.size() << endl;
-
cout << "vecInt4 Empty?: " << boolalpha << vecInt4.empty() << endl;
-
}
-
-
system("pause");
-
}
-
- deque
-
//////////////////////////////////////////////////////////////////////////
-
// CopyRight(c) 2009, YOYO, All Rights Reserved.
-
// Author: LIN YiQian
-
// Created: 2009/08/24
-
// Describe: STL deque 使用DEMO
-
//////////////////////////////////////////////////////////////////////////
-
#include <iostream>
-
#include <deque>
-
-
using namespace std;
-
-
typedef deque<int> INT_DEQ;
-
-
// 打印Deque
-
void PrintDeque(INT_DEQ deqInt)
-
{
-
for (INT_DEQ::iterator deqIter = deqInt.begin(); deqIter != deqInt.end(); ++deqIter)
-
{
-
cout << *deqIter;
-
}
-
cout << endl;
-
}
-
-
// 逆向打印Deque
-
void PrintDequeReverse(INT_DEQ deqInt)
-
{
-
for (INT_DEQ::reverse_iterator deqRIter = deqInt.rbegin(); deqRIter != deqInt.rend(); ++deqRIter)
-
{
-
cout << *deqRIter;
-
}
-
cout << endl;
-
}
-
-
void main(void)
-
{
-
INT_DEQ deqInt1;
-
cout << "deqInt1: "; PrintDeque(deqInt1);
-
-
// push_front() & push_back()
-
{
-
deqInt1.push_front(3);
-
deqInt1.push_front(4);
-
deqInt1.push_back(6);
-
deqInt1.push_back(9);
-
cout << "deqInt1 after Push: "; PrintDeque(deqInt1);
-
}
-
-
// reverse()
-
{
-
cout << "deqInt1 Reverse: "; PrintDequeReverse(deqInt1);
-
}
-
-
// size()
-
{
-
cout << "deqInt1 size: " << deqInt1.size() << endl;
-
}
-
-
// max_size()
-
{
-
cout << "deqInt1 max_size: " << deqInt1.max_size() << endl;
-
}
-
-
INT_DEQ deqInt2(5, 5);
-
cout << "deqInt2(5, 5): "; PrintDeque(deqInt2);
-
-
// erase()
-
{
-
deqInt2.erase(deqInt2.begin()+2);
-
cout << "deqInt2 after Erase: "; PrintDeque(deqInt2);
-
-
deqInt2.erase(deqInt2.begin()+1, deqInt2.end()-1);
-
cout << "deqInt2 after Erase: "; PrintDeque(deqInt2);
-
}
-
-
// empty()
-
{
-
cout << "deqInt2 empty?: " << boolalpha << deqInt2.empty();
-
}
-
-
INT_DEQ deqInt3(deqInt1);
-
cout << "deqInt3(deqInt1): "; PrintDeque(deqInt3);
-
-
// pop_front() & pop_back()
-
{
-
deqInt3.pop_front();
-
deqInt3.pop_back();
-
cout << "deqInt3 after Pop: "; PrintDeque(deqInt3);
-
}
-
-
// assign()
-
{
-
deqInt3.assign(6, 6);
-
cout << "deqInt3 after Assign: "; PrintDeque(deqInt3);
-
}
-
-
INT_DEQ deqInt4(deqInt3.begin()+2, deqInt3.end());
-
cout << "deqInt4(deqInt3.begin()+2, deqInt3.end()): "; PrintDeque(deqInt4);
-
-
// at() and operator[]
-
{
-
cout << "deqInt4 at(3): " << deqInt4.at(3) << endl;
-
cout << "deqInt4[2]: " << deqInt4[2] << endl;
-
}
-
-
// front() and back()
-
{
-
cout << "deqInt4 front: " << deqInt4.front() << endl;
-
cout << "deqInt4 back: " << deqInt4.back() << endl;
-
}
-
-
// operator cmp
-
{
-
cout << "deqInt4 == deqInt2?: " << boolalpha << (deqInt4 == deqInt2) << endl;
-
cout << "deqInt4 >= deqInt2?: " << boolalpha << (deqInt4 >= deqInt2) << endl;
-
}
-
-
// swap()
-
{
-
deqInt4.swap(deqInt1);
-
cout << "deqInt1 after Swap: "; PrintDeque(deqInt1);
-
cout << "deqInt4 after Swap: "; PrintDeque(deqInt4);
-
}
-
-
// clear()
-
{
-
deqInt4.clear();
-
cout << "deqInt4 after Clear: "; PrintDeque(deqInt4);
-
cout << "deqInt4 size: " << deqInt4.size() << endl;
-
cout << "deqInt4 empty?: " << boolalpha << deqInt4.empty() << endl;
-
}
-
-
system("pause");
-
}
-
- list
-
//////////////////////////////////////////////////////////////////////////
-
// CopyRight(c) 2009, YOYO, All Rights Reserved.
-
// Author: LIN YiQian
-
// Created: 2009/08/24
-
// Describe: STL list 使用DEMO
-
//////////////////////////////////////////////////////////////////////////
-
#include <iostream>
-
#include <list>
-
-
using namespace std;
-
-
typedef list<int> INT_LST;
-
-
// 打印List
-
void PrintList(INT_LST lstInt)
-
{
-
for (INT_LST::iterator lstIter = lstInt.begin(); lstIter != lstInt.end(); ++lstIter)
-
{
-
cout << *lstIter;
-
}
-
cout << endl;
-
}
-
-
// 逆向打印List
-
void PrintListReverse(INT_LST lstInt)
-
{
-
for (INT_LST::reverse_iterator lstRIter = lstInt.rbegin(); lstRIter != lstInt.rend(); ++lstRIter)
-
{
-
cout << *lstRIter;
-
}
-
cout << endl;
-
}
-
-
void main(void)
-
{
-
INT_LST lstInt1;
-
cout << "lstInt1: "; PrintList(lstInt1);
-
-
// push_front() & push_back()
-
{
-
lstInt1.push_front(3);
-
lstInt1.push_front(2);
-
lstInt1.push_back(8);
-
lstInt1.push_back(9);
-
lstInt1.push_back(7);
-
cout << "lstInt1 after Push: "; PrintList(lstInt1);
-
}
-
-
// insert()
-
{
-
lstInt1.insert(++ ++lstInt1.begin(), 5);
-
lstInt1.insert(++ lstInt1.begin(), 6);
-
cout << "lstInt1 after Insert: "; PrintList(lstInt1);
-
}
-
-
// sort()
-
{
-
lstInt1.sort();
-
cout << "lstInt1 after Sort: "; PrintList(lstInt1);
-
}
-
-
// size() & max_size()
-
{
-
cout << "lstInt1 size: " << lstInt1.size() << endl;
-
cout << "lstInt1 max_size: " << lstInt1.max_size() << endl;
-
}
-
-
// empty()
-
{
-
cout << "lstInt1 Empty?: " << boolalpha << lstInt1.empty() << endl;
-
}
-
-
// reverse()
-
{
-
cout << "lstInt1 reverse: "; PrintListReverse(lstInt1);
-
}
-
-
// front() & back()
-
{
-
cout << "lstInt1 front: " << lstInt1.front() << endl;
-
cout << "lstInt1 back: " << lstInt1.back() << endl;
-
}
-
-
INT_LST lstInt2(7, 9);
-
cout << "lstInt2: "; PrintList(lstInt2);
-
-
INT_LST lstInt3(lstInt1);
-
cout << "lstInt3: "; PrintList(lstInt3);
-
-
// pop_front() & pop_back()
-
{
-
lstInt3.pop_front();
-
lstInt3.pop_back();
-
cout << "lstInt3 after Pop: "; PrintList(lstInt3);
-
}
-
-
// erase()
-
{
-
lstInt3.erase(++lstInt3.begin());
-
cout << "lstInt3 after Erase: "; PrintList(lstInt3);
-
-
lstInt3.erase(++lstInt3.begin(), --lstInt3.end());
-
cout << "lstInt3 after Erase: "; PrintList(lstInt3);
-
}
-
-
INT_LST lstInt4(lstInt2.begin(), lstInt2.end());
-
cout << "lstInt4: "; PrintList(lstInt4);
-
-
// assign()
-
{
-
lstInt4.assign(9, 2);
-
cout << "lstInt4 after Assign: "; PrintList(lstInt4);
-
}
-
-
// operator cmp
-
{
-
cout << "lstInt4 == lstInt2?: " << boolalpha << (lstInt2 == lstInt4) << endl;
-
cout << "lstInt4 <= lstInt2?: " << boolalpha << (lstInt4 <= lstInt2) << endl;
-
}
-
-
// splice()
-
{
-
lstInt4.splice(++lstInt4.begin(), lstInt2, ++ ++lstInt2.begin(), --lstInt2.end());
-
cout << "lstInt4 after Splice: "; PrintList(lstInt4);
-
cout << "lstInt2 after Splice: "; PrintList(lstInt2);
-
-
lstInt4.splice(lstInt4.begin(), lstInt1, ++ lstInt1.begin());
-
cout << "lstInt4 after Splice: "; PrintList(lstInt4);
-
cout << "lstInt1 after Splice: "; PrintList(lstInt1);
-
-
lstInt4.splice(lstInt4.begin(), lstInt3);
-
cout << "lstInt4 after Splice: "; PrintList(lstInt4);
-
cout << "lstInt3 after Splice: "; PrintList(lstInt3);
-
}
-
-
// swap()
-
{
-
lstInt4.swap(lstInt1);
-
cout << "lstInt4 after Swap: "; PrintList(lstInt4);
-
cout << "lstInt1 after Swap: "; PrintList(lstInt1);
-
}
-
-
// clear()
-
{
-
lstInt4.clear();
-
cout << "lstInt4 after Clear: "; PrintList(lstInt4);
-
cout << "lstInt4 size: " << lstInt4.size() << endl;
-
cout << "lstInt4 Empty?: " << boolalpha << lstInt4.empty() << endl;
-
}
-
-
system("pause");
-
}
-
- map
-
//////////////////////////////////////////////////////////////////////////
-
// CopyRight(c) 2009, YOYO, All Rights Reserved.
-
// Author: LIN YiQian
-
// Created: 2009/08/24
-
// Describe: STL map 使用DEMO
-
//////////////////////////////////////////////////////////////////////////
-
#include <iostream>
-
#include <map>
-
#include <string>
-
-
using namespace std;
-
-
typedef map<int, string> STR_MAP;
-
-
// 打印Map
-
void PrintMap(STR_MAP strMap)
-
{
-
for (STR_MAP::iterator strMapIter = strMap.begin(); strMapIter != strMap.end(); ++strMapIter)
-
{
-
cout << (*strMapIter).first << "." << (*strMapIter).second << " " << endl;
-
}
-
cout << endl;
-
}
-
-
// 反向打印Map
-
void PrintMapReverse(STR_MAP strMap)
-
{
-
for (STR_MAP::reverse_iterator strMapRIter = strMap.rbegin(); strMapRIter != strMap.rend(); ++strMapRIter)
-
{
-
cout << (*strMapRIter).first << "." << (*strMapRIter).second << " " << endl;
-
}
-
cout << endl;
-
}
-
-
// 打印指定范围的Map
-
void PrintMapRange(STR_MAP strMap, STR_MAP::key_type low, STR_MAP::key_type up)
-
{
-
for (STR_MAP::iterator strMapIter = strMap.lower_bound(low); strMapIter != strMap.upper_bound(up); ++strMapIter)
-
{
-
cout << (*strMapIter).first << "." << (*strMapIter).second << " " << endl;
-
}
-
cout << endl;
-
}
-
-
void main(void)
-
{
-
STR_MAP strMap;
-
cout << "New StringMap: " << endl; PrintMap(strMap);
-
-
// insert()
-
{
-
strMap.insert(STR_MAP::value_type(0, "Zero"));
-
strMap.insert(STR_MAP::value_type(1, "One"));
-
strMap.insert(STR_MAP::value_type(2, "Two"));
-
strMap.insert(STR_MAP::value_type(6, "Six"));
-
strMap.insert(STR_MAP::value_type(4, "Four"));
-
strMap.insert(STR_MAP::value_type(8, "Eight"));
-
strMap.insert(STR_MAP::value_type(9, "Nine"));
-
cout << "After Insert: " << endl;
-
PrintMap(strMap);
-
}
-
-
// test insert() again
-
{
-
strMap.insert(STR_MAP::value_type(1, "One")); // fail
-
strMap.insert(STR_MAP::value_type(3, "Three")); // success
-
strMap.insert(STR_MAP::value_type(4, "Four")); // fail
-
strMap.insert(STR_MAP::value_type(8, "Eight")); // fail
-
strMap.insert(STR_MAP::value_type(7, "Seven")); // success
-
strMap.insert(STR_MAP::value_type(5, "Five")); // success
-
cout << "After Insert again: " << endl;
-
PrintMap(strMap);
-
}
-
-
// Print Range Map
-
{
-
cout << "Print Map in Range: low-2, up-7" << endl;
-
PrintMapRange(strMap, 2, 7);
-
}
-
-
// Print Reverse Map
-
{
-
cout << "Print Map Reverse: " << endl;
-
PrintMapReverse(strMap);
-
}
-
-
// find()
-
{
-
cout << "Find Value 3?: " << boolalpha << (strMap.find(3) != strMap.end()) << endl;
-
cout << "Find Value 12?: " << boolalpha << (strMap.find(12) != strMap.end()) << endl;
-
cout << endl;
-
}
-
-
// erase()
-
{
-
strMap.erase(9);
-
strMap.erase(19);
-
cout << "After Erase 9, 19: " << endl; PrintMap(strMap);
-
-
strMap.erase(strMap.find(2));
-
cout << "After Erase 2: " << endl; PrintMap(strMap);
-
}
-
-
// count()
-
{
-
cout << "Value 4 counts: " << strMap.count(4) << endl;
-
cout << "Value 9 counts: " << strMap.count(9) << endl;
-
cout << endl;
-
}
-
-
// size() & max_size()
-
{
-
cout << "Map size: " << strMap.size() << endl;
-
cout << "Map max_size: " << strMap.max_size() << endl;
-
cout << endl;
-
}
-
-
// empty()
-
{
-
cout << "Map Empty?: " << boolalpha << strMap.empty() << endl;
-
cout << endl;
-
}
-
-
// A Demo for Using Map to Search
-
{
-
string inKey = "";
-
while (true)
-
{
-
cout << "Input 'q' to exit, or Input a Number: ";
-
cin >> inKey;
-
-
if (inKey == "q")
-
{
-
break;
-
}
-
-
string::size_type strLen = inKey.length();
-
string::size_type index = 0;
-
-
for (; index < strLen; ++index)
-
{
-
string::reference ch = inKey[index];
-
STR_MAP::key_type key = ch - STR_MAP::key_type('0');
-
-
STR_MAP::iterator strMapIter = strMap.find(key);
-
-
if (strMapIter == strMap.end())
-
{
-
cout << "[not found] " ;
-
}
-
else
-
{
-
cout << "[" << (*strMapIter).second << "] ";
-
}
-
}
-
-
cout << endl;
-
}
-
-
cout << endl;
-
}
-
-
// clear()
-
{
-
strMap.clear();
-
cout << "After Clear: " << endl;
-
PrintMap(strMap);
-
-
cout << "Map size: " << strMap.size() << endl;
-
cout << "Map Empty?: " << boolalpha << strMap.empty() << endl;
-
}
-
-
system("pause");
-
}
-
- multimap
-
//////////////////////////////////////////////////////////////////////////
-
// CopyRight(c) 2009, YOYO, All Rights Reserved.
-
// Author: LIN YiQian
-
// Created: 2009/08/24
-
// Describe: STL multimap 使用DEMO
-
//////////////////////////////////////////////////////////////////////////
-
#include <iostream>
-
#include <map>
-
#include <string>
-
-
using namespace std;
-
-
typedef multimap<int, string> STR_MMAP;
-
-
// 打印MultiMap
-
void PrintMap(STR_MMAP strMap)
-
{
-
for (STR_MMAP::iterator strMapIter = strMap.begin(); strMapIter != strMap.end(); ++strMapIter)
-
{
-
cout << (*strMapIter).first << "." << (*strMapIter).second << " " << endl;
-
}
-
cout << endl;
-
}
-
-
// 反向打印MultiMap
-
void PrintMapReverse(STR_MMAP strMap)
-
{
-
for (STR_MMAP::reverse_iterator strMapRIter = strMap.rbegin(); strMapRIter != strMap.rend(); ++strMapRIter)
-
{
-
cout << (*strMapRIter).first << "." << (*strMapRIter).second << " " << endl;
-
}
-
cout << endl;
-
}
-
-
// 打印指定范围的MultiMap
-
void PrintMapRange(STR_MMAP strMap, STR_MMAP::key_type low, STR_MMAP::key_type up)
-
{
-
for (STR_MMAP::iterator strMapIter = strMap.lower_bound(low); strMapIter != strMap.upper_bound(up); ++strMapIter)
-
{
-
cout << (*strMapIter).first << "." << (*strMapIter).second << " " << endl;
-
}
-
cout << endl;
-
}
-
-
void main(void)
-
{
-
STR_MMAP strMap;
-
cout << "New StringMultiMap: " << endl; PrintMap(strMap);
-
-
// insert()
-
{
-
strMap.insert(STR_MMAP::value_type(80, "张三"));
-
strMap.insert(STR_MMAP::value_type(70, "李四"));
-
strMap.insert(STR_MMAP::value_type(90, "王五"));
-
strMap.insert(STR_MMAP::value_type(75, "赵六"));
-
cout << "After Insert: " << endl;
-
PrintMap(strMap);
-
}
-
-
// test insert() again
-
{
-
strMap.insert(STR_MMAP::value_type(80, "张A"));
-
strMap.insert(STR_MMAP::value_type(85, "DFA"));
-
strMap.insert(STR_MMAP::value_type(90, "王C"));
-
strMap.insert(STR_MMAP::value_type(95, "ASDF"));
-
cout << "After Insert again: " << endl;
-
PrintMap(strMap);
-
}
-
-
// Print Range Map
-
{
-
cout << "Print Map in Range: low-80, up-90" << endl;
-
PrintMapRange(strMap, 80, 90);
-
}
-
-
// Print Reverse Map
-
{
-
cout << "Print Map Reverse: " << endl;
-
PrintMapReverse(strMap);
-
}
-
-
// find()
-
{
-
cout << "Find Value 80?: " << boolalpha << (strMap.find(80) != strMap.end()) << endl;
-
cout << "Find Value 82?: " << boolalpha << (strMap.find(82) != strMap.end()) << endl;
-
cout << endl;
-
}
-
-
// erase()
-
{
-
strMap.erase(85);
-
strMap.erase(91);
-
cout << "After Erase 85, 91: " << endl; PrintMap(strMap);
-
-
strMap.erase(strMap.find(70));
-
cout << "After Erase 70: " << endl; PrintMap(strMap);
-
}
-
-
// count()
-
{
-
cout << "Value 80 counts: " << strMap.count(80) << endl;
-
cout << "Value 75 counts: " << strMap.count(75) << endl;
-
cout << "Value 60 counts: " << strMap.count(60) << endl;
-
cout << endl;
-
}
-
-
// size() & max_size()
-
{
-
cout << "Map size: " << strMap.size() << endl;
-
cout << "Map max_size: " << strMap.max_size() << endl;
-
cout << endl;
-
}
-
-
// empty()
-
{
-
cout << "Map Empty?: " << boolalpha << strMap.empty() << endl;
-
cout << endl;
-
}
-
-
// clear()
-
{
-
strMap.clear();
-
cout << "After Clear: " << endl;
-
PrintMap(strMap);
-
-
cout << "Map size: " << strMap.size() << endl;
-
cout << "Map Empty?: " << boolalpha << strMap.empty() << endl;
-
}
-
-
system("pause");
-
}
-
- set
-
//////////////////////////////////////////////////////////////////////////
-
// CopyRight(c) 2009, YOYO, All Rights Reserved.
-
// Author: LIN YiQian
-
// Created: 2009/08/24
-
// Describe: STL set 使用DEMO
-
//////////////////////////////////////////////////////////////////////////
-
#include <iostream>
-
#include <set>
-
-
using namespace std;
-
-
typedef set<int> INT_SET;
-
-
// 打印Set
-
void PrintSet(INT_SET setInt)
-
{
-
for (INT_SET::iterator setIntIter = setInt.begin(); setIntIter != setInt.end(); ++setIntIter)
-
{
-
cout << (*setIntIter) << " ";
-
}
-
cout << endl;
-
}
-
-
// 反向打印Set
-
void PrintSetReverse(INT_SET setInt)
-
{
-
for (INT_SET::reverse_iterator setIntIter = setInt.rbegin(); setIntIter != setInt.rend(); ++setIntIter)
-
{
-
cout << (*setIntIter) << " ";
-
}
-
cout << endl;
-
}
-
-
// 打印指定范围的Set
-
void PrintSetRange(INT_SET setInt, INT_SET::value_type low, INT_SET::value_type up)
-
{
-
for (INT_SET::iterator setIntIter = setInt.lower_bound(low); setIntIter != setInt.upper_bound(up); ++setIntIter)
-
{
-
cout << (*setIntIter) << " ";
-
}
-
cout << endl;
-
}
-
-
void main(void)
-
{
-
INT_SET setInt;
-
cout << "New Int Set: "; PrintSet(setInt);
-
-
// insert()
-
{
-
setInt.insert(3);
-
setInt.insert(2);
-
setInt.insert(1);
-
setInt.insert(4);
-
cout << "After Insert: "; PrintSet(setInt);
-
}
-
-
// test insert() again
-
{
-
setInt.insert(3); // fail
-
setInt.insert(5); // success
-
setInt.insert(7); // success
-
setInt.insert(4); // fail
-
setInt.insert(8); // success
-
setInt.insert(9); // success
-
cout << "After Insert again: "; PrintSet(setInt);
-
}
-
-
// reverse()
-
{
-
cout << "Set Reverse: "; PrintSetReverse(setInt);
-
}
-
-
// range()
-
{
-
cout << "Set Range(low-3, up-8): "; PrintSetRange(setInt, 3, 8);
-
}
-
-
// size() & max_size()
-
{
-
cout << "Set size: " << setInt.size() << endl;
-
cout << "Set max_size: " << setInt.max_size() << endl;
-
}
-
-
// empty()
-
{
-
cout << "Set Empty?: " << boolalpha << setInt.empty() << endl;
-
}
-
-
// find()
-
{
-
cout << "Value 7 find?: " << boolalpha << (setInt.find(7) != setInt.end()) << endl;
-
cout << "Value 6 find?: " << boolalpha << (setInt.find(6) != setInt.end()) << endl;
-
}
-
-
// count()
-
{
-
cout << "Value 3 count: " << setInt.count(3) << endl;
-
cout << "Value -5 count: " << setInt.count(-5) << endl;
-
}
-
-
// clear()
-
{
-
setInt.clear();
-
cout << "After Clear: "; PrintSet(setInt);
-
cout << "Set size: " << setInt.size() << endl;
-
cout << "Set Empty?: " << boolalpha << setInt.empty() << endl;
-
}
-
-
system("pause");
-
}
-
- multiset
-
//////////////////////////////////////////////////////////////////////////
-
// CopyRight(c) 2009, YOYO, All Rights Reserved.
-
// Author: LIN YiQian
-
// Created: 2009/08/24
-
// Describe: STL multiset 使用DEMO
-
//////////////////////////////////////////////////////////////////////////
-
#include <iostream>
-
#include <set>
-
-
using namespace std;
-
-
typedef multiset<int> INT_MSET;
-
-
// 打印Set
-
void PrintSet(INT_MSET setInt)
-
{
-
for (INT_MSET::iterator setIntIter = setInt.begin(); setIntIter != setInt.end(); ++setIntIter)
-
{
-
cout << (*setIntIter) << " ";
-
}
-
cout << endl;
-
}
-
-
// 反向打印Set
-
void PrintSetReverse(INT_MSET setInt)
-
{
-
for (INT_MSET::reverse_iterator setIntIter = setInt.rbegin(); setIntIter != setInt.rend(); ++setIntIter)
-
{
-
cout << (*setIntIter) << " ";
-
}
-
cout << endl;
-
}
-
-
// 打印指定范围的Set
-
void PrintSetRange(INT_MSET setInt, INT_MSET::value_type low, INT_MSET::value_type up)
-
{
-
for (INT_MSET::iterator setIntIter = setInt.lower_bound(low); setIntIter != setInt.upper_bound(up); ++setIntIter)
-
{
-
cout << (*setIntIter) << " ";
-
}
-
cout << endl;
-
}
-
-
void main(void)
-
{
-
INT_MSET setInt;
-
cout << "New Int Set: "; PrintSet(setInt);
-
-
// insert()
-
{
-
setInt.insert(3);
-
setInt.insert(2);
-
setInt.insert(1);
-
setInt.insert(4);
-
cout << "After Insert: "; PrintSet(setInt);
-
}
-
-
// test insert() again
-
{
-
setInt.insert(3);
-
setInt.insert(5);
-
setInt.insert(7);
-
setInt.insert(4);
-
setInt.insert(8);
-
setInt.insert(9);
-
cout << "After Insert again: "; PrintSet(setInt);
-
}
-
-
// reverse()
-
{
-
cout << "Set Reverse: "; PrintSetReverse(setInt);
-
}
-
-
// range()
-
{
-
cout << "Set Range(low-3, up-8): "; PrintSetRange(setInt, 3, 8);
-
}
-
-
// size() & max_size()
-
{
-
cout << "Set size: " << setInt.size() << endl;
-
cout << "Set max_size: " << setInt.max_size() << endl;
-
}
-
-
// empty()
-
{
-
cout << "Set Empty?: " << boolalpha << setInt.empty() << endl;
-
}
-
-
// find()
-
{
-
cout << "Value 7 find?: " << boolalpha << (setInt.find(7) != setInt.end()) << endl;
-
cout << "Value 6 find?: " << boolalpha << (setInt.find(6) != setInt.end()) << endl;
-
}
-
-
// count()
-
{
-
cout << "Value 3 count: " << setInt.count(3) << endl;
-
cout << "Value 1 count: " << setInt.count(1) << endl;
-
cout << "Value -5 count: " << setInt.count(-5) << endl;
-
}
-
-
// clear()
-
{
-
setInt.clear();
-
cout << "After Clear: "; PrintSet(setInt);
-
cout << "Set size: " << setInt.size() << endl;
-
cout << "Set Empty?: " << boolalpha << setInt.empty() << endl;
-
}
-
-
system("pause");
-
}
-
- stack
-
//////////////////////////////////////////////////////////////////////////
-
// CopyRight(c) 2009, YOYO, All Rights Reserved.
-
// Author: LIN YiQian
-
// Created: 2009/08/24
-
// Describe: STL stack 使用DEMO
-
//////////////////////////////////////////////////////////////////////////
-
#include <iostream>
-
#include <stack>
-
-
using namespace std;
-
-
typedef stack<int> INT_STK;
-
-
void main(void)
-
{
-
INT_STK stkInt;
-
-
// Print is stack empty?
-
cout << "Stack Empty?: " << boolalpha << stkInt.empty() << endl;
-
-
// Push elements
-
for (int i = 0; i < 10; i += 2)
-
{
-
stkInt.push(i);
-
}
-
-
// Get size
-
cout << "Stack size: " << stkInt.size() << endl;
-
-
// Get top element & change its value
-
if (!stkInt.empty())
-
{
-
cout << "Top Element: " << stkInt.top() << " change to 99 " << endl;
-
stkInt.top() = 99;
-
}
-
-
// Print stack
-
{
-
while (!stkInt.empty())
-
{
-
cout << stkInt.top() << " ";
-
stkInt.pop();
-
}
-
}
-
cout << endl;
-
-
system("pause");
-
}
-
- queue
-
//////////////////////////////////////////////////////////////////////////
-
// CopyRight(c) 2009, YOYO, All Rights Reserved.
-
// Author: LIN YiQian
-
// Created: 2009/08/24
-
// Describe: STL queue 使用DEMO
-
//////////////////////////////////////////////////////////////////////////
-
#include <iostream>
-
#include <queue>
-
-
using namespace std;
-
-
typedef queue<int> INT_QUE;
-
-
void main(void)
-
{
-
INT_QUE intQue;
-
int x;
-
-
// Push
-
cout << "Please input some Non-negative integer(put negative integer to finish input): " << endl;
-
while (cin>>x)
-
{
-
if (x<0) break;
-
intQue.push(x);
-
}
-
-
// Size
-
cout << "Queue Size: " << intQue.size() << endl;
-
-
// Empty?
-
cout << "Queue Empty?: " << boolalpha << intQue.empty() << endl;
-
-
// Change Front Element & Back Element
-
{
-
cout << "Queue front Element: " << intQue.front() << " Change to 99" << endl;
-
intQue.front() = 99;
-
-
cout << "Queue back Element: " << intQue.back() << " Change to 33" << endl;
-
intQue.back() = 33;
-
}
-
-
// Print Queue
-
{
-
cout << "Queue: ";
-
while (!intQue.empty())
-
{
-
cout << intQue.front() << " ";
-
intQue.pop();
-
}
-
cout << endl;
-
}
-
-
system("pause");
-
}
-
- valarray
-
//////////////////////////////////////////////////////////////////////////
-
// CopyRight(c) 2009, YOYO, All Rights Reserved.
-
// Author: LIN YiQian
-
// Created: 2009/08/24
-
// Describe: STL valarray 使用DEMO
-
//////////////////////////////////////////////////////////////////////////
-
#include <iostream>
-
#include <valarray>
-
-
using namespace std;
-
-
typedef valarray<double> DOUBLE_VAR;
-
-
const int VALARRAY_SIZE = 3;
-
-
void PrintArray(DOUBLE_VAR varDouble)
-
{
-
for (int i = 0; i < varDouble.size(); ++i)
-
{
-
cout << varDouble[i] << " ";
-
}
-
cout << endl;
-
}
-
-
void main(void)
-
{
-
DOUBLE_VAR varDouble(VALARRAY_SIZE);
-
-
// assign
-
for (int i = 1; i < 4; ++i)
-
{
-
varDouble[i-1] = i*i;
-
}
-
-
// size
-
{
-
cout << "ValArray size: " << varDouble.size() << endl;
-
}
-
-
// print array
-
{
-
cout << "ValArray: "; PrintArray(varDouble);
-
}
-
-
DOUBLE_VAR varIntSqrt(VALARRAY_SIZE);
-
varIntSqrt = sqrt(varDouble);
-
-
// print sqrt array
-
{
-
cout << "Sqrt ValArray: "; PrintArray(varIntSqrt);
-
}
-
-
DOUBLE_VAR varIntPow(VALARRAY_SIZE);
-
varIntPow = pow(varDouble, 2.0);
-
-
// print pow array
-
{
-
cout << "Pow ValArray: "; PrintArray(varIntPow);
-
}
-
-
// sum, max & min
-
{
-
cout << "Sum: " << varIntPow.sum() << endl;
-
cout << "Max: " << varIntPow.max() << endl;
-
cout << "Min: " << varIntPow.min() << endl;
-
}
-
-
system("pause");
-
}
-
- bitset
-
//////////////////////////////////////////////////////////////////////////
-
// CopyRight(c) 2009, YOYO, All Rights Reserved.
-
// Author: LIN YiQian
-
// Created: 2009/08/24
-
// Describe: STL bitset 使用DEMO
-
//////////////////////////////////////////////////////////////////////////
-
#include <iostream>
-
#include <bitset>
-
#include <cassert>
-
-
using namespace std;
-
-
void main(void)
-
{
-
bitset<16> bset1; // 16位的bitset,初始化为0
-
assert(bset1.none()); // 没有位被设置
-
-
bitset<16> bset2(0xFFFF); // 16位的bitset,初始化为1
-
assert(16 == bset2.count()); // 被设置的位数为16个
-
-
bitset<32> bset3(0xFFFF); // 32位的bitset,低16位初始化为1,高16位仍为0
-
assert(0xFFFF == bset3.to_ulong());
-
-
bitset<8> bset4(0xFFFF); // 8位则全部初始化为1
-
assert(8 == bset4.count());
-
-
bitset<8> bset5(string("111110000011")); // 8位则使用字符串的前8个数字进行初始化
-
assert(0xF8 == bset5.to_ulong()); // 0-7位为:0001 1111(倒过来)
-
-
bitset<8> bset6(string("111110000011"), 4, 3); // 使用字符串第5个数字(下标4)开始3个数字("100")进行初始化
-
assert(0x04 == bset6.to_ulong()); // 0-7位为:0010 0000(倒过来)
-
-
assert(bitset<8>() == (bset5 & bset6)); // 0001 1111 & 0010 0000 = 0000 0000
-
assert(bitset<8>(string("11111100")) == (bset5 | bset6)); // 0001 1111 | 0010 0000 = 0011 1111
-
assert(bitset<8>(string("11111100")) == (bset5 ^ bset6)); // 0001 1111 ^ 0010 0000 = 0011 1111
-
-
bset5[1] = 1; // bset5变成:0101 1111
-
assert(0xFA == bset5.to_ulong());
-
-
bool b = ~bset5[1]; // b = 0, bset5[1]不变
-
assert(!b);
-
assert(0xFA == bset5.to_ulong());
-
-
bset5[1].flip(); // bset5[1]取反
-
assert(0xF8 == bset5.to_ulong());
-
-
assert(bitset<8>(string("10000000")) == (bset5 << 4)); // bset5左移4位 等于1000 0000
-
assert(bitset<8>(string("00001111")) == (bset5 >> 4)); // bset5右移4位 等于0000 1111
-
-
bitset<8> bset7;
-
cout << "Before Input: " << bset7 << endl;
-
-
cout << "Please Input: ";
-
cin >> bset7;
-
cout << "After Input: " << bset7 << endl;
-
-
system("pause");
-
}
-
- priority_queue
-
//////////////////////////////////////////////////////////////////////////
-
// CopyRight(c) 2009, YOYO, All Rights Reserved.
-
// Author: LIN YiQian
-
// Created: 2009/08/24
-
// Describe: STL priority_queue 使用DEMO
-
//////////////////////////////////////////////////////////////////////////
-
#include <iostream>
-
#include <queue>
-
#include <vector>
-
#include <Windows.h>
-
-
using namespace std;
-
-
// 队列元素
-
template <class T>
-
class PriorityMessage
-
{
-
public:
-
PriorityMessage(int priority, T objMessage): m_nPriority(priority), m_objMessage(objMessage)
-
{
-
LARGE_INTEGER liValue;
-
::QueryPerformanceCounter(&liValue);
-
m_tInQueue = liValue.LowPart;
-
}
-
~PriorityMessage()
-
{
-
}
-
-
public:
-
int m_nPriority;
-
unsigned long m_tInQueue;
-
T m_objMessage;
-
};
-
-
// 比较器
-
template <class _Ty>
-
struct PMessageCmp: public binary_function<_Ty, _Ty, bool>
-
{
-
bool operator()(const PriorityMessage<_Ty>& _Left, const PriorityMessage<_Ty>& _Right) const
-
{
-
if (_Left.m_nPriority != _Right.m_nPriority)
-
{
-
return _Left.m_nPriority < _Right.m_nPriority;
-
}
-
else
-
{
-
return _Left.m_tInQueue < _Right.m_tInQueue;
-
}
-
}
-
};
-
-
typedef PriorityMessage<string> STR_PM;
-
typedef priority_queue<STR_PM, vector<STR_PM>, PMessageCmp<string> > STR_PMQ;
-
-
void main(void)
-
{
-
STR_PMQ pmqStr;
-
-
// 插入优先队列
-
pmqStr.push(STR_PM(1, "level11"));
-
pmqStr.push(STR_PM(3, "level31"));
-
pmqStr.push(STR_PM(1, "level12"));
-
pmqStr.push(STR_PM(2, "level21"));
-
pmqStr.push(STR_PM(3, "level32"));
-
pmqStr.push(STR_PM(4, "level41"));
-
pmqStr.push(STR_PM(5, "level51"));
-
pmqStr.push(STR_PM(2, "level23"));
-
pmqStr.push(STR_PM(4, "level42"));
-
pmqStr.push(STR_PM(1, "level13"));
-
pmqStr.push(STR_PM(2, "level24"));
-
pmqStr.push(STR_PM(4, "level43"));
-
pmqStr.push(STR_PM(4, "level44"));
-
-
// 打印优先队列
-
while (!pmqStr.empty())
-
{
-
STR_PM& pmStr = pmqStr.top(); // 获取头部元素
-
cout << "[LEVEL: " << pmStr.m_nPriority << "] "
-
<< "[QUEUE TIME: " << pmStr.m_tInQueue << "] "
-
<< "[MESSAGE: " << pmStr.m_objMessage.c_str() << "] " << endl;
-
pmqStr.pop();
-
}
-
-
system("pause");
-
}
-
- 无匹配