C++11/14新特性 更性感

本文介绍了C++11的重要更新,包括四种转型操作、lambda表达式、初始化列表、foreach遍历、强枚举类型、静态断言、构造函数相互调用、禁止重写final、统一初始化风格、decltype类型指示器、原生字符串表示、仿函数functor等,展示了C++11在代码简洁性和安全性上的改进。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天学习并了解了很多有关C++11的新特性

C++中四种转型操作:

1)static_cast 和C的旧式转换相同

2) const_cast 它一般用来改变表达式中的常量性和变易性

3) dynaic_cast用来运行继承体系中 :安全的向下转型或跨系转型的动作:

4) reinterpret_cast 最常用的用途是函数指针类型的转哈un

 

lambda表达式

格式:

[= / & ,list](params){

..

}

[] 表示不使用外部环境变量

[=] 表示使用外部环境中所有的变量

[&] 表示使用外部环境中所有的变量的引用

[=,&..] 除了个别元素采用引用的方式

lambda表达式相当于一个inline的内联函数

 

lambda表达式使用详情

 

#include <iostream>

using namespace std;

 

int main(){

int a = 1,b = 2,c = 3;
auto fun = [ = ](){

return a+b+c;

}

cout << fun() << endl;

}

 

输出结果为6

它相当于一个小规模的内联函数 可以嵌入到表达式当中 方便灵活

 

初始化列表

std::vector<int> vec = {1,2,3,4,5};

std::list<int> l = {1,2,3,4};

std::queue<int> = {1,1,2,3};

 

遍历foreach:

对于一个vector<int>的遍历

vector<int> vec;

for(auto it = vec.begin() ; it != vec.end() ; ++it)

 

使用新的遍历方式

for ( value : vec)

cout << value <<endl;

这里代码简洁了很多 但每一次都会对 vector的值进行复制 如果value是一个类我们更希望采用引用 的方式

for (auto& value : vec )

cout << value << endl;

 

C++11强枚举类型

enum class EnumName{

};

枚举类型范围不在是全局的而是指定的范围内的

写法更严格

 

static_assert 静态断言

static_assert (sizeof(int) == 4);

可能在编译时作出判断

 

构造函数的相互调用

有点类似于委托构造函数

<span style="color:#000000"><code>class A {</code>
<code>public:</code>
<code>    </code><code>A(int x, int y, const std::string& name) : x(x), y(y), name(name) {</code>
<code>        </code><code>if (x < 0 || y < 0)</code>
<code>            </code><code>throw std::runtime_error("invalid coordination");</code>
<code>        </code><code>if (name.empty())</code>
<code>            </code><code>throw std::runtime_error("empty name");</code>
<code>        </code><code>// other stuff</code>
<code>    </code><code>}</code>

<code>    </code><code>A(int x, int y) : A(x, y, "A")</code>
<code>    </code><code>{}</code>

<code>    </code><code>A() : A(0, 0)</code>
<code>    </code><code>{}</code>

<code>private:</code>
<code>    </code><code>int x;</code>
<code>    </code><code>int y;</code>
<code>    </code><code>std::string name;</code>
<code>};</code></span>

你可以在初始化参数列表中调用同一个构造函数来节省代码冗余度

 

禁止重写final

进制虚函数继续被重写

class A{
public:

virtual void print() final {}

};

 

显式声明禁止重写override

 

 

C++11/14新特性——更简洁 (补)

新的字符串表示方式——原生字符串

#include <iostream>

using namespace std;

 

int main(){

string path = "//usr//local//sdchaind//wallet_propose";

 

//更简洁的表示

string path2 = R"(/usr/local/sdchaind/wallet_propose)";

 

cout << path << endl;

cout << path2 << endl;

}

 

统一的初始化风格

在c++11中程序允许用花括号的形式来调用构造函数 这样多种构造方式可以方便的统一起来了

int a = {5};

int aa[] = {1,5,3,5};

complex z = {4,-3}; //原本的表示法应该是 complex z(4,-3);

 

decltype 类型指示器

1)获取表达式类型

auto 类型作为占位符来修饰变量,必须初始化,编译器通过初始化来确定auto所代表的类型,即必须定义变量,如果我们仅仅希望得到类型又不初始化 那要如何做到呢?decltype(expr) expr代表被推倒的表达式,被decltype所声明过的变量可以初始化也可以不初始化。

#include <iostream>

using namespace std;

int main(){

int a=10;

double b = 20.2;

 

decltype(a+b) c;

c = a+ b;

cout << c <<endl;

return 0;

}

decltype(expr) 推导出来的类型和expr完全一致 同auto一样 在编译期间完成 不会真正的计算表达式的值

 

decltype typedef 的联合运用

typedef decltype(vec.begin()) iter;

 

for(iter it = vec.begin() ; it != vec.end() ; ++it)

autodecltype 的联合运用

#include <iostream>

using namespace std;

 

template<typename R, typename T, typename U>

R add(T a,T b){

return a+b;

}

 

template<typename R,typename T,typename U>

auto add2(T a,U b)->decltype(a+b){

return a+b;

}

 

int main(){

int a = 1;

double b = 1.1;

 

auto ret = add<decltype(a+b),int ,float>(a,b);

cout << ret <<endl;

 

}

 

仿函数 functor

1)重载了operator() 的类的对象 在使用中 语法类型于函数 所以称作仿函数 这种语法优于常见的函数回调

class Add{

public:

int operator()(int x,int y){

return x+y;

}

};

 

int main(){
int a = 1,b = 2;

Add add;

cout << add(a,b) << endl;

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值