fork download
  1. #include <iostream>
  2. #include<map>
  3. #include<iterator>
  4. #include<utility>
  5. using namespace std;
  6.  
  7. void printMap(map<string,int> &fm){
  8. cout << "size = " << fm.size() << endl;
  9. //map<string,int>::iterator it;
  10. for(auto it = fm.begin();it != fm.end();++it){
  11. //cout << (*it).first << "-" << (*it).second << endl;
  12. cout << it->first << "-" << it->second << endl;
  13. }
  14. cout << endl;
  15. }
  16. int main() {
  17. //basic initialization.
  18. map<string,int> m1;
  19. m1["date"] = 22; //O(log(n));n = current size.
  20. m1["month"] = 05;
  21. m1["year"] = 2004;
  22. printMap(m1);
  23.  
  24. //initializing a map using pair,using insert():
  25. map<string,int> m2;
  26. m2.insert(make_pair("date",22));
  27. m2.insert({"month",05});
  28. printMap(m2);
  29. m2["day"]; //inserts an empty string to "day" key.
  30. printMap(m2);
  31.  
  32. //find(key):returns the iterator for a specific key in the map.
  33. //if the key doesnt exist, it will return the iterator for end().
  34. auto it2 = m2.find("date");
  35. cout << (*it2).second << endl;
  36. auto it3 = m2.find("month");
  37. if(it3 == m2.end())
  38. cout << "no such key\n";
  39. else
  40. cout << it3->second << endl; //-> works only for iterators.
  41.  
  42. //erase():erases a key. Can take the key or associated iterator as input.
  43. m2.erase("month"); //O(log(n)
  44. printMap(m2);
  45. //passing as iterator.
  46. auto it4 = m2.find("day");
  47. m2.erase(it4);
  48. printMap(m2);
  49. //passing an iterator that doesnt have a key.
  50. auto it5 = m2.find("arif"); //points to m2.end()
  51.  
  52. if (it5 != m2.end())
  53. m2.erase(it5);//a non-existent iterator cannot be passed to erase().
  54. printMap(m2);
  55.  
  56. //clear(): Clears the whole container.
  57. m2.clear();
  58. printMap(m2);
  59. return 0;
  60. }
  61. /*
  62. Here’s the updated summary of our chat about maps, including the handling of value overwriting with `insert()` and other methods:
  63.  
  64. ### Summary of Our Chat About Maps
  65.  
  66. 1. **Normal Maps with String Keys**:
  67.   - Keys in maps (dictionaries) can be strings, allowing for dynamic access and organization of values.
  68.   - Accessing values is done using string keys, which can be dynamically assigned.
  69.  
  70.   ```cpp
  71.   #include <iostream>
  72.   #include <map>
  73.   #include <string>
  74.  
  75.   int main() {
  76.   std::map<std::string, int> myMap;
  77.   myMap["apple"] = 1;
  78.   myMap["banana"] = 2;
  79.   myMap["cherry"] = 3;
  80.  
  81.   for (const auto& pair : myMap) {
  82.   std::cout << pair.first << " " << pair.second << std::endl;
  83.   }
  84.   return 0;
  85.   }
  86.   ```
  87.  
  88.   **Output**:
  89.   ```
  90.   apple 1
  91.   banana 2
  92.   cherry 3
  93.   ```
  94.  
  95. 2. **Sorting Behavior**:
  96.   - In **`std::map`**, keys are automatically sorted in ascending lexicographical order.
  97.   - In **`std::unordered_map`**, there is no guaranteed order for the keys, and their order can appear random.
  98.  
  99.   ```cpp
  100.   #include <iostream>
  101.   #include <unordered_map>
  102.   #include <string>
  103.  
  104.   int main() {
  105.   std::unordered_map<std::string, int> myMap;
  106.   myMap["banana"] = 2;
  107.   myMap["apple"] = 1;
  108.   myMap["cherry"] = 3;
  109.  
  110.   for (const auto& pair : myMap) {
  111.   std::cout << pair.first << " " << pair.second << std::endl;
  112.   }
  113.   return 0;
  114.   }
  115.   ```
  116.  
  117.   **Output** (order may vary):
  118.   ```
  119.   banana 2
  120.   cherry 3
  121.   apple 1
  122.   ```
  123.  
  124. 3. **Lexicographical Sorting**:
  125.   - Lexicographical order sorts keys based on alphabetical character comparison, similar to how words are arranged in a dictionary.
  126.   - The sorting process considers only the keys, independent of their associated values.
  127.  
  128. 4. **Sorting by Values**:
  129.   - To sort by values instead of keys, extract key-value pairs, sort them based on values, and print them.
  130.  
  131.   ```cpp
  132.   #include <iostream>
  133.   #include <unordered_map>
  134.   #include <vector>
  135.   #include <algorithm>
  136.   #include <string>
  137.  
  138.   int main() {
  139.   std::unordered_map<std::string, int> myMap;
  140.   myMap["banana"] = 2;
  141.   myMap["apple"] = 1;
  142.   myMap["cherry"] = 3;
  143.  
  144.   // Extract key-value pairs into a vector
  145.   std::vector<std::pair<std::string, int>> vec(myMap.begin(), myMap.end());
  146.  
  147.   // Sort by value
  148.   std::sort(vec.begin(), vec.end(), [](const auto& a, const auto& b) {
  149.   return a.second < b.second; // Compare based on the second element (value)
  150.   });
  151.  
  152.   // Print sorted key-value pairs
  153.   for (const auto& pair : vec) {
  154.   std::cout << pair.first << " " << pair.second << std::endl;
  155.   }
  156.  
  157.   return 0;
  158.   }
  159.   ```
  160.  
  161.   **Output of Sorting by Values**:
  162.   ```
  163.   apple 1
  164.   banana 2
  165.   cherry 3
  166.   ```
  167.  
  168. 5. **Handling Value Overwrites**:
  169.   - If you want to insert a value in a map but might overwrite an existing value, you can use different methods:
  170.  
  171.   **Using `operator[]`**:
  172.   ```cpp
  173.   myMap["banana"] = 5; // Overwriting the value of "banana"
  174.   ```
  175.   **insert() doesnt allow overwriting.
  176.   **Using `insert_or_assign()` (C++17 and later)**:
  177.   ```cpp
  178.   myMap.insert_or_assign("banana", 5); // Overwriting or inserting
  179.   ```
  180.  
  181.   **Using `find()` to Check Before Inserting**:
  182.   ```cpp
  183.   auto it = myMap.find(keyToUpdate);
  184.   if (it != myMap.end()) {
  185.   it->second = newValue; // Overwrite
  186.   } else {
  187.   myMap[keyToUpdate] = newValue; // Insert
  188.   }
  189.   ```
  190.  
  191. 6. **Real-World Analogy**:
  192.   - The concept of sorting keys in maps is analogous to looking up words in a dictionary, where words are arranged alphabetically, making them easy to find.
  193.  
  194. If you have any more questions or need further clarification, feel free to ask!
  195. */
  196.  
Success #stdin #stdout 0.01s 5240KB
stdin
Standard input is empty
stdout
size = 3
date-22
month-5
year-2004

size = 2
date-22
month-5

size = 3
date-22
day-0
month-5

22
5
size = 2
date-22
day-0

size = 1
date-22

size = 1
date-22

size = 0