您现在的位置是:网站首页 > 语言基础 > C++11新特性总结

简介本文主要介绍C++11新特性: 1、auto;2、nullptr空指针;3、for容器遍历;4、lambda表达式;5、override、final;6、右值引用;7、move构造函数;8、容器初始化。

1、auto

类型推导

从初始化表达式中推断出变里类型

auto a; // 错误,auto是通过初始化表达式进行类型推导,如果没有初始化表达式,就无法确定a的类型
auto i = 1;
auto d = 1.0;
auto str = "Hello World";
auto ch = 'A';
auto func = less<int>();
vector<int> iv;
auto ite = iv.begin();
auto p = new foo() // 对自定义类型进行类型推导

decltype类型推导

auto使用的前提是:必须要对auto声明的类型进行初始化,否则编译器无法推导出auto的实际类型。 但有时候可能需要根据表达式运行完成之后,对结果的类型进行推导,因为编译期间,代码不会运行,此时auto也就无能为力。

看下面例子:

// 任意两个数的加法
template<class T1, class T2>
T1 Add(const T1& a, const T2& b)
{
    return a + b;
}

需要改为:

// 任意两个数的加法
template<class T1, class T2>
auto Add(const T1& a, const T2& b)
 -> decltype(a+b)
{
    return a + b;
}

decltype实际上有点像 auto的反函数 ,auto可以让你声明一个变量,而 decltype则可以从一个变量或表达式中得到类型 ,示例如下:

int x = 3;
decltype(x) y = x;

2、nullptr 空指针

nullptr是为了解决原来C++中NULL的二义性问题而引进的一种新的类型,因为NULL实际上代表的是0,

以往我们使用NULL表示空指针。它实际上是个为0的int值。

void F(int a){
    cout<<a<<endl;
}
 
void F(int *p){
    assert(p != NULL);
 
    cout<< p <<endl;
}
 
int main(){
 
    int *p = nullptr;
    int *q = NULL;
    bool equal = ( p == q ); // equal的值为true,说明p和q都是空指针
    int a = nullptr; // 编译失败,nullptr不能转型为int
    F(0); // 在C++98中编译失败,有二义性;在C++11中调用F(int)
    F(nullptr);
 
    return 0;
}

3、for容器遍历

在C++11 中for循环可以使用类似java的简化的for循环 ,可以 用于遍历数组,容器,string以及由begin和end函数定义的序列 (即有Iterator),示例代码如下:

void show()
{
	// 遍历数组
	int array[] = { 1,2,3 };
	for (auto e : array)
	{
		cout << e << endl;
	}
	// 容器遍历
	map<string, int> m{ { "a", 1 },{ "b", 2 },{ "c", 3 } };
	for (auto p : m) {
		cout << p.first << " : " << p.second << endl;
	}

	// string遍历
	string str = "C++实战网(www.cppszw.com)";
	for (auto s : str)
	{
		cout << s << endl;
	}
}

4、lambda表达式

ambda表达式类似Javascript中的闭包,它可以用于创建并定义匿名的函数对象,以简化编程工作。

Lambda的语法如下:

[函数对象参数](操作符重载函数参数)->返回值类型{函数体}
vector<int> iv{5, 4, 3, 2, 1};
int a = 2, b = 1;
 
for_each(iv.begin(), iv.end(), [b](int &x){cout<<(x + b)<<endl;}); // (1)
 
for_each(iv.begin(), iv.end(), [=](int &x){x *= (a + b);});     // (2)
 
for_each(iv.begin(), iv.end(), [=](int &x)->int{return x * (a + b);});// (3)
  • []内的参数指的是Lambda表达式可以取得的全局变量。(1)函数中的b就是指函数可以得到在Lambda表达式外的全局变量,如果在[]中传入=的话,即是可以取得所有的外部变量,如(2)和(3)Lambda表达式()内的参数是每次调用函数时传入的参数。
  • ->后加上的是Lambda表达式返回值的类型,如(3)中返回了一个int类型的变量

示例:

// 使用lambda表达式计算字符串个数以及大写字母个数
int lambda()
{
	int count = 0;
	// auto用于获取lambda表达式返回值
	auto func = [&count](string str)->int // lambda:表达式
	{
		int upper = 0;
		for (auto c : str)
		{
			count++;
			if (isupper(c))
				upper++;
		}
		return upper;
	};

	int upper = func("C++实战网(www.cppszw.com)");
	cout << "alpha counts:" << count << endl;
}

5、override、final

  • override表示重写基类的虚函数
  • final表示禁止重写基类虚函数
class base
{
public:
	virtual void function(short)
	{
		cout << "base function" << endl;
	}
	// 使用fina1修饰,表明禁止派生类重写虚函数
	virtual void show() const final
	{
		cout << "base show" << endl;
	}
};

class derived :public base
{
public:
	// 使用override修饰表示重写基类的虚函数
	virtual void function(short) override
	{
		cout << "derived function override" << endl;
	}
	virtual void show()
	{
		cout << "derived show" << endl;
	}
};

6、右值引用

  • 左值:指向稳定的内存空间 lvalue中l表示Location,寻址意思
  • 右值:指向临时的内存空间rvalue中r表示read,只读意思
extern int x;
// 返回值可以做左值,左值定义:拥有地址的表达式,左值指向的是一个稳定的内存空间
int&getvalue()
{
	return x;
}
// 右值指向的不是一个稳定的内存空间,而是一个临时空间
double getage()
{
	double age = 12.4;
	return age;
}
// 左值和右值的联系:(1)右值给左值赋值(2)可用const左值引用绑定一个右值
double leftv = getage();

// cost左值引用并不是一个左值,没有建立一个稳定的内存空间,若不是const的话,可以修改其值,右值不能赋值
//	因此必须使用const左值引用,如果使用double&xef = getage()会出错
const double&refv = getage();

	// 右值引用:可以使用右值引用显示绑定一个右值,语法是“&&”,指定了右值引用,const.或者非const都正确
double&& refright = 12.3;
const double&& crightref = 12.3;

// 重载左值和右值
void show(double& left)
{
	cout << "left value " << endl;
}
void show(double&& right)
{
	cout << "right value " << endl;
}

7、move构造函数

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <vector>
using namespace std;
 
class Str{
    public:
        char *str;
        Str(char value[])
        {
            cout<<"origin construct function..."<<endl;
            str = NULL;
            int len = strlen(value);
            str = new char[len + 1];
            memset(str,0,len + 1);
            strcpy(str,value);
        }
        Str(const Str &s)
        {
            cout<<"copy construct function..."<<endl;
            str = NULL;
            int len = strlen(s.str);
            str = new char[len + 1];
            memset(str,0,len + 1);
            strcpy(str,s.str);
        }
        Str(Str &&s)
        {
            cout<<"move construct function..."<<endl;
            str = s.str;
            s.str = NULL;
        }
        ~Str()
        {
            cout<<"destruct funciton..."<<endl;
            if(str != NULL)
            {
                delete []str;
                str = NULL;
            }
        }
};
int main()
{
    char value[] = "Hello Word!";
    cout<<"step 1"<<endl;
    Str s(value);
    cout<<"step 2"<<endl;
    Str ss(move(s));
    cout<<"step 3"<<endl;
    Str ss1 = ss;
    cout<<"step 4"<<endl;
    Str ss2 = static_cast<Str&&>(ss1);
    cout<<"finish"<<endl;
    return 0;
}

编译输出:

step 1
origin construct function...
step 2
move construct function...
step 3
copy construct function...
step 4
move construct function...
finish
destruct funciton...
destruct funciton...
destruct funciton...
destruct funciton...
  • step1:声明一个类对象,调用原始构造函数即可
  • step2:由于move将对象变量转换为右值引用,因此调用移动构造函数,可以看下移动构造函数写法,以及如何将内存转为自己的
  • step3:调用原始复制构造函数
  • step4:同样调用移动构造函数,可以看到static_cast<T&&>(lvalue)和move函数的作用是一样的。

8、容器初始化

在引入C++11之前,只有数组能使用初始化列表,其他容器想要使用初始化列表,只能用以下方法:

int arr[3] = {1, 2, 3}
vector<int> v(arr, arr + 3);

在C++11中,我们可以使用 更加优雅的 语法来进行替换:

int arr[3]{1, 2, 3};
vector<int> iv{1, 2, 3};
map<int, string>{{1, "a"}, {2, "b"}};
string str{"Hello World"};

内容来源于网络,由C++实战网(www.cppszw.com)收集整理。

更多为你推荐