使用C++11中的遍历工具

处理日期和时间的chromo库

duration表示一段时间间隔,表示几秒、几分钟等。

typedef duration<Rep, ratio<60,1>> minutes; typedef duration<Rep, ratio<1,1>> seconeds; typedef duration<Rep, ratio<1,1000>> milliseconds;

Rep表示时间数值,ratio表示时钟周期。ratio<60,1>表示一个时钟周期是60秒。
通过定义这些常用的时间间隔,我们可以方便的直接使用:

this_thread::sleep_for(chrono::seconds(3)); //休眠3秒钟

时间间隔之间的计算

    chrono::minutes t1(10);
    chrono::seconds t2(60);
    chrono::seconds t3 = t1 - t2;
    cout << t3.count() << " seconds" << endl;

输出结果为540秒。

数值类型和字符串的相互转化

    double f = 1.53;
    string fstring = to_string(f);
    cout << fstring << endl;
atoi: 将字符串转化为int
atol: long
atoll: long long
atof: float

示例代码:

    const char* str1 = "10";
    cout << atoi(str1) << endl;

    const char* str2 = "3.15144";
    cout << atoi(str2) << endl;
    cout << atof(str2) << endl;

    const char* str3 = "31222 with words";
    cout << atoi(str3) << endl;
    cout << atof(str3) << endl;

    const char* str4 = "begin 31222 with words";
    cout << atoi(str4) << endl;
    cout << atof(str4) << endl;

输出:

10
3
3.15144
31222
31222
0
0
请按任意键继续. . .

你可能感兴趣的:(使用C++11中的遍历工具)