程式扎記: [C++ 範例代碼] 使用 STL 的 map 操作範例

標籤

2010年8月1日 星期日

[C++ 範例代碼] 使用 STL 的 map 操作範例


前言 :
The class template std::map is a standard C++ container. It is a sorted associative array of unique keys and associated data.[1] The types of key and data may differ, and the elements of the map are internally sorted from lowest to highest key value. Since each key value is unique, if an object is inserted with an already existing key, the object already present in the map does not change.[2] A variation on the map, called themultimap, allows duplicate keys. (More)

範例代碼 :
第一個範例, 我們示範簡單的 STL map 操作並使用 int 當key與string 的 data, 包含插入元素與使用 iterator 與矩陣方法取出元素.
- exampleMap.cpp 代碼 :
  1. #include   
  2. #include   
  3. #include   
  4.   
  5. using namespace std;  
  6.   
  7. int main(){  
  8.         map<int, string> Employees;  
  9.   
  10.         // 1) Assignment using array index notation  
  11.         Employees[1234] = "Mike C.";  
  12.         Employees[1235] = "Charlie M.";  
  13.         Employees[1236] = "David D.";  
  14.         Employees[1237] = "John A.";  
  15.         Employees[1238] = "Peter Q.";  
  16.   
  17.         cout << "Employees[1234]= " << Employees[1234] << endl << endl;  
  18.         cout << "Map size: " << Employees.size() << endl;  
  19.         for(map<int, string>::iterator i=Employees.begin(); i!=Employees.end(); i++) {  
  20.                 cout << (*i).first << ": " << (*i).second << endl;  
  21.         }  
  22. }  
執行結果 :
linux-tl0r:~/gccprac/Test/STL # g++ exampleMap.cpp <編譯代碼>
linux-tl0r:~/gccprac/Test/STL # ./a.out <執行程式>
Employees[1234]= Mike C.

Map size: 5
1234: Mike C.
1235: Charlie M.
1236: David D.
1237: John A.
1238: Peter Q.

第二個範例示範幾個 STL map 常用的 insert() 函數與其重載版本, 並使用 string 當 key 與 int 作為 data 存入 map :
- exampleMap2.cpp 代碼 :
  1. #include   
  2. #include   
  3. #include   
  4.   
  5. using namespace std;  
  6.   
  7. int main(){  
  8.         mapint> Employees;  
  9.         // Examples of assigning Map container contents  
  10.   
  11.         // 1) Assignment using array index notation  
  12.         Employees["Mike C."] = 1234;  
  13.         Employees["Charlie M."] = 1235;  
  14.   
  15.         // 2) Assignment using member function insert() and STL pair  
  16.         Employees.insert(std::pairint>("David D."1236));  
  17.   
  18.         // 3) Assignment using member function insert() and "value_type()"  
  19.         Employees.insert(mapint>::value_type("John A."1237));  
  20.   
  21.         // 4) Assignment using member function insert() and "make_pair()"  
  22.         Employees.insert(std::make_pair("Peter Q.",1238));  
  23.   
  24.         cout << "Map size: " << Employees.size() << endl;  
  25.         for(mapint>::iterator i=Employees.begin(); i!=Employees.end(); i++) {  
  26.                 cout << (*i).first << ": " << (*i).second << endl;  
  27.         }  
  28. }  
執行結果 :
linux-tl0r:~/gccprac/Test/STL # g++ exampleMap2.cpp <編譯源代碼>
linux-tl0r:~/gccprac/Test/STL # ./a.out <執行程序>
Map size: 5
Charlie M.: 1235
David D.: 1236
John A.: 1237
Mike C.: 1234
Peter Q.: 1238

第三個範例將介紹如何對自製與使用自定義的比較函數 cmp_str 來對 char* 型態的 key 作 sorting :
- exampleMap3.cpp 代碼 :
  1. #include   
  2. #include   
  3. #include   
  4.   
  5. using namespace std;  
  6.   
  7. struct cmp_str  
  8. {  
  9.         bool operator()(char const *a, char const *b)  
  10.         {  
  11.                 return strcmp(a, b) < 0;  
  12.         }  
  13. };  
  14. int main(){  
  15.         map<char*, int, cmp_str> Employees;  
  16.         // Examples of assigning Map container contents  
  17.   
  18.         // 1) Assignment using array index notation  
  19.         Employees["Mike C."] = 5234;  
  20.         Employees["Charlie M."] = 3374;  
  21.   
  22.         // 2) Assignment using member function insert() and STL pair  
  23.         Employees.insert(std::pair<char*, int>("David D."1923));  
  24.   
  25.         // 3) Assignment using member function insert() and "value_type()"  
  26.         Employees.insert(map<char*,int>::value_type("John A.",7582));  
  27.   
  28.         // 4) Assignment using member function insert() and "make_pair()"  
  29.         Employees.insert(std::make_pair((char *)"Peter Q.",5238));  
  30.   
  31.         cout << "Map size:" << Employees.size() << endl;  
  32.         for(map<char*, int, cmp_str>::iterator i=Employees.begin(); i!=Employees.end(); i++) {  
  33.                 cout << (*i).first << ": " << (*i).second << endl;  
  34.         }  
  35. }  
執行結果 :
linux-tl0r:~/gccprac/Test/STL # g++ exampleMap3.cpp <編譯源代碼>
linux-tl0r:~/gccprac/Test/STL # ./a.out
Map size:5
Charlie M.: 3374
David D.: 1923
John A.: 7582
Mike C.: 5234
Peter Q.: 5238 <輸出的結果, 在key 的部分已經按照A-Z 進行排序>

補充說明 :
C++ : Reference : STL Containers : map
Maps are a kind of associative containers that stores elements formed by the combination of a key value and a mapped value.
In a map, the key value is generally used to uniquely identify the element, while the mapped value is some sort of value associated to this key. Types of key and mapped value may differ. For example, a typical example of a map is a telephone guide where the name is the key and the telephone number is the mapped value.
Internally, the elements in the map are sorted from lower to higher key value following a specific strict weak ordering criterion set on construction.
This message was edited 5 times. Last update was at 06/06/2010 10:45:42

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!