首页
社区
课程
招聘
[分享]《探索现代化C++》泛读笔记摘要17
发表于: 2022-9-27 22:20 6112

[分享]《探索现代化C++》泛读笔记摘要17

2022-9-27 22:20
6112

《探索现代化C++》泛读笔记摘要17

Chapter 4 C++库

元编程

元编程通过模板推演和编译器数值计算,来保证代码生成的安全性和提升程序运行性能。

Limits库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <limits>
 
using namespace std;
 
template<typename T>
inline void test(const T& x)
{
    cout<<"x = "<< x <<" (";
    auto oldp = cout.precision(numeric_limits<T>::digits10+1);
    cout<<x<<")"<<endl;
    cout.precision(oldp);
}
 
int main()
{
    test(1.f/3.f);
    test(1./3.0);
    test(1./3.0l);
}

C++11 type traits

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct simple_point
{
#ifdef __cplusplus
    simple_point(double x, double y): x(x),y(y) {}
    simple_point() = default;
    simple_point(initializer_list<double> il)
    {
        auto it = begin(il);
        x = *it;
        y = *next(it);
    }
#endif
    double x,y;
};
 
simple_point p1{3.0, 7.1}, p2;
static_assert(std::is_trivially_copyable
<simple_point>::value,
"simple_point is not as simple as you think "
"and cannot be memcpyd!");
std::memcpy (&p2, &p1, sizeof(p1));

Utilities

C++17 optional

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>
#include <optional>
 
using namespace std;
 
optional <double> square_root(double x)
{
    if (x < 0.0)
        return nullopt;
    else
        return sqrt(x);
}
 
int main()
{
    vector <double> v = { 4.0, -4.0, -0.09, 0.25 };
    for (double d : v)
        if (auto s = square_root(d); s)
            cout << "Square root of " << d << " is " << * s << '\n';
        else
            cout << d << " has no square root.\n";
}

C++11 tuple

1
2
3
4
5
6
7
tuple<matrix, vector> lu(const matrix& A)
{
    matrix LU(A);
    vector p(n);
    // ... some computations
    return tuple<matrix, vector>(LU, p);
}

通过类型参数推导简化

1
2
3
4
5
6
7
tuple<matrix, vector> lu(const matrix& A)
{
    matrix LU(A);
    vector p(n);
    // ... some computations
    return make_tuple(LU,p);
}

C++17 进一步简化

1
2
3
4
5
auto lu(const matrix& A)
{
    // ...
    return tuple{LU,p};
}

make_tuple

1
auto t= make_tuple (LU, p, 7.3, 9, LU*p, 2.0+9.0i);

C++17 能直接从构造函数推导

1
tuple t{LU, p, 7.3, 9, LU*p, 2.0+9.0i};

C++17 甚至都不用使用tuple进行存储

1
auto [LU,p] = lu(A);

C++17 variant

1
2
3
4
5
6
7
8
9
10
11
#include <variant>
 
using my_variant = variant<int,double,string>;
my_variant var;
 
var = 3;
var = 4.2;
var = "Text";
 
string s = get<2>(var);
s = get<string>(var);

C++17 any

1
2
3
4
5
6
7
8
auto find_ints(const vector<any>& v)
{
    vector<int> vi;
    for (const any& a : v)
        if (a.type() == typeid(int))
            vi.push_back( any_cast<int>(a) );
    return vi;
}

C++17 string_view

1
2
3
4
void print_some_string(string_view sv)
{
    cout << sv << '\n';
}

C++11 function

支持的类型有函数,仿函数,lambdas。

1
2
3
4
5
6
7
8
9
10
11
double add(double x, double y)
{
    return x + y;
}
 
int main()
{
    using bin_fun = function<double(double, double)>;
    bin_fun f = &add;
    cout << "f(6, 3) = " << f(6, 3) << endl;
}

时间相关的库chrono

1
using namespace std::chrono; // 见到这个标志查文档吧

Concurrency

C++11就有介绍

1
2
3
4
5
6
threads
future
promise
async
atomic
mutex

C++14

1
shared_timed_mutex

C++17

1
shared_mutex

C++20 协程

1
co_await、co_yield、co_return 三个语法特征

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (3)
雪    币: 8764
活跃值: (5240)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
2
std::memcpy 是要加哪个头文件吗?
我这里include <string> 只能memcpy不能 std::memcpy
2022-9-29 10:25
0
雪    币: 1907
活跃值: (6004)
能力值: ( LV7,RANK:116 )
在线值:
发帖
回帖
粉丝
3
sunsjw std::memcpy 是要加哪个头文件吗? 我这里include 只能memcpy不能 std::memcpy
vs2019 只需要包含<iostream>
2022-9-29 13:38
0
雪    币: 389
活跃值: (565)
能力值: ( LV7,RANK:110 )
在线值:
发帖
回帖
粉丝
4
我爱水子
2022-9-29 16:43
0
游客
登录 | 注册 方可回帖
返回
//