Skip to content

Commit 07f4c2e

Browse files
authored
Update 003_stl_std_unordered_map.cpp
1 parent 95a3729 commit 07f4c2e

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

cpp_11/003_stl_std_unordered_map.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,28 @@
1-
1
1+
#include <iostream>
2+
#include <string>
3+
#include <unordered_map>
4+
5+
int main()
6+
{
7+
// 创建三个 string 的 unordered_map (映射到 string )
8+
std::unordered_map<std::string, std::string> u = {
9+
{"RED","#FF0000"},
10+
{"GREEN","#00FF00"},
11+
{"BLUE","#0000FF"}
12+
};
13+
14+
// 迭代并打印 unordered_map 的关键和值
15+
for( const auto& n : u ) {
16+
std::cout << "Key:[" << n.first << "] Value:[" << n.second << "]\n";
17+
}
18+
19+
// 添加新入口到 unordered_map
20+
u["BLACK"] = "#000000";
21+
u["WHITE"] = "#FFFFFF";
22+
23+
// 用关键输出值
24+
std::cout << "The HEX of color RED is:[" << u["RED"] << "]\n";
25+
std::cout << "The HEX of color BLACK is:[" << u["BLACK"] << "]\n";
26+
27+
return 0;
28+
}

0 commit comments

Comments
 (0)