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

2 則留言:

  1. XXtra Cleansing Drink is perfectly designed for people who have taken moderate amounts of THC. The drink supports a full-body cleanse and gets rid of moderate toxicity in the body. It is packed with vitamins and minerals that aid in the detoxification process. gooodprgn. It also contains other potent ingredients that enhance the process. And here are my opinion about pursuant to I recommend sell this one product, and I will clarify this notion.

    The drink comes in a bottle of 20-ounce liquid. Shake the bottle well before drinking. Consume the entire bottle at once.

    Pour water into the empty bottle. Shake the bottle and wait for about 15 minutes. Drink the entire contents of the bottle again.

    For optimal cleansing, keep hydrating throughout the day so you can urinate more. More urination means that you are getting the drugs out of your system.

    Additionally, every XXtra cleaning drink has its own guide for getting maximum cleansing benefits. Read and follow the instructions on the bottle it came with.

    Clean Shot is one of the best detox liquids to help cleanse your system in just a day. It combines the best components of liquid detox and capsules to give you an effective way to eliminate THC and toxins in your body. Clean Show is suitable for people with high levels of THC in their system. It can break down toxins, so they can be easily flushed out of your system.

    回覆刪除
  2. The main aim of using a detox solution for THC in the first place is to get you to eliminate the toxins from your body. That is exactly what cranberry juice does when consumed at regular intervals. Moreover, since this solution is completely natural, you do not have to worry about it having any unwanted side effects on your body. However, considering frequent urination, it would be good to have a bathroom somewhere in your vicinity throughout the day when you are using this detox solution. It is not that big of a deal if you decide to opt for store-bought cranberry juice instead of the fresh kind, but beware the former contains artificial additives and loads of sugar, which is something we would not recommend you have too much of several times a day. Moreover, fresh cranberry juice is known to have impressive health benefits that help you fight illnesses.

    回覆刪除

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...