Add stl hash table
This commit is contained in:
		@@ -64,6 +64,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
- [总结](./stl.md)
 | 
					- [总结](./stl.md)
 | 
				
			||||||
- [排序](./stl_sorting.md)
 | 
					- [排序](./stl_sorting.md)
 | 
				
			||||||
 | 
					- [哈希表](./stl_hash_table.md)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# 技巧
 | 
					# 技巧
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,48 +9,3 @@
 | 
				
			|||||||
它们的区别在于,unordered_map 存储的是 key-value ,而 unordered_set 只存储 key 。
 | 
					它们的区别在于,unordered_map 存储的是 key-value ,而 unordered_set 只存储 key 。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
一般我们直接用 unordered_map 就可以完成所有操作了。
 | 
					一般我们直接用 unordered_map 就可以完成所有操作了。
 | 
				
			||||||
 | 
					 | 
				
			||||||
常见用法:
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
```cpp
 | 
					 | 
				
			||||||
#include <iostream>
 | 
					 | 
				
			||||||
#include <string>
 | 
					 | 
				
			||||||
#include <unordered_map>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
int main(int argc, const char *argv[]) {
 | 
					 | 
				
			||||||
  // key 为 std::string 类型的,value 为 int 类型的
 | 
					 | 
				
			||||||
  std::unordered_map<std::string, int> map;
 | 
					 | 
				
			||||||
  // 插入
 | 
					 | 
				
			||||||
  map["foo"] = 1;
 | 
					 | 
				
			||||||
  map["bar"] = 2;
 | 
					 | 
				
			||||||
  map["non"] = 3;
 | 
					 | 
				
			||||||
  // 访问
 | 
					 | 
				
			||||||
  std::cout << map["foo"] << std::endl;
 | 
					 | 
				
			||||||
  // 删除
 | 
					 | 
				
			||||||
  map.erase("foo");
 | 
					 | 
				
			||||||
  // 判断元素是否存在
 | 
					 | 
				
			||||||
  if (map.count("bar") > 0) {
 | 
					 | 
				
			||||||
    std::cout << "Exist" << std::endl;
 | 
					 | 
				
			||||||
  } else {
 | 
					 | 
				
			||||||
    std::cout << "Not exist" << std::endl;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  // 另一种判断元素是否存在的方法
 | 
					 | 
				
			||||||
  // find() 会返回元素的正向迭代器,如果没找到则返回 end()
 | 
					 | 
				
			||||||
  if (map.find("bar") != map.end()) {
 | 
					 | 
				
			||||||
    std::cout << "Exist" << std::endl;
 | 
					 | 
				
			||||||
  } else {
 | 
					 | 
				
			||||||
    std::cout << "Not exist" << std::endl;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  // 合并两个 unordered_map
 | 
					 | 
				
			||||||
  std::unordered_map<std::string, int> newMap;
 | 
					 | 
				
			||||||
  newMap["microsoft"] = 1;
 | 
					 | 
				
			||||||
  newMap["surface"] = 1;
 | 
					 | 
				
			||||||
  map.insert(newMap.begin(), newMap.end());
 | 
					 | 
				
			||||||
  // 遍历
 | 
					 | 
				
			||||||
  for (std::unordered_map<std::string, int>::iterator iter = map.begin();
 | 
					 | 
				
			||||||
       iter != map.end(); ++iter) {
 | 
					 | 
				
			||||||
    std::cout << iter->first << iter->second << std::endl;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  return 0;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
```
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										44
									
								
								notes/src/stl_hash_table.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								notes/src/stl_hash_table.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,44 @@
 | 
				
			|||||||
 | 
					# 哈希表
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```cpp
 | 
				
			||||||
 | 
					#include <iostream>
 | 
				
			||||||
 | 
					#include <string>
 | 
				
			||||||
 | 
					#include <unordered_map>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int main(int argc, const char *argv[]) {
 | 
				
			||||||
 | 
					  // key 为 std::string 类型的,value 为 int 类型的
 | 
				
			||||||
 | 
					  std::unordered_map<std::string, int> map;
 | 
				
			||||||
 | 
					  // 插入
 | 
				
			||||||
 | 
					  map["foo"] = 1;
 | 
				
			||||||
 | 
					  map["bar"] = 2;
 | 
				
			||||||
 | 
					  map["non"] = 3;
 | 
				
			||||||
 | 
					  // 访问
 | 
				
			||||||
 | 
					  std::cout << map["foo"] << std::endl;
 | 
				
			||||||
 | 
					  // 删除
 | 
				
			||||||
 | 
					  map.erase("foo");
 | 
				
			||||||
 | 
					  // 判断元素是否存在
 | 
				
			||||||
 | 
					  if (map.count("bar") > 0) {
 | 
				
			||||||
 | 
					    std::cout << "Exist" << std::endl;
 | 
				
			||||||
 | 
					  } else {
 | 
				
			||||||
 | 
					    std::cout << "Not exist" << std::endl;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  // 另一种判断元素是否存在的方法
 | 
				
			||||||
 | 
					  // find() 会返回元素的正向迭代器,如果没找到则返回 end()
 | 
				
			||||||
 | 
					  if (map.find("bar") != map.end()) {
 | 
				
			||||||
 | 
					    std::cout << "Exist" << std::endl;
 | 
				
			||||||
 | 
					  } else {
 | 
				
			||||||
 | 
					    std::cout << "Not exist" << std::endl;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  // 合并两个 unordered_map
 | 
				
			||||||
 | 
					  std::unordered_map<std::string, int> newMap;
 | 
				
			||||||
 | 
					  newMap["microsoft"] = 1;
 | 
				
			||||||
 | 
					  newMap["surface"] = 1;
 | 
				
			||||||
 | 
					  map.insert(newMap.begin(), newMap.end());
 | 
				
			||||||
 | 
					  // 遍历
 | 
				
			||||||
 | 
					  for (std::unordered_map<std::string, int>::iterator iter = map.begin();
 | 
				
			||||||
 | 
					       iter != map.end(); ++iter) {
 | 
				
			||||||
 | 
					    std::cout << iter->first << iter->second << std::endl;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
		Reference in New Issue
	
	Block a user