Liny_@NotePad

沉迷ACG中

【20090824】C++培训日记-STL扫盲

YOYO posted @ 2009年8月25日 00:09 in 【C/C++】 with tags STL 容器 , 2345 阅读

今天是几个常用容器的基本使用。。于是放几个DEMO上来:

包括 vector, deque, list, map, multimap, set, multiset, stack, queue,以及较少用到的valarray, bitset, priority_queue

  • vector
    1. //////////////////////////////////////////////////////////////////////////
    2. //      CopyRight(c) 2009, YOYO, All Rights Reserved.
    3. //      Author: LIN YiQian
    4. //      Created: 2009/08/24
    5. //      Describe: STL vector 使用DEMO
    6. //////////////////////////////////////////////////////////////////////////
    7. #include <iostream>
    8. #include <vector>
    9.  
    10. using namespace std;
    11.  
    12. typedef vector<int> INT_VEC;
    13.  
    14. // 打印Vector
    15. void PrintVector(INT_VEC vecInt)
    16. {
    17.         for (INT_VEC::iterator vecIter = vecInt.begin(); vecIter != vecInt.end(); ++vecIter)
    18.         {
    19.                 cout << *vecIter ;
    20.         }
    21.         cout << endl;
    22. }
    23.  
    24. // 逆向打印Vector
    25. void PrintVectorReversse(INT_VEC vecInt)
    26. {
    27.         for (INT_VEC::reverse_iterator vecRIter = vecInt.rbegin(); vecRIter != vecInt.rend(); ++vecRIter)
    28.         {
    29.                 cout << *vecRIter;
    30.         }
    31.         cout << endl;
    32. }
    33.  
    34. void main(void)
    35. {
    36.         INT_VEC vecInt1;
    37.         cout << "vecInt1(): "; PrintVector(vecInt1);
    38.  
    39.         //      insert()
    40.         {
    41.                 vecInt1.insert(vecInt1.begin(), 5);
    42.                 vecInt1.insert(vecInt1.begin()+1, 4, 6);
    43.                 cout << "vecInt1() after Insert: "; PrintVector(vecInt1);
    44.         }
    45.  
    46.         INT_VEC vecInt2(10, 3);
    47.         cout << "vecInt2(10, 3): "; PrintVector(vecInt2);
    48.  
    49.         //      erase()
    50.         {
    51.                 vecInt2.erase(vecInt2.begin()+5);
    52.                 cout << "vecInt2 after Erase: "; PrintVector(vecInt2);
    53.  
    54.                 cout << "vecInt2 size: " << vecInt2.size() << endl;
    55.  
    56.                 vecInt2.erase(vecInt2.begin()+3, vecInt2.end()-3);
    57.                 cout << "vecInt2 after Erase: "; PrintVector(vecInt2);
    58.         }
    59.  
    60.         //      size()
    61.         {
    62.                 cout << "vecInt2 size: " << vecInt2.size() << endl;
    63.         }
    64.  
    65.         //      capacity()
    66.         {
    67.                 cout << "vecInt2 Capacity: " << vecInt2.capacity() << endl;
    68.         }
    69.  
    70.         //      max_size()
    71.         {
    72.                 cout << "vecInt2 Max size: " << vecInt2.max_size() << endl;
    73.         }
    74.  
    75.         INT_VEC vecInt3(vecInt2);
    76.         cout << "vecInt3(vecInt2): "; PrintVector(vecInt3);
    77.  
    78.         //      insert()
    79.         {
    80.                 vecInt3.insert(vecInt3.begin(), vecInt1.begin(), vecInt1.end()-1);
    81.                 cout << "vecInt1 after Insert: "; PrintVector(vecInt1);
    82.                 cout << "vecInt3 after Insert: "; PrintVector(vecInt3);
    83.         }
    84.  
    85.         //      reverse()
    86.         {
    87.                 cout << "vecInt3 Reverse: "; PrintVectorReversse(vecInt3);
    88.         }
    89.  
    90.         INT_VEC vecInt4(vecInt3.begin()+2, vecInt3.end()-5);
    91.         cout << "vecInt4(vecInt3.begin()+2, vecInt3.end()-5): "; PrintVector(vecInt4);
    92.  
    93.         //      assign()
    94.         {
    95.                 vecInt4.assign(4, 7);
    96.                 cout << "vecInt4 after Assign: "; PrintVector(vecInt4);
    97.         }
    98.  
    99.         //      pop_back() & push_back()
    100.         {
    101.                 vecInt4.pop_back();
    102.                 cout << "vecInt4 after Pop_Back: "; PrintVector(vecInt4);
    103.  
    104.                 vecInt4.push_back(8);
    105.                 cout << "vecInt4 after Push_Back: "; PrintVector(vecInt4);
    106.         }
    107.  
    108.         //      at() & operator[] - Get Value
    109.         {
    110.                 cout << "vecInt4 first elem: " << vecInt4.at(0) << endl;
    111.                 cout << "vecInt3 first elem: " << vecInt3[0] << endl;
    112.         }
    113.  
    114.         //      operator cmp
    115.         {
    116.                 cout << "vecInt4 == vecInt3?: " << boolalpha << (vecInt4 == vecInt3) << endl;
    117.                 cout << "vecInt4 < vecInt3?: " << boolalpha << (vecInt4 < vecInt3) << endl;
    118.         }
    119.  
    120.         //      swap()
    121.         {
    122.                 vecInt4.swap(vecInt3);
    123.                 cout << "vecInt4 after Swap: "; PrintVector(vecInt4);
    124.                 cout << "vecInt3 after Swap: "; PrintVector(vecInt3);
    125.         }
    126.  
    127.         //      front() & back()
    128.         {
    129.                 cout << "vecInt4 front: " << vecInt4.front() << endl;
    130.                 cout << "vecInt4 back: " << vecInt4.back() << endl;
    131.         }
    132.  
    133.         //      empty()
    134.         {
    135.                 cout << "vecInt4 Empty?: " << boolalpha << vecInt4.empty() << endl;
    136.         }
    137.  
    138.         //      clear()
    139.         {
    140.                 vecInt4.clear();
    141.                 cout << "vecInt4 after Clear: "; PrintVector(vecInt4);
    142.                 cout << "vecInt4 size: " << vecInt4.size() << endl;
    143.                 cout << "vecInt4 Empty?: " << boolalpha << vecInt4.empty() << endl;
    144.         }
    145.        
    146.         system("pause");
    147. }
  • deque
    1. //////////////////////////////////////////////////////////////////////////
    2. //      CopyRight(c) 2009, YOYO, All Rights Reserved.
    3. //      Author: LIN YiQian
    4. //      Created: 2009/08/24
    5. //      Describe: STL deque 使用DEMO
    6. //////////////////////////////////////////////////////////////////////////
    7. #include <iostream>
    8. #include <deque>
    9.  
    10. using namespace std;
    11.  
    12. typedef deque<int> INT_DEQ;
    13.  
    14. // 打印Deque
    15. void PrintDeque(INT_DEQ deqInt)
    16. {
    17.         for (INT_DEQ::iterator deqIter = deqInt.begin(); deqIter != deqInt.end(); ++deqIter)
    18.         {
    19.                 cout << *deqIter;
    20.         }
    21.         cout << endl;
    22. }
    23.  
    24. // 逆向打印Deque
    25. void PrintDequeReverse(INT_DEQ deqInt)
    26. {
    27.         for (INT_DEQ::reverse_iterator deqRIter = deqInt.rbegin(); deqRIter != deqInt.rend(); ++deqRIter)
    28.         {
    29.                 cout << *deqRIter;
    30.         }
    31.         cout << endl;
    32. }
    33.  
    34. void main(void)
    35. {
    36.         INT_DEQ deqInt1;
    37.         cout << "deqInt1: "; PrintDeque(deqInt1);
    38.  
    39.         //      push_front() & push_back()
    40.         {
    41.                 deqInt1.push_front(3);
    42.                 deqInt1.push_front(4);
    43.                 deqInt1.push_back(6);
    44.                 deqInt1.push_back(9);
    45.                 cout << "deqInt1 after Push: "; PrintDeque(deqInt1);
    46.         }
    47.  
    48.         //      reverse()
    49.         {
    50.                 cout << "deqInt1 Reverse: "; PrintDequeReverse(deqInt1);
    51.         }
    52.  
    53.         //      size()
    54.         {
    55.                 cout << "deqInt1 size: " << deqInt1.size() << endl;
    56.         }
    57.  
    58.         //      max_size()
    59.         {
    60.                 cout << "deqInt1 max_size: " << deqInt1.max_size() << endl;
    61.         }
    62.  
    63.         INT_DEQ deqInt2(5, 5);
    64.         cout << "deqInt2(5, 5): "; PrintDeque(deqInt2);
    65.  
    66.         //      erase()
    67.         {
    68.                 deqInt2.erase(deqInt2.begin()+2);
    69.                 cout << "deqInt2 after Erase: "; PrintDeque(deqInt2);
    70.  
    71.                 deqInt2.erase(deqInt2.begin()+1, deqInt2.end()-1);
    72.                 cout << "deqInt2 after Erase: "; PrintDeque(deqInt2);
    73.         }
    74.  
    75.         //      empty()
    76.         {
    77.                 cout << "deqInt2 empty?: " << boolalpha << deqInt2.empty();
    78.         }
    79.  
    80.         INT_DEQ deqInt3(deqInt1);
    81.         cout << "deqInt3(deqInt1): "; PrintDeque(deqInt3);
    82.  
    83.         //      pop_front() & pop_back()
    84.         {
    85.                 deqInt3.pop_front();
    86.                 deqInt3.pop_back();
    87.                 cout << "deqInt3 after Pop: "; PrintDeque(deqInt3);
    88.         }
    89.  
    90.         //      assign()
    91.         {
    92.                 deqInt3.assign(6, 6);
    93.                 cout << "deqInt3 after Assign: "; PrintDeque(deqInt3);
    94.         }
    95.  
    96.         INT_DEQ deqInt4(deqInt3.begin()+2, deqInt3.end());
    97.         cout << "deqInt4(deqInt3.begin()+2, deqInt3.end()): "; PrintDeque(deqInt4);
    98.  
    99.         //      at() and operator[]
    100.         {
    101.                 cout << "deqInt4 at(3): " << deqInt4.at(3) << endl;
    102.                 cout << "deqInt4[2]: " << deqInt4[2] << endl;
    103.         }
    104.        
    105.         //      front() and back()
    106.         {
    107.                 cout << "deqInt4 front: " << deqInt4.front() << endl;
    108.                 cout << "deqInt4 back: " << deqInt4.back() << endl;
    109.         }
    110.  
    111.         //      operator cmp
    112.         {
    113.                 cout << "deqInt4 == deqInt2?: " << boolalpha << (deqInt4 == deqInt2) << endl;
    114.                 cout << "deqInt4 >= deqInt2?: " << boolalpha << (deqInt4 >= deqInt2) << endl;
    115.         }
    116.  
    117.         //      swap()
    118.         {
    119.                 deqInt4.swap(deqInt1);
    120.                 cout << "deqInt1 after Swap: "; PrintDeque(deqInt1);
    121.                 cout << "deqInt4 after Swap: "; PrintDeque(deqInt4);
    122.         }
    123.  
    124.         //      clear()
    125.         {
    126.                 deqInt4.clear();
    127.                 cout << "deqInt4 after Clear: "; PrintDeque(deqInt4);
    128.                 cout << "deqInt4 size: " << deqInt4.size() << endl;
    129.                 cout << "deqInt4 empty?: " << boolalpha << deqInt4.empty() << endl;
    130.         }
    131.  
    132.         system("pause");
    133. }
  • list
    1. //////////////////////////////////////////////////////////////////////////
    2. //      CopyRight(c) 2009, YOYO, All Rights Reserved.
    3. //      Author: LIN YiQian
    4. //      Created: 2009/08/24
    5. //      Describe: STL list 使用DEMO
    6. //////////////////////////////////////////////////////////////////////////
    7. #include <iostream>
    8. #include <list>
    9.  
    10. using namespace std;
    11.  
    12. typedef list<int> INT_LST;
    13.  
    14. // 打印List
    15. void PrintList(INT_LST lstInt)
    16. {
    17.         for (INT_LST::iterator lstIter = lstInt.begin(); lstIter != lstInt.end(); ++lstIter)
    18.         {
    19.                 cout << *lstIter;
    20.         }
    21.         cout << endl;
    22. }
    23.  
    24. // 逆向打印List
    25. void PrintListReverse(INT_LST lstInt)
    26. {
    27.         for (INT_LST::reverse_iterator lstRIter = lstInt.rbegin(); lstRIter != lstInt.rend(); ++lstRIter)
    28.         {
    29.                 cout << *lstRIter;
    30.         }
    31.         cout << endl;
    32. }
    33.  
    34. void main(void)
    35. {
    36.         INT_LST lstInt1;
    37.         cout << "lstInt1: "; PrintList(lstInt1);
    38.  
    39.         //      push_front() & push_back()
    40.         {
    41.                 lstInt1.push_front(3);
    42.                 lstInt1.push_front(2);
    43.                 lstInt1.push_back(8);
    44.                 lstInt1.push_back(9);
    45.                 lstInt1.push_back(7);
    46.                 cout << "lstInt1 after Push: "; PrintList(lstInt1);
    47.         }
    48.  
    49.         //      insert()
    50.         {
    51.                 lstInt1.insert(++ ++lstInt1.begin(), 5);
    52.                 lstInt1.insert(++ lstInt1.begin(), 6);
    53.                 cout << "lstInt1 after Insert: "; PrintList(lstInt1);
    54.         }
    55.  
    56.         //      sort()
    57.         {
    58.                 lstInt1.sort();
    59.                 cout << "lstInt1 after Sort: "; PrintList(lstInt1);
    60.         }
    61.  
    62.         //      size() &  max_size()
    63.         {
    64.                 cout << "lstInt1 size: " << lstInt1.size() << endl;
    65.                 cout << "lstInt1 max_size: " << lstInt1.max_size() << endl;
    66.         }
    67.  
    68.         //      empty()
    69.         {
    70.                 cout << "lstInt1 Empty?: " << boolalpha << lstInt1.empty() << endl;
    71.         }
    72.  
    73.         //      reverse()
    74.         {
    75.                 cout << "lstInt1 reverse: "; PrintListReverse(lstInt1);
    76.         }
    77.  
    78.         //      front() & back()
    79.         {
    80.                 cout << "lstInt1 front: " << lstInt1.front() << endl;
    81.                 cout << "lstInt1 back: " << lstInt1.back() << endl;
    82.         }
    83.  
    84.         INT_LST lstInt2(7, 9);
    85.         cout << "lstInt2: "; PrintList(lstInt2);
    86.  
    87.         INT_LST lstInt3(lstInt1);
    88.         cout << "lstInt3: "; PrintList(lstInt3);
    89.  
    90.         //      pop_front() & pop_back()
    91.         {
    92.                 lstInt3.pop_front();
    93.                 lstInt3.pop_back();
    94.                 cout << "lstInt3 after Pop: "; PrintList(lstInt3);
    95.         }
    96.  
    97.         //      erase()
    98.         {
    99.                 lstInt3.erase(++lstInt3.begin());
    100.                 cout << "lstInt3 after Erase: "; PrintList(lstInt3);
    101.  
    102.                 lstInt3.erase(++lstInt3.begin(), --lstInt3.end());
    103.                 cout << "lstInt3 after Erase: "; PrintList(lstInt3);
    104.         }
    105.  
    106.         INT_LST lstInt4(lstInt2.begin(), lstInt2.end());
    107.         cout << "lstInt4: "; PrintList(lstInt4);
    108.  
    109.         //      assign()
    110.         {
    111.                 lstInt4.assign(9, 2);
    112.                 cout << "lstInt4 after Assign: "; PrintList(lstInt4);
    113.         }
    114.  
    115.         //      operator cmp
    116.         {
    117.                 cout << "lstInt4 == lstInt2?: " << boolalpha << (lstInt2 == lstInt4) << endl;
    118.                 cout << "lstInt4 <= lstInt2?: " << boolalpha << (lstInt4 <= lstInt2) << endl;
    119.         }
    120.  
    121.         //      splice()
    122.         {
    123.                 lstInt4.splice(++lstInt4.begin(), lstInt2, ++ ++lstInt2.begin(), --lstInt2.end());
    124.                 cout << "lstInt4 after Splice: "; PrintList(lstInt4);
    125.                 cout << "lstInt2 after Splice: "; PrintList(lstInt2);
    126.  
    127.                 lstInt4.splice(lstInt4.begin(), lstInt1, ++ lstInt1.begin());
    128.                 cout << "lstInt4 after Splice: "; PrintList(lstInt4);
    129.                 cout << "lstInt1 after Splice: "; PrintList(lstInt1);
    130.  
    131.                 lstInt4.splice(lstInt4.begin(), lstInt3);
    132.                 cout << "lstInt4 after Splice: "; PrintList(lstInt4);
    133.                 cout << "lstInt3 after Splice: "; PrintList(lstInt3);
    134.         }
    135.  
    136.         //      swap()
    137.         {
    138.                 lstInt4.swap(lstInt1);
    139.                 cout << "lstInt4 after Swap: "; PrintList(lstInt4);
    140.                 cout << "lstInt1 after Swap: "; PrintList(lstInt1);
    141.         }
    142.  
    143.         //      clear()
    144.         {
    145.                 lstInt4.clear();
    146.                 cout << "lstInt4 after Clear: "; PrintList(lstInt4);
    147.                 cout << "lstInt4 size: " << lstInt4.size() << endl;
    148.                 cout << "lstInt4 Empty?: " << boolalpha << lstInt4.empty() << endl;
    149.         }
    150.  
    151.         system("pause");
    152. }
  • map
    1. //////////////////////////////////////////////////////////////////////////
    2. //      CopyRight(c) 2009, YOYO, All Rights Reserved.
    3. //      Author: LIN YiQian
    4. //      Created: 2009/08/24
    5. //      Describe: STL map 使用DEMO
    6. //////////////////////////////////////////////////////////////////////////
    7. #include <iostream>
    8. #include <map>
    9. #include <string>
    10.  
    11. using namespace std;
    12.  
    13. typedef map<int, string> STR_MAP;
    14.  
    15. //      打印Map
    16. void PrintMap(STR_MAP strMap)
    17. {
    18.         for (STR_MAP::iterator strMapIter = strMap.begin(); strMapIter != strMap.end(); ++strMapIter)
    19.         {
    20.                 cout << (*strMapIter).first << "." << (*strMapIter).second << " " << endl;
    21.         }
    22.         cout << endl;
    23. }
    24.  
    25. //      反向打印Map
    26. void PrintMapReverse(STR_MAP strMap)
    27. {
    28.         for (STR_MAP::reverse_iterator strMapRIter = strMap.rbegin(); strMapRIter != strMap.rend(); ++strMapRIter)
    29.         {
    30.                 cout << (*strMapRIter).first << "." << (*strMapRIter).second << " " << endl;
    31.         }
    32.         cout << endl;
    33. }
    34.  
    35. //      打印指定范围的Map
    36. void PrintMapRange(STR_MAP strMap, STR_MAP::key_type low, STR_MAP::key_type up)
    37. {
    38.         for (STR_MAP::iterator strMapIter = strMap.lower_bound(low); strMapIter != strMap.upper_bound(up); ++strMapIter)
    39.         {
    40.                 cout << (*strMapIter).first << "." << (*strMapIter).second << " " << endl;
    41.         }
    42.         cout << endl;
    43. }
    44.  
    45. void main(void)
    46. {
    47.         STR_MAP strMap;
    48.         cout << "New StringMap: " << endl; PrintMap(strMap);
    49.  
    50.         //      insert()
    51.         {
    52.                 strMap.insert(STR_MAP::value_type(0, "Zero"));
    53.                 strMap.insert(STR_MAP::value_type(1, "One"));
    54.                 strMap.insert(STR_MAP::value_type(2, "Two"));
    55.                 strMap.insert(STR_MAP::value_type(6, "Six"));
    56.                 strMap.insert(STR_MAP::value_type(4, "Four"));
    57.                 strMap.insert(STR_MAP::value_type(8, "Eight"));
    58.                 strMap.insert(STR_MAP::value_type(9, "Nine"));
    59.                 cout << "After Insert: " << endl;
    60.                 PrintMap(strMap);
    61.         }
    62.  
    63.         //      test insert() again
    64.         {
    65.                 strMap.insert(STR_MAP::value_type(1, "One"));   // fail
    66.                 strMap.insert(STR_MAP::value_type(3, "Three"))//       success
    67.                 strMap.insert(STR_MAP::value_type(4, "Four"))//        fail
    68.                 strMap.insert(STR_MAP::value_type(8, "Eight"))//       fail
    69.                 strMap.insert(STR_MAP::value_type(7, "Seven"))//       success
    70.                 strMap.insert(STR_MAP::value_type(5, "Five"))//        success
    71.                 cout << "After Insert again: " << endl;
    72.                 PrintMap(strMap);
    73.         }
    74.  
    75.         //      Print Range Map
    76.         {
    77.                 cout << "Print Map in Range: low-2, up-7" << endl;
    78.                 PrintMapRange(strMap, 2, 7);
    79.         }
    80.  
    81.         //      Print Reverse Map
    82.         {
    83.                 cout << "Print Map Reverse: " << endl;
    84.                 PrintMapReverse(strMap);
    85.         }
    86.  
    87.         //      find()
    88.         {
    89.                 cout << "Find Value 3?: " << boolalpha << (strMap.find(3) != strMap.end()) << endl;
    90.                 cout << "Find Value 12?: " << boolalpha << (strMap.find(12) != strMap.end()) << endl;
    91.                 cout << endl;
    92.         }
    93.  
    94.         //      erase()
    95.         {
    96.                 strMap.erase(9);
    97.                 strMap.erase(19);
    98.                 cout << "After Erase 9, 19: " << endl; PrintMap(strMap);
    99.  
    100.                 strMap.erase(strMap.find(2));
    101.                 cout << "After Erase 2: " << endl; PrintMap(strMap);
    102.         }
    103.  
    104.         //      count()
    105.         {
    106.                 cout << "Value 4 counts: " << strMap.count(4) << endl;
    107.                 cout << "Value 9 counts: " << strMap.count(9) << endl;
    108.                 cout << endl;
    109.         }
    110.  
    111.         //      size() & max_size()
    112.         {
    113.                 cout << "Map size: " << strMap.size() << endl;
    114.                 cout << "Map max_size: " << strMap.max_size() << endl;
    115.                 cout << endl;
    116.         }
    117.  
    118.         //      empty()
    119.         {
    120.                 cout << "Map Empty?: " << boolalpha << strMap.empty() << endl;
    121.                 cout << endl;
    122.         }
    123.  
    124.         //      A Demo for Using Map to Search
    125.         {
    126.                 string inKey = "";
    127.                 while (true)
    128.                 {
    129.                         cout << "Input 'q' to exit, or Input a Number: ";
    130.                         cin >> inKey;
    131.  
    132.                         if (inKey == "q")
    133.                         {
    134.                                 break;
    135.                         }
    136.  
    137.                         string::size_type strLen = inKey.length();
    138.                         string::size_type index = 0;
    139.  
    140.                         for (; index < strLen; ++index)
    141.                         {
    142.                                 string::reference ch = inKey[index];
    143.                                 STR_MAP::key_type key = ch - STR_MAP::key_type('0');
    144.  
    145.                                 STR_MAP::iterator strMapIter = strMap.find(key);
    146.  
    147.                                 if (strMapIter == strMap.end())
    148.                                 {
    149.                                         cout << "[not found] " ;
    150.                                 }
    151.                                 else
    152.                                 {
    153.                                         cout << "[" << (*strMapIter).second << "] ";
    154.                                 }
    155.                         }
    156.  
    157.                         cout << endl;
    158.                 }
    159.  
    160.                 cout << endl;
    161.         }
    162.  
    163.         //      clear()
    164.         {
    165.                 strMap.clear();
    166.                 cout << "After Clear: " << endl;
    167.                 PrintMap(strMap);
    168.  
    169.                 cout << "Map size: " << strMap.size() << endl;
    170.                 cout << "Map Empty?: " << boolalpha << strMap.empty() << endl;
    171.         }
    172.  
    173.         system("pause");
    174. }
  • multimap
    1. //////////////////////////////////////////////////////////////////////////
    2. //      CopyRight(c) 2009, YOYO, All Rights Reserved.
    3. //      Author: LIN YiQian
    4. //      Created: 2009/08/24
    5. //      Describe: STL multimap 使用DEMO
    6. //////////////////////////////////////////////////////////////////////////
    7. #include <iostream>
    8. #include <map>
    9. #include <string>
    10.  
    11. using namespace std;
    12.  
    13. typedef multimap<int, string> STR_MMAP;
    14.  
    15. //      打印MultiMap
    16. void PrintMap(STR_MMAP strMap)
    17. {
    18.         for (STR_MMAP::iterator strMapIter = strMap.begin(); strMapIter != strMap.end(); ++strMapIter)
    19.         {
    20.                 cout << (*strMapIter).first << "." << (*strMapIter).second << " " << endl;
    21.         }
    22.         cout << endl;
    23. }
    24.  
    25. //      反向打印MultiMap
    26. void PrintMapReverse(STR_MMAP strMap)
    27. {
    28.         for (STR_MMAP::reverse_iterator strMapRIter = strMap.rbegin(); strMapRIter != strMap.rend(); ++strMapRIter)
    29.         {
    30.                 cout << (*strMapRIter).first << "." << (*strMapRIter).second << " " << endl;
    31.         }
    32.         cout << endl;
    33. }
    34.  
    35. //      打印指定范围的MultiMap
    36. void PrintMapRange(STR_MMAP strMap, STR_MMAP::key_type low, STR_MMAP::key_type up)
    37. {
    38.         for (STR_MMAP::iterator strMapIter = strMap.lower_bound(low); strMapIter != strMap.upper_bound(up); ++strMapIter)
    39.         {
    40.                 cout << (*strMapIter).first << "." << (*strMapIter).second << " " << endl;
    41.         }
    42.         cout << endl;
    43. }
    44.  
    45. void main(void)
    46. {
    47.         STR_MMAP strMap;
    48.         cout << "New StringMultiMap: " << endl; PrintMap(strMap);
    49.  
    50.         //      insert()
    51.         {
    52.                 strMap.insert(STR_MMAP::value_type(80, "张三"));
    53.                 strMap.insert(STR_MMAP::value_type(70, "李四"));
    54.                 strMap.insert(STR_MMAP::value_type(90, "王五"));
    55.                 strMap.insert(STR_MMAP::value_type(75, "赵六"));
    56.                 cout << "After Insert: " << endl;
    57.                 PrintMap(strMap);
    58.         }
    59.  
    60.         //      test insert() again
    61.         {
    62.                 strMap.insert(STR_MMAP::value_type(80, "张A"));
    63.                 strMap.insert(STR_MMAP::value_type(85, "DFA"));
    64.                 strMap.insert(STR_MMAP::value_type(90, "王C"));
    65.                 strMap.insert(STR_MMAP::value_type(95, "ASDF"));
    66.                 cout << "After Insert again: " << endl;
    67.                 PrintMap(strMap);
    68.         }
    69.  
    70.         //      Print Range Map
    71.         {
    72.                 cout << "Print Map in Range: low-80, up-90" << endl;
    73.                 PrintMapRange(strMap, 80, 90);
    74.         }
    75.  
    76.         //      Print Reverse Map
    77.         {
    78.                 cout << "Print Map Reverse: " << endl;
    79.                 PrintMapReverse(strMap);
    80.         }
    81.  
    82.         //      find()
    83.         {
    84.                 cout << "Find Value 80?: " << boolalpha << (strMap.find(80) != strMap.end()) << endl;
    85.                 cout << "Find Value 82?: " << boolalpha << (strMap.find(82) != strMap.end()) << endl;
    86.                 cout << endl;
    87.         }
    88.  
    89.         //      erase()
    90.         {
    91.                 strMap.erase(85);
    92.                 strMap.erase(91);
    93.                 cout << "After Erase 85, 91: " << endl; PrintMap(strMap);
    94.  
    95.                 strMap.erase(strMap.find(70));
    96.                 cout << "After Erase 70: " << endl; PrintMap(strMap);
    97.         }
    98.  
    99.         //      count()
    100.         {
    101.                 cout << "Value 80 counts: " << strMap.count(80) << endl;
    102.                 cout << "Value 75 counts: " << strMap.count(75) << endl;
    103.                 cout << "Value 60 counts: " << strMap.count(60) << endl;
    104.                 cout << endl;
    105.         }
    106.  
    107.         //      size() & max_size()
    108.         {
    109.                 cout << "Map size: " << strMap.size() << endl;
    110.                 cout << "Map max_size: " << strMap.max_size() << endl;
    111.                 cout << endl;
    112.         }
    113.  
    114.         //      empty()
    115.         {
    116.                 cout << "Map Empty?: " << boolalpha << strMap.empty() << endl;
    117.                 cout << endl;
    118.         }
    119.  
    120.         //      clear()
    121.         {
    122.                 strMap.clear();
    123.                 cout << "After Clear: " << endl;
    124.                 PrintMap(strMap);
    125.  
    126.                 cout << "Map size: " << strMap.size() << endl;
    127.                 cout << "Map Empty?: " << boolalpha << strMap.empty() << endl;
    128.         }
    129.  
    130.         system("pause");
    131. }
  • set
    1. //////////////////////////////////////////////////////////////////////////
    2. //      CopyRight(c) 2009, YOYO, All Rights Reserved.
    3. //      Author: LIN YiQian
    4. //      Created: 2009/08/24
    5. //      Describe: STL set 使用DEMO
    6. //////////////////////////////////////////////////////////////////////////
    7. #include <iostream>
    8. #include <set>
    9.  
    10. using namespace std;
    11.  
    12. typedef set<int> INT_SET;
    13.  
    14. //      打印Set
    15. void PrintSet(INT_SET setInt)
    16. {
    17.         for (INT_SET::iterator setIntIter = setInt.begin(); setIntIter != setInt.end(); ++setIntIter)
    18.         {
    19.                 cout << (*setIntIter) << " ";
    20.         }
    21.         cout << endl;
    22. }
    23.  
    24. //      反向打印Set
    25. void PrintSetReverse(INT_SET setInt)
    26. {
    27.         for (INT_SET::reverse_iterator setIntIter = setInt.rbegin(); setIntIter != setInt.rend(); ++setIntIter)
    28.         {
    29.                 cout << (*setIntIter) << " ";
    30.         }
    31.         cout << endl;
    32. }
    33.  
    34. //      打印指定范围的Set
    35. void PrintSetRange(INT_SET setInt, INT_SET::value_type low, INT_SET::value_type up)
    36. {
    37.         for (INT_SET::iterator setIntIter = setInt.lower_bound(low); setIntIter != setInt.upper_bound(up); ++setIntIter)
    38.         {
    39.                 cout << (*setIntIter) << " ";
    40.         }
    41.         cout << endl;
    42. }
    43.  
    44. void main(void)
    45. {
    46.         INT_SET setInt;
    47.         cout << "New Int Set: "; PrintSet(setInt);
    48.  
    49.         //      insert()
    50.         {
    51.                 setInt.insert(3);
    52.                 setInt.insert(2);
    53.                 setInt.insert(1);
    54.                 setInt.insert(4);
    55.                 cout << "After Insert: "; PrintSet(setInt);
    56.         }
    57.  
    58.         //      test insert() again
    59.         {
    60.                 setInt.insert(3);       //     fail
    61.                 setInt.insert(5);       //     success
    62.                 setInt.insert(7);       //     success
    63.                 setInt.insert(4);       //     fail
    64.                 setInt.insert(8);       //     success
    65.                 setInt.insert(9);       //     success
    66.                 cout << "After Insert again: "; PrintSet(setInt);
    67.         }
    68.  
    69.         //      reverse()
    70.         {
    71.                 cout << "Set Reverse: "; PrintSetReverse(setInt);
    72.         }
    73.  
    74.         //      range()
    75.         {
    76.                 cout << "Set Range(low-3, up-8): "; PrintSetRange(setInt, 3, 8);
    77.         }
    78.  
    79.         //      size() & max_size()
    80.         {
    81.                 cout << "Set size: " << setInt.size() << endl;
    82.                 cout << "Set max_size: " << setInt.max_size() << endl;
    83.         }
    84.  
    85.         //      empty()
    86.         {
    87.                 cout << "Set Empty?: " << boolalpha << setInt.empty() << endl;
    88.         }
    89.  
    90.         //      find()
    91.         {
    92.                 cout << "Value 7 find?: " << boolalpha << (setInt.find(7) != setInt.end()) << endl;
    93.                 cout << "Value 6 find?: " << boolalpha << (setInt.find(6) != setInt.end()) << endl;
    94.         }
    95.  
    96.         //      count()
    97.         {
    98.                 cout << "Value 3 count: " << setInt.count(3) << endl;
    99.                 cout << "Value -5 count: " << setInt.count(-5) << endl;
    100.         }
    101.  
    102.         //      clear()
    103.         {
    104.                 setInt.clear();
    105.                 cout << "After Clear: "; PrintSet(setInt);
    106.                 cout << "Set size: " << setInt.size() << endl;
    107.                 cout << "Set Empty?: " << boolalpha << setInt.empty() << endl;
    108.         }
    109.  
    110.         system("pause");
    111. }
  • multiset
    1. //////////////////////////////////////////////////////////////////////////
    2. //      CopyRight(c) 2009, YOYO, All Rights Reserved.
    3. //      Author: LIN YiQian
    4. //      Created: 2009/08/24
    5. //      Describe: STL multiset 使用DEMO
    6. //////////////////////////////////////////////////////////////////////////
    7. #include <iostream>
    8. #include <set>
    9.  
    10. using namespace std;
    11.  
    12. typedef multiset<int> INT_MSET;
    13.  
    14. //      打印Set
    15. void PrintSet(INT_MSET setInt)
    16. {
    17.         for (INT_MSET::iterator setIntIter = setInt.begin(); setIntIter != setInt.end(); ++setIntIter)
    18.         {
    19.                 cout << (*setIntIter) << " ";
    20.         }
    21.         cout << endl;
    22. }
    23.  
    24. //      反向打印Set
    25. void PrintSetReverse(INT_MSET setInt)
    26. {
    27.         for (INT_MSET::reverse_iterator setIntIter = setInt.rbegin(); setIntIter != setInt.rend(); ++setIntIter)
    28.         {
    29.                 cout << (*setIntIter) << " ";
    30.         }
    31.         cout << endl;
    32. }
    33.  
    34. //      打印指定范围的Set
    35. void PrintSetRange(INT_MSET setInt, INT_MSET::value_type low, INT_MSET::value_type up)
    36. {
    37.         for (INT_MSET::iterator setIntIter = setInt.lower_bound(low); setIntIter != setInt.upper_bound(up); ++setIntIter)
    38.         {
    39.                 cout << (*setIntIter) << " ";
    40.         }
    41.         cout << endl;
    42. }
    43.  
    44. void main(void)
    45. {
    46.         INT_MSET setInt;
    47.         cout << "New Int Set: "; PrintSet(setInt);
    48.  
    49.         //      insert()
    50.         {
    51.                 setInt.insert(3);
    52.                 setInt.insert(2);
    53.                 setInt.insert(1);
    54.                 setInt.insert(4);
    55.                 cout << "After Insert: "; PrintSet(setInt);
    56.         }
    57.  
    58.         //      test insert() again
    59.         {
    60.                 setInt.insert(3);
    61.                 setInt.insert(5);
    62.                 setInt.insert(7);
    63.                 setInt.insert(4);
    64.                 setInt.insert(8);
    65.                 setInt.insert(9);
    66.                 cout << "After Insert again: "; PrintSet(setInt);
    67.         }
    68.  
    69.         //      reverse()
    70.         {
    71.                 cout << "Set Reverse: "; PrintSetReverse(setInt);
    72.         }
    73.  
    74.         //      range()
    75.         {
    76.                 cout << "Set Range(low-3, up-8): "; PrintSetRange(setInt, 3, 8);
    77.         }
    78.  
    79.         //      size() & max_size()
    80.         {
    81.                 cout << "Set size: " << setInt.size() << endl;
    82.                 cout << "Set max_size: " << setInt.max_size() << endl;
    83.         }
    84.  
    85.         //      empty()
    86.         {
    87.                 cout << "Set Empty?: " << boolalpha << setInt.empty() << endl;
    88.         }
    89.  
    90.         //      find()
    91.         {
    92.                 cout << "Value 7 find?: " << boolalpha << (setInt.find(7) != setInt.end()) << endl;
    93.                 cout << "Value 6 find?: " << boolalpha << (setInt.find(6) != setInt.end()) << endl;
    94.         }
    95.  
    96.         //      count()
    97.         {
    98.                 cout << "Value 3 count: " << setInt.count(3) << endl;
    99.                 cout << "Value 1 count: " << setInt.count(1) << endl;
    100.                 cout << "Value -5 count: " << setInt.count(-5) << endl;
    101.         }
    102.  
    103.         //      clear()
    104.         {
    105.                 setInt.clear();
    106.                 cout << "After Clear: "; PrintSet(setInt);
    107.                 cout << "Set size: " << setInt.size() << endl;
    108.                 cout << "Set Empty?: " << boolalpha << setInt.empty() << endl;
    109.         }
    110.  
    111.         system("pause");
    112. }
  • stack
    1. //////////////////////////////////////////////////////////////////////////
    2. //      CopyRight(c) 2009, YOYO, All Rights Reserved.
    3. //      Author: LIN YiQian
    4. //      Created: 2009/08/24
    5. //      Describe: STL stack 使用DEMO
    6. //////////////////////////////////////////////////////////////////////////
    7. #include <iostream>
    8. #include <stack>
    9.  
    10. using namespace std;
    11.  
    12. typedef stack<int> INT_STK;
    13.  
    14. void main(void)
    15. {
    16.         INT_STK stkInt;
    17.  
    18.         //      Print is stack empty?
    19.         cout << "Stack Empty?: " << boolalpha << stkInt.empty() << endl;
    20.  
    21.         //      Push elements
    22.         for (int i = 0; i < 10; i += 2)
    23.         {
    24.                 stkInt.push(i);
    25.         }
    26.  
    27.         //      Get size
    28.         cout << "Stack size: " << stkInt.size() << endl;
    29.  
    30.         //      Get top element & change its value
    31.         if (!stkInt.empty())
    32.         {
    33.                 cout << "Top Element: " << stkInt.top() << " change to 99 " << endl;
    34.                 stkInt.top() = 99;
    35.         }
    36.  
    37.         //      Print stack
    38.         {
    39.                 while (!stkInt.empty())
    40.                 {
    41.                         cout << stkInt.top() << " ";
    42.                         stkInt.pop();
    43.                 }
    44.         }
    45.         cout << endl;
    46.  
    47.         system("pause");
    48. }
  • queue
    1. //////////////////////////////////////////////////////////////////////////
    2. //      CopyRight(c) 2009, YOYO, All Rights Reserved.
    3. //      Author: LIN YiQian
    4. //      Created: 2009/08/24
    5. //      Describe: STL queue 使用DEMO
    6. //////////////////////////////////////////////////////////////////////////
    7. #include <iostream>
    8. #include <queue>
    9.  
    10. using namespace std;
    11.  
    12. typedef queue<int> INT_QUE;
    13.  
    14. void main(void)
    15. {
    16.         INT_QUE intQue;
    17.         int x;
    18.  
    19.         //      Push
    20.         cout << "Please input some Non-negative integer(put negative integer to finish input): " << endl;
    21.         while (cin>>x)
    22.         {
    23.                 if (x<0) break;
    24.                 intQue.push(x);
    25.         }
    26.  
    27.         //      Size
    28.         cout << "Queue Size: " << intQue.size() << endl;
    29.  
    30.         //      Empty?
    31.         cout << "Queue Empty?: " << boolalpha << intQue.empty() << endl;
    32.  
    33.         //      Change Front Element & Back Element
    34.         {
    35.                 cout << "Queue front Element: " << intQue.front() << " Change to 99" << endl;
    36.                 intQue.front() = 99;
    37.  
    38.                 cout << "Queue back Element: " << intQue.back() << " Change to 33" << endl;
    39.                 intQue.back() = 33;
    40.         }
    41.  
    42.         //      Print Queue
    43.         {
    44.                 cout << "Queue: ";
    45.                 while (!intQue.empty())
    46.                 {
    47.                         cout << intQue.front() << " ";
    48.                         intQue.pop();
    49.                 }
    50.                 cout << endl;
    51.         }
    52.  
    53.         system("pause");
    54. }
  • valarray
    1. //////////////////////////////////////////////////////////////////////////
    2. //      CopyRight(c) 2009, YOYO, All Rights Reserved.
    3. //      Author: LIN YiQian
    4. //      Created: 2009/08/24
    5. //      Describe: STL valarray 使用DEMO
    6. //////////////////////////////////////////////////////////////////////////
    7. #include <iostream>
    8. #include <valarray>
    9.  
    10. using namespace std;
    11.  
    12. typedef valarray<double> DOUBLE_VAR;
    13.  
    14. const int VALARRAY_SIZE = 3;
    15.  
    16. void PrintArray(DOUBLE_VAR varDouble)
    17. {
    18.         for (int i = 0; i < varDouble.size(); ++i)
    19.         {
    20.                 cout << varDouble[i] << " ";
    21.         }
    22.         cout << endl;
    23. }
    24.  
    25. void main(void)
    26. {
    27.         DOUBLE_VAR varDouble(VALARRAY_SIZE);
    28.  
    29.         //      assign
    30.         for (int i = 1; i < 4; ++i)
    31.         {
    32.                 varDouble[i-1] = i*i;
    33.         }
    34.  
    35.         //      size
    36.         {
    37.                 cout << "ValArray size: " << varDouble.size() << endl;
    38.         }
    39.  
    40.         //      print array
    41.         {
    42.                 cout << "ValArray: "; PrintArray(varDouble);
    43.         }
    44.  
    45.         DOUBLE_VAR varIntSqrt(VALARRAY_SIZE);
    46.         varIntSqrt = sqrt(varDouble);
    47.  
    48.         //      print sqrt array
    49.         {
    50.                 cout << "Sqrt ValArray: "; PrintArray(varIntSqrt);
    51.         }
    52.  
    53.         DOUBLE_VAR varIntPow(VALARRAY_SIZE);
    54.         varIntPow = pow(varDouble, 2.0);
    55.  
    56.         //      print pow array
    57.         {
    58.                 cout << "Pow ValArray: "; PrintArray(varIntPow);
    59.         }
    60.  
    61.         //      sum, max & min
    62.         {
    63.                 cout << "Sum: " << varIntPow.sum() << endl;
    64.                 cout << "Max: " << varIntPow.max() << endl;
    65.                 cout << "Min: " << varIntPow.min() << endl;
    66.         }
    67.  
    68.         system("pause");
    69. }
  • bitset
    1. //////////////////////////////////////////////////////////////////////////
    2. //      CopyRight(c) 2009, YOYO, All Rights Reserved.
    3. //      Author: LIN YiQian
    4. //      Created: 2009/08/24
    5. //      Describe: STL bitset 使用DEMO
    6. //////////////////////////////////////////////////////////////////////////
    7. #include <iostream>
    8. #include <bitset>
    9. #include <cassert>
    10.  
    11. using namespace std;
    12.  
    13. void main(void)
    14. {
    15.         bitset<16> bset1;                                   //     16位的bitset,初始化为0
    16.         assert(bset1.none());            // 没有位被设置
    17.  
    18.         bitset<16> bset2(0xFFFF);                     //     16位的bitset,初始化为1
    19.         assert(16 == bset2.count());        //  被设置的位数为16个
    20.  
    21.         bitset<32> bset3(0xFFFF);                     //     32位的bitset,低16位初始化为1,高16位仍为0
    22.         assert(0xFFFF == bset3.to_ulong());
    23.  
    24.         bitset<8> bset4(0xFFFF);                        //      8位则全部初始化为1
    25.         assert(8 == bset4.count());
    26.  
    27.         bitset<8> bset5(string("111110000011"));                //      8位则使用字符串的前8个数字进行初始化
    28.         assert(0xF8 == bset5.to_ulong());                            //     0-7位为:0001 1111(倒过来)
    29.  
    30.         bitset<8> bset6(string("111110000011"), 4, 3)//        使用字符串第5个数字(下标4)开始3个数字("100")进行初始化
    31.         assert(0x04 == bset6.to_ulong());                            //     0-7位为:0010 0000(倒过来)
    32.  
    33.         assert(bitset<8>() == (bset5 & bset6));      //       0001 1111 & 0010 0000 = 0000 0000
    34.         assert(bitset<8>(string("11111100")) == (bset5 | bset6));       //     0001 1111 | 0010 0000 = 0011 1111
    35.         assert(bitset<8>(string("11111100")) == (bset5 ^ bset6));       //     0001 1111 ^ 0010 0000 = 0011 1111
    36.  
    37.         bset5[1] = 1;         // bset5变成:0101 1111
    38.         assert(0xFA == bset5.to_ulong());
    39.  
    40.         bool b = ~bset5[1];          //   b = 0, bset5[1]不变
    41.         assert(!b);
    42.         assert(0xFA == bset5.to_ulong());
    43.  
    44.         bset5[1].flip();                //      bset5[1]取反
    45.         assert(0xF8 == bset5.to_ulong());
    46.  
    47.         assert(bitset<8>(string("10000000")) == (bset5 << 4))//        bset5左移4位 等于1000 0000
    48.         assert(bitset<8>(string("00001111")) == (bset5 >> 4))//        bset5右移4位 等于0000 1111
    49.  
    50.         bitset<8> bset7;
    51.         cout << "Before Input: " << bset7 << endl;
    52.  
    53.         cout << "Please Input: ";
    54.         cin >> bset7;
    55.         cout << "After Input: " << bset7 << endl;
    56.  
    57.         system("pause");
    58. }
  • priority_queue
    1. //////////////////////////////////////////////////////////////////////////
    2. //      CopyRight(c) 2009, YOYO, All Rights Reserved.
    3. //      Author: LIN YiQian
    4. //      Created: 2009/08/24
    5. //      Describe: STL priority_queue 使用DEMO
    6. //////////////////////////////////////////////////////////////////////////
    7. #include <iostream>
    8. #include <queue>
    9. #include <vector>
    10. #include <Windows.h>
    11.  
    12. using namespace std;
    13.  
    14. //      队列元素
    15. template <class T>
    16. class PriorityMessage
    17. {
    18. public:
    19.         PriorityMessage(int priority, T objMessage): m_nPriority(priority), m_objMessage(objMessage)
    20.         {
    21.                 LARGE_INTEGER liValue;
    22.                 ::QueryPerformanceCounter(&liValue);
    23.                 m_tInQueue = liValue.LowPart;
    24.         }
    25.         ~PriorityMessage()
    26.         {
    27.         }
    28.  
    29. public:
    30.         int m_nPriority;
    31.         unsigned long m_tInQueue;
    32.         T m_objMessage;
    33. };
    34.  
    35. //      比较器
    36. template <class _Ty>
    37. struct PMessageCmp: public binary_function<_Ty, _Ty, bool>
    38. {
    39.         bool operator()(const PriorityMessage<_Ty>& _Left, const PriorityMessage<_Ty>& _Right) const
    40.         {
    41.                 if (_Left.m_nPriority != _Right.m_nPriority)
    42.                 {
    43.                         return _Left.m_nPriority < _Right.m_nPriority;
    44.                 }
    45.                 else
    46.                 {
    47.                         return _Left.m_tInQueue < _Right.m_tInQueue;
    48.                 }
    49.         }
    50. };
    51.  
    52. typedef PriorityMessage<string> STR_PM;
    53. typedef priority_queue<STR_PM, vector<STR_PM>, PMessageCmp<string> > STR_PMQ;
    54.  
    55. void main(void)
    56. {
    57.         STR_PMQ pmqStr;
    58.  
    59.         //      插入优先队列
    60.         pmqStr.push(STR_PM(1, "level11"));
    61.         pmqStr.push(STR_PM(3, "level31"));
    62.         pmqStr.push(STR_PM(1, "level12"));
    63.         pmqStr.push(STR_PM(2, "level21"));
    64.         pmqStr.push(STR_PM(3, "level32"));
    65.         pmqStr.push(STR_PM(4, "level41"));
    66.         pmqStr.push(STR_PM(5, "level51"));
    67.         pmqStr.push(STR_PM(2, "level23"));
    68.         pmqStr.push(STR_PM(4, "level42"));
    69.         pmqStr.push(STR_PM(1, "level13"));
    70.         pmqStr.push(STR_PM(2, "level24"));
    71.         pmqStr.push(STR_PM(4, "level43"));
    72.         pmqStr.push(STR_PM(4, "level44"));
    73.  
    74.         //      打印优先队列
    75.         while (!pmqStr.empty())
    76.         {
    77.                 STR_PM& pmStr = pmqStr.top();   // 获取头部元素
    78.                 cout << "[LEVEL: " << pmStr.m_nPriority << "] "
    79.                         << "[QUEUE TIME: " << pmStr.m_tInQueue << "] "
    80.                          << "[MESSAGE: " << pmStr.m_objMessage.c_str() << "] " << endl;
    81.                 pmqStr.pop();
    82.         }
    83.  
    84.         system("pause");
    85. }
  • 无匹配

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter