mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-18 05:04:39 +03:00
prepare for c++17
This commit is contained in:
36
code/4/4.2.cpp
Normal file
36
code/4/4.2.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// 4.2.cpp
|
||||
// c++1x tutorial
|
||||
//
|
||||
// created by changkun at shiyanlou.com
|
||||
//
|
||||
// 无序容器
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <map>
|
||||
|
||||
int main() {
|
||||
// 两组结构按同样的顺序初始化
|
||||
std::unordered_map<int, std::string> u = {
|
||||
{1, "1"},
|
||||
{3, "3"},
|
||||
{2, "2"}
|
||||
};
|
||||
std::map<int, std::string> v = {
|
||||
{1, "1"},
|
||||
{3, "3"},
|
||||
{2, "2"}
|
||||
};
|
||||
|
||||
// 分别对两组结构进行遍历
|
||||
std::cout << "std::unordered_map" << std::endl;
|
||||
for( const auto & n : u)
|
||||
std::cout << "Key:[" << n.first << "] Value:[" << n.second << "]\n";
|
||||
|
||||
std::cout << std::endl;
|
||||
std::cout << "std::map" << std::endl;
|
||||
for( const auto & n : v)
|
||||
std::cout << "Key:[" << n.first << "] Value:[" << n.second << "]\n";
|
||||
}
|
||||
Reference in New Issue
Block a user