#include
#include
int main()
{
std::unordered_map myunorderedmap = { {'a', 1}, {'b', 2}, {'c', 5} };
myunorderedmap['b'] = 4; // key exists, change the value
myunorderedmap['d'] = 10; // key does not exist, insert the new element
for (auto el : myunorderedmap)
{
std::cout << el.first << ' ' << el.second << '
';
}
}
11.11 std::tuple
std::pair是一个两个成员特殊命名(first, second)的类模板。
std::tuple是一个有多个数据成员,可以由get<>()访问的类模板。
While std::pair can hold only two values, the std::tuple wrapper can hold more than two values. To use tuples, we need to include the header. To access a certain tuple element , we use the std::get(tuple_name) function:
#include
#include
#include
int main()
{
std::tuple mytuple = { 'a', 123, 3.14 };
std::cout << "The first element is: " << std::get<0>(mytuple) << '
';
std::cout << "The second element is: " << std::get<1>(mytuple) << '
';
std::cout << "The third element is: " << std::get<2>(mytuple) << '
';
}
We can create a tuple using the std::make_tuple function:
#include
#include
#include
int main()
{
auto mytuple = std::make_tuple(123, 3.14, "Hello World.");
std::cout << "The first tuple element is: " << std::get<0>(mytuple) << '
';
std::cout << "The second tuple element is: " << std::get<1>(mytuple) << '
';
std::cout << "The third tuple element is: " << std::get<2>(mytuple) << '
';
}
Instead of typing a lengthy tuple type, which is std::tuple, we used the auto specifier to deduce the type name for us.
40.1.12 static_assert
The static_assert directive checks a static (constexpr) condition during compile time. If the condition is false, the directive fails the compilation and displays an error message. Example:
int main()
{
constexpr int x = 123;
static_assert(x == 456, "The constexpr value is not 456.");
}
Here the static_assert checks if the value of x is equal to 456 during compile time. Since it is not, the compilation will fail with a "The constexpr value is not 456." message. We can think of the static_assert as a way of testing our code during compile time. It is also a neat way of testing if the value of a constexpr expression is what we expect it to be.
40.1.13 Introduction to Concurrency
C++11 standard introduces facilities for working with threads. To enable threading, we need to add the -pthreads flag when compiling with g++ and clang on the command line. Example:
g++ -std=c++11 -Wall -pthread source.cpp
With clang it will be:
clang++ -std=c++11 -Wall -pthread source.cpp
When we compile and link our source code program, an executable file is produced. When we start the executable, the program gets loaded into memory and starts running. This running program is called a process. When we start multiple executable files, we can have multiple processes. Each process has its memory, its own address space. Within a process, there can be multiple threads. What are the threads? Threads or threads of execution are an OS mechanism that allows us to execute multiple pieces of code concurrently/simultaneously.
For example, we can execute multiple functions concurrently using threads