mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 12:44:40 +03:00
37
code/4/4.2.unordered.map.cpp
Normal file
37
code/4/4.2.unordered.map.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// 4.2.unordered.map.cpp
|
||||
// chapter 04 containers
|
||||
// modern c++ tutorial
|
||||
//
|
||||
// created by changkun at changkun.de
|
||||
// https://github.com/changkun/modern-cpp-tutorial
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <map>
|
||||
|
||||
int main() {
|
||||
// initialized in same order
|
||||
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"}
|
||||
};
|
||||
|
||||
// iterates in the same way
|
||||
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