资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C++培训C++11新特性:杂项-创新互联

C++培训之前小编给大家总结了一些C++的新特性,这一篇文章是介绍的C++11新特性之杂项,在后面的文章中,小编还会给大家总结一些C++11新特性的知识出来!

我们提供的服务有:网站建设、成都网站建设、微信公众号开发、网站优化、网站认证、通榆ssl等。为成百上千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的通榆网站制作公司

类型别名声明

类似typedef,新标准中可以使用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语句在之前的章节中已经见识过了:

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不能用于动态分配内存的数组,因为动态分配的内存中没有begin、end方法可供调用。

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";

新的除法舍入规则

新标准中,重新统一了除法的舍入规则。主要需要留意两数符号不同时的规则:

(-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";

使用字符串作为文件名

在新标准中,可以直接使用string作为文件名进行文件流处理,而不必要求C风格字符数组。

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"<

字符串和数值的转换

新标准中,添加了多个函数用于string和数值之间的转换

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 函数

新标准中,提供了功能更强大的参数绑定函数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个参数,等待后续真正调用时由用户传入。

显式类型转换

新标准中,可以指定一个类型转换为显式(explicit)的。指定为显式的类型转换,不能再进行隐式转换。

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类型之间的转换是显式的,在if语句中,也仍然可以执行隐式类型转换。

内联命名空间

新标准中引入了内联命名空间。内联命名空间内的名字可以不加命名空间名字前缀,直接在外层命名空间中使用。这为我们设置一个默认的命名空间提供了方便。

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"<

只需在第一次声明内联命名空间时加上inline关键字,之后就可以省略了。

限定作用域的 enum

新标准中,可以通过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就没有这个限制。

指定枚举的数据类型

新标准中,可以指定枚举的数据类型为除了int之外的其他整数类型。

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";

输出

整个测试程序的输出结果如下:

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.

总结

类似typedef,新标准中可以使用using为类型声明一个别名(alias)。

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

range for不能用于动态分配内存的数组。

新标准中,重新统一了除法的舍入规则。主要需要留意两数符号不同时的规则:

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

x % (-y) = x % y

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

可以采用尾置返回类型,提高代码的可读性。

可以直接使用string作为文件名进行文件流处理,而不必要求C风格字符数组。

添加了多个函数用于string和数值之间的转换。

提供了功能更强大的参数绑定函数bind,用于替换原有的bind1st和bind2nd。

可以指定一个类型转换为显式(explicit)的。指定为显式的类型转换,不能再进行隐式转换。有一个例外,即,即使指定一个类型和bool类型之间的转换是显式的,在if语句中,也仍然可以执行隐式类型转换。

引入了内联命名空间。内联命名空间内的名字可以不加命名空间名字前缀,直接在外层命名空间中使用。

可以通过enum class foo这样的形式定义一个限定作用域的枚举,其中的枚举变量在作用域之外不可见。

可以指定枚举的数据类型为除了int之外的其他整数类型。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


名称栏目:C++培训C++11新特性:杂项-创新互联
分享路径:http://cdkjz.cn/article/dissps.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220