内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

C++培訓(xùn)C++11新特性:雜項

C++培訓(xùn)之前小編給大家總結(jié)了一些C++的新特性,這一篇文章是介紹的C++11新特性之雜項,在后面的文章中,小編還會給大家總結(jié)一些C++11新特性的知識出來!

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供新都網(wǎng)站建設(shè)、新都做網(wǎng)站、新都網(wǎng)站設(shè)計、新都網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、新都企業(yè)網(wǎng)站模板建站服務(wù),十年新都做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。

類型別名聲明

類似typedef,新標(biāo)準(zhǔn)中可以使用using為類型聲明一個別名(alias)。

std::cout<<"test using alias:\n";

using HT = double;

using NAME = std::string;

HT h = 1.78;

NAME name = "Robert";

std::cout<

range for

range for語句在之前的章節(jié)中已經(jīng)見識過了:

std::cout<<"test range for:\n";

std::string nation = "CHINA";

for(auto c : nation)

std::cout<

for(auto &rc : nation)

rc = tolower(rc);

std::cout<<"lower: "<

std::cout<<"test range for done.\n";

這里需要注意的是,在第二個例子中,range for語句中可以直接使用引用,從而修改被迭代遍歷的對象本身。

另外,range for不能用于動態(tài)分配內(nèi)存的數(shù)組,因為動態(tài)分配的內(nèi)存中沒有begin、end方法可供調(diào)用。

std::cout<<"test range for of dynamic array:\n";

int *darr = new int[5]{0, 1, 2, 3, 4};

// wrong. dynamic array don't has a begin method,

// so we can't use range for here.

//for(auto i : darr)

// std::cout<

for(size_t i = 0; i < 5; i++)

std::cout<

std::cout<<'\n';

std::cout<<"test range for of dynamic array done.\n";

新的除法舍入規(guī)則

新標(biāo)準(zhǔn)中,重新統(tǒng)一了除法的舍入規(guī)則。主要需要留意兩數(shù)符號不同時的規(guī)則:

(-x) / y = x / (-y) = – (x / y)

x % (-y) = x % y

(-x) % y = – (x % y)

std::cout<<"test divide:\n";

std::cout<<"10 / 3 = "<<(10 / 3)<<"\n";

std::cout<<"-10 / 3 = "<<(-10 / 3)<<"\n";

std::cout<<"-10 / -3 = "<<(-10 / (-3))<<"\n";

std::cout<<"10 % 3 = "<<(10 % 3)<<"\n";

std::cout<<"-10 % 3 = "<<(-10 % 3)<<"\n";

std::cout<<"-10 % -3 = "<<(-10 % (-3))<<"\n";

std::cout<<"10 % -3 = "<<(10 % (-3))<<"\n";

std::cout<<"test divide done.\n"<

尾置返回類型

之前,我們也見識過尾置返回類型和decltype的配合使用。有時,采用尾置返回類型,代碼的可讀性更高。

int (*dummy_ret1(int i))[5]

{

static int ret1[5] = {i, i*2, i*3, i*4, i*5};

int(*pret)[5] = &ret1;

return pret;

}

auto dummy_ret2(int i) -> int (*)[5]

{

static int ret2[5] = {i+1, i+2, i+3, i+4, i+5};

int(*pret)[5] = &ret2;

return pret;

}

std::cout<<"test trailing return type:\n";

int (*arr1)[5] = dummy_ret1(1);

std::cout<<(*arr1)[0]<<'\t'<<(*arr1)[4]<

int (*arr2)[5] = dummy_ret2(2);

std::cout<<(*arr2)[0]<<'\t'<<(*arr2)[4]<

std::cout<<"test trailing return type done.\n";

使用字符串作為文件名

在新標(biāo)準(zhǔn)中,可以直接使用string作為文件名進行文件流處理,而不必要求C風(fēng)格字符數(shù)組。

std::cout<<"test string filename:\n";

std::string filename = "regex.cpp";

std::ifstream in(filename);

std::string head_ctx;

in>>head_ctx;

std::cout<

std::cout<<"test string filename done.\n"<

字符串和數(shù)值的轉(zhuǎn)換

新標(biāo)準(zhǔn)中,添加了多個函數(shù)用于string和數(shù)值之間的轉(zhuǎn)換

std::cout<<"test str number cvt:\n";

int age = 15;

double weight = 137.5;

std::string str_age = std::to_string(age);

std::string str_weight = std::to_string(weight);

std::cout<<"str age: "<

bind 函數(shù)

新標(biāo)準(zhǔn)中,提供了功能更強大的參數(shù)綁定函數(shù)bind,用于替換原有的bind1st和bind2nd。

int add_int(int a, int b)

{

return a + b;

}

std::cout<<"test bind:\n";

auto add5 = std::bind(add_int, std::placeholders::_1, 5);

std::cout<<"add5(6): "<

std::cout<<"test bind done.\n"<

std::placeholders::_1表示占位符,指代第1個參數(shù),等待后續(xù)真正調(diào)用時由用戶傳入。

顯式類型轉(zhuǎn)換

新標(biāo)準(zhǔn)中,可以指定一個類型轉(zhuǎn)換為顯式(explicit)的。指定為顯式的類型轉(zhuǎn)換,不能再進行隱式轉(zhuǎn)換。

class OtherType

{

public:

OtherType(int i) : val(i){}

explicit operator int() {return val;}

explicit operator bool() {return val != 0;}

private:

int val;

};

std::cout<<"test explicit type cvt:\n";

OtherType c(10);

//int i = c + 10; // wrong. can't implicit cvt c to int.

int j = static_cast(c) + 10;

std::cout<<"OtherType(10) + 10: "<

if(c)

{

std::cout<<"OtherType can be cvt to bool implicitly in if clause.\n";

}

std::cout<<"test explicit type cvt done.\n"<

這其中有一個例外,即,即使指定一個類型和bool類型之間的轉(zhuǎn)換是顯式的,在if語句中,也仍然可以執(zhí)行隱式類型轉(zhuǎn)換。

內(nèi)聯(lián)命名空間

新標(biāo)準(zhǔn)中引入了內(nèi)聯(lián)命名空間。內(nèi)聯(lián)命名空間內(nèi)的名字可以不加命名空間名字前綴,直接在外層命名空間中使用。這為我們設(shè)置一個默認(rèn)的命名空間提供了方便。

inline namespace InlineSpace

{

int inline_val1 = 1;

}

namespace InlineSpace

{

int inline_val2 = 2;

}

namespace NormalSpace

{

int normal_val3 = 3;

}

std::cout<<"test inline namespace:\n";

std::cout<<"inline vals: "<

//std::cout<<"normal vals: "<

std::cout<<"normal vals: "<

std::cout<<"test inline namespace done.\n"<

只需在第一次聲明內(nèi)聯(lián)命名空間時加上inline關(guān)鍵字,之后就可以省略了。

限定作用域的 enum

新標(biāo)準(zhǔn)中,可以通過enum class foo這樣的形式定義一個限定作用域的枚舉,其中的枚舉變量在作用域之外不可見。

std::cout<<"test scoped enum:\n";

enum class number {one, two, three};

enum nation {CHN, USA, FRA};

enum nation n1 = CHN;

//enum nation n2 = nation::USA;

std::cout<<"unscoped enum: "<

//number num1 = one;

number num2 = number::two;

std::cout<<"scoped enum: "<<(int)num2<<'\n';

std::cout<<"test scoped enum done.\n";

其中,one、two、three就只能在number限定的作用域中可見,而CHN、USA、FRA就沒有這個限制。

指定枚舉的數(shù)據(jù)類型

新標(biāo)準(zhǔn)中,可以指定枚舉的數(shù)據(jù)類型為除了int之外的其他整數(shù)類型。

std::cout<<"test enum type declare:\n";

enum long_enum

{

firstl = LLONG_MAX - 1,

secondl = ULLONG_MAX,

};

enum longlong_enum : long long

{

firstll = LLONG_MAX - 1,

secondll = LLONG_MAX,

//secondll = ULLONG_MAX

};

std::cout<

std::cout<

std::cout<<"test enum type declare done.\n";

輸出

整個測試程序的輸出結(jié)果如下:

test using alias:

Robert's height is 1.78

test using alias done.

test range for:

C H I N A

lower: china

test range for done.

test divide:

10 / 3 = 3

-10 / 3 = -3

-10 / -3 = 3

10 % 3 = 1

-10 % 3 = -1

-10 % -3 = -1

10 % -3 = 1

test divide done.

test trailing return type:

1 5

3 7

test trailing return type done.

test string filename:

#include

test string filename done.

test str number cvt:

str age: 15 str weight: 137.500000

int age: 15 double weight: 137.5

test str number cvt done.

test bind:

add5(6): 11

test bind done.

test range for of dynamic array:

0 1 2 3 4

test range for of dynamic array done.

test explicit type cvt:

OtherType(10) + 10: 20

OtherType can be cvt to bool implicitly in if clause.

test explicit type cvt done.

test inline namespace:

inline vals: 1 2

normal vals: 3

test inline namespace done.

test scoped enum:

unscoped enum: 0

scoped enum: 1

test scoped enum done.

test enum type declare:

9223372036854775806

18446744073709551615

9223372036854775806

9223372036854775807

test enum type declare done.

總結(jié)

類似typedef,新標(biāo)準(zhǔn)中可以使用using為類型聲明一個別名(alias)。

range for語句可以方便的迭代對象,并且支持引用迭代。

range for不能用于動態(tài)分配內(nèi)存的數(shù)組。

新標(biāo)準(zhǔn)中,重新統(tǒng)一了除法的舍入規(guī)則。主要需要留意兩數(shù)符號不同時的規(guī)則:

(-x) / y = x / (-y) = – (x / y)

x % (-y) = x % y

(-x) % y = – (x % y)

可以采用尾置返回類型,提高代碼的可讀性。

可以直接使用string作為文件名進行文件流處理,而不必要求C風(fēng)格字符數(shù)組。

添加了多個函數(shù)用于string和數(shù)值之間的轉(zhuǎn)換。

提供了功能更強大的參數(shù)綁定函數(shù)bind,用于替換原有的bind1st和bind2nd。

可以指定一個類型轉(zhuǎn)換為顯式(explicit)的。指定為顯式的類型轉(zhuǎn)換,不能再進行隱式轉(zhuǎn)換。有一個例外,即,即使指定一個類型和bool類型之間的轉(zhuǎn)換是顯式的,在if語句中,也仍然可以執(zhí)行隱式類型轉(zhuǎn)換。

引入了內(nèi)聯(lián)命名空間。內(nèi)聯(lián)命名空間內(nèi)的名字可以不加命名空間名字前綴,直接在外層命名空間中使用。

可以通過enum class foo這樣的形式定義一個限定作用域的枚舉,其中的枚舉變量在作用域之外不可見。

可以指定枚舉的數(shù)據(jù)類型為除了int之外的其他整數(shù)類型。

網(wǎng)頁題目:C++培訓(xùn)C++11新特性:雜項
文章起源:http://www.rwnh.cn/article46/pgcheg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、外貿(mào)網(wǎng)站建設(shè)、用戶體驗、品牌網(wǎng)站建設(shè)App設(shè)計、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化
天台县| 夹江县| 汉沽区| 泗阳县| 桃源县| 万源市| 高雄县| 奉节县| 搜索| 南郑县| 辉南县| 宜宾市| 台中市| 康平县| 邢台市| 门头沟区| 广安市| 黑水县| 武夷山市| 阿瓦提县| 宜阳县| 松江区| 新河县| 扬中市| 新干县| 融水| 中卫市| 南陵县| 东源县| 诏安县| 舞阳县| 晋江市| 饶平县| 新平| 郴州市| 海原县| 杂多县| 峨边| 墨脱县| 大港区| 延津县|