首页 > 社交 > 科普中国

C++

常驻编辑 科普中国 2022-07-18 标准   特性   定义   概念   类型
gV0拜客生活常识网

#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)的类模板。gV0拜客生活常识网

std::tuple是一个有多个数据成员,可以由get<>()访问的类模板。gV0拜客生活常识网

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:gV0拜客生活常识网

#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:gV0拜客生活常识网

#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.gV0拜客生活常识网

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:gV0拜客生活常识网

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.gV0拜客生活常识网

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:gV0拜客生活常识网

g++ -std=c++11 -Wall -pthread source.cpp

With clang it will be:gV0拜客生活常识网

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.gV0拜客生活常识网

For example, we can execute multiple functions concurrently using threads

相关阅读:

  • 巩俐谈北影节评奖标准,陈坤请教好友周迅,张颂文笑言可能
  • pm2.5标准范围多少正常
  • 安徽最低工资标准2021上调了吗
  • 正常血压标准范围是多少(血压高到160危险吗)
  • 血红蛋白正常值是多少(3到6岁血红蛋白标准)
  • 视力正常标准是多少(视力最好是5.0还是5.2)
  • 甲醛标准范围多少正常(甲醛0.11宝宝能住吗)
  • 一海里等于多少米(标准海里长度为多少米)
  • 酒驾标准是多少毫升(酒驾吹气低于20怎么处理)
  • 国道限速多少(国道限速标准)
    • 网站地图 |
    • 声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。文章内容仅供参考,不做权威认证,如若验证其真实性,请咨询相关权威专业人士。