前言 :
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 代碼 :
執行結果 :
第二個範例示範幾個 STL map 常用的 insert() 函數與其重載版本, 並使用 string 當 key 與 int 作為 data 存入 map :
- exampleMap2.cpp 代碼 :
執行結果 :
第三個範例將介紹如何對自製與使用自定義的比較函數 cmp_str 來對 char* 型態的 key 作 sorting :
- exampleMap3.cpp 代碼 :
執行結果 :
補充說明 :
* C++ : Reference : STL Containers : map
The class template std::map
範例代碼 :
第一個範例, 我們示範簡單的 STL map 操作並使用 int 當key與string 的 data, 包含插入元素與使用 iterator 與矩陣方法取出元素.
- exampleMap.cpp 代碼 :
- #include
- #include
- #include
- using namespace std;
- int main(){
- map<int, string> Employees;
- // 1) Assignment using array index notation
- Employees[1234] = "Mike C.";
- Employees[1235] = "Charlie M.";
- Employees[1236] = "David D.";
- Employees[1237] = "John A.";
- Employees[1238] = "Peter Q.";
- cout << "Employees[1234]= " << Employees[1234] << endl << endl;
- cout << "Map size: " << Employees.size() << endl;
- for(map<int, string>::iterator i=Employees.begin(); i!=Employees.end(); i++) {
- cout << (*i).first << ": " << (*i).second << endl;
- }
- }
第二個範例示範幾個 STL map 常用的 insert() 函數與其重載版本, 並使用 string 當 key 與 int 作為 data 存入 map :
- exampleMap2.cpp 代碼 :
- #include
- #include
- #include
- using namespace std;
- int main(){
- map
int> Employees; - // Examples of assigning Map container contents
- // 1) Assignment using array index notation
- Employees["Mike C."] = 1234;
- Employees["Charlie M."] = 1235;
- // 2) Assignment using member function insert() and STL pair
- Employees.insert(std::pair
int>("David D.", 1236)); - // 3) Assignment using member function insert() and "value_type()"
- Employees.insert(map
int>::value_type("John A.", 1237)); - // 4) Assignment using member function insert() and "make_pair()"
- Employees.insert(std::make_pair("Peter Q.",1238));
- cout << "Map size: " << Employees.size() << endl;
- for(map
int>::iterator i=Employees.begin(); i!=Employees.end(); i++) { - cout << (*i).first << ": " << (*i).second << endl;
- }
- }
第三個範例將介紹如何對自製與使用自定義的比較函數 cmp_str 來對 char* 型態的 key 作 sorting :
- exampleMap3.cpp 代碼 :
- #include
- #include
- #include
- using namespace std;
- struct cmp_str
- {
- bool operator()(char const *a, char const *b)
- {
- return strcmp(a, b) < 0;
- }
- };
- int main(){
- map<char*, int, cmp_str> Employees;
- // Examples of assigning Map container contents
- // 1) Assignment using array index notation
- Employees["Mike C."] = 5234;
- Employees["Charlie M."] = 3374;
- // 2) Assignment using member function insert() and STL pair
- Employees.insert(std::pair<char*, int>("David D.", 1923));
- // 3) Assignment using member function insert() and "value_type()"
- Employees.insert(map<char*,int>::value_type("John A.",7582));
- // 4) Assignment using member function insert() and "make_pair()"
- Employees.insert(std::make_pair((char *)"Peter Q.",5238));
- cout << "Map size:" << Employees.size() << endl;
- for(map<char*, int, cmp_str>::iterator i=Employees.begin(); i!=Employees.end(); i++) {
- cout << (*i).first << ": " << (*i).second << endl;
- }
- }
補充說明 :
* C++ : Reference : STL Containers : map
This message was edited 5 times. Last update was at 06/06/2010 10:45:42