Organize the map in Java and C + + basic operations, we welcome you to exchange learning.
Attached: for C + +, choose Map or Unordered_map, you can refer to this discussion. The relatively simple and crude conclusion is that UNORDERED_MAP is faster because UNORDERED_MAP uses a hash table internally, and map uses the structure of a red-black tree internally, so for the lookup operation, the former is O (1), which is actually O (LgN).
Main differences:
- C + + in the main can be used [] to operate (add,update,access); Java is not [], the corresponding action is put (Key,value), get (key)
- For a key lookup, C + + uses Iteator iterator operations, using ContainsKey (key) in Java
|
C++ |
Java |
Describe |
Structure |
Unordered_map<string,int>salary |
map<string, integer> salarymap = new hashmap<string, integer> (); |
|
Increase |
Salary["Tom"] = 10; |
Salarymap. put ("Tom", 10); |
Update: If there is Join: If not present |
Delete |
Salary. Erase ("Tom"); |
Salary. Remove ("Tom"); |
If it does not exist, ignore |
Find |
if (Salary.find ("Tom")! = Salary.end ()) { cout << "Tom:" << salary["Tom"]; } |
if (Salarymap.containskey ("Tom")) { System.out.println ("Tom:" + Salarymap.get ("Tom")); } |
Use Key to query |
Modify |
salary["Tom"] = 20; |
Salarymap.put ("Tom", 20); |
|
Capacity |
Salary.size (); Salary. Empty (); |
Salarymap.size (); Salarymap. IsEmpty (); |
|
Clear |
Salary.clear (); |
Salary.clear () |
|
Reference |
#include <unordered_map> |
Import Java.util.Map; Import Java.util.HashMap; |
|
Summary Map:c++ V.s. Java