C++11的部分总结(一)

版权声明: https://blog.csdn.net/qq_38697681/article/details/81474709

在本文中,对自己学习到的C++11的做个小小的总结。C++11相对于之前的标准有如下的改变:

1.C++11新增:char16_t、char32_t和long long类型

          char16_t长2个字节,char32_t长4个字节,long long至少64位,且至少与long一样长。

int main()
{
	long long a = 10000;
	char16_t ch1 = 'q';
	char32_t ch2 = '\U0000222B';
	cout << a<<","<<sizeof(long long) << endl;        //输出结果:10000,8
	cout << ch1 <<","<< sizeof(char16_t) << endl;     //输出结果:113,2
	cout << ch2 << "," << sizeof(char32_t) << endl;   //输出结果:41426,4
	return 0;
}

2.C++11新增:auto声明和decltype声明

        auto关键字实现自动类型推断,让编译器能够将变量的类型设置为初始值的类型。decltype将变量的类型声明为表达式指定的类型。

int main()
{
	auto au1 = 5.12345678;
	auto au2 = 5.1;
	auto au3 = 5;
	auto au4 = 'q';
	auto au5 = &au4;
	decltype (au1) decl1 = 5;
	cout << au1<<"," << sizeof(au1) << endl;         //double型,输出结果:5.12346,8
	cout << au2 << "," << sizeof(au2) << endl;       //double型,输出结果:5.1,8
	cout << au3 << "," << sizeof(au3) << endl;       //int型,输出结果:5,4
	cout << au4 << "," << sizeof(au4) << endl;       //char型,输出结果:q,1
	cout << *au5 << "," << sizeof(au5) << endl;      //指针变量,输出结果:q,4
	cout << decl1 << "," << sizeof(decl1) << endl;   //double型,输出结果:5,8
	return 0;
}

3.C++11数组初始化的新方式

        数组初始化可以使用{}直接赋值,若{}内为空,则默认值为0;

int main()
{

	double arr1[5]{ 1.1, 2.2, 3.3, 4.4, 5.5 };
	float arr2[5]{};
	for (int i = 0; i < 5; i++)
		cout << arr1[i] << " ";
	cout << endl;                   //for的输出结果:1.1 2.2 3.3 4.4 5.5
	for (int i = 0; i < 5; i++)
		cout << arr2[i] << " ";
	cout << endl;                   //for的输出结果:0 0 0 0 0

	return 0;
}

4.C++11字符串初始化的新方式

          字符串初始化可以使用{}直接赋值。

#include <iostream>
#include <string>
using namespace std;
int main()
{
	char ch1[]{"This is the first example."};
	string str1{ "This is the second example" };
	cout << ch1 << endl;                             //输出结果:This is the first example.
	cout << str1 << endl;                            //输出结果:This is the second example.
	return 0;
}

5.C++11新增模板类array

        vector类的功能比数组强大,但付出的代价是效率稍低。与数组一样,array对象的长度也是固定的,也使用栈(静态内存分配),而不是自由存储区,因此其效率与数组相同,但更方便,更安全。

#include <iostream>
#include <array>
using namespace std;
int main()
{
	array <float, 4> array1={1.1,1.2,1.3,1.4};
	for (int i = 0; i < 4; i++)
		cout << array1[i] << " ";
	cout << endl;                                //for循环输出结果:1.1,1.2,1.3,1.4
	return 0;
}

6.C++11新增基于范围的for循环

int main()
{
	double arr3[]{1.1, 2.2, 3.3, 4.4, 5.5};
	for (double x : arr3)
		cout << x << " ";
	cout << endl;                               //for循环输出结果:1.1,2.2,3.3,4.4,5.5
	return 0;
}

7.C++11新增右值引用

        左值是一个表示数据的表达式,程序可获取其地址。传统的C++引用(左值引用)使得标识符关联到左值。右值包括字面常量(C-风格字符串除外,它表示地址)、诸如x+y等表达式以及返回值的函数(该函数返回的不是引用)。右值引用可关联到右值,即可出现在赋值表达式右边,但不能对其应用地址运算符的值。

int main()
{
	double &&rref = sqrt(36);
	double j = 15;
	double &&jref = 2 * j + 18.5;
	cout << rref << ',' << jref << endl;
	return 0;
}

8.C++11新增空指针关键字nullptr

       空指针是不会指向有效数据的指针。C++之前使用0表示这种指针。C++11新增加了关键字nullptr,用于表示空指针,它是指针类型,不能转换为整型类型。

int main()
{
	char *ptr = nullptr;
	char *psr = 0;
	return 0;
}

C++对类方面的总结将在后续中写出

本文源代码下载地址:https://github.com/XiaoYaoNet/C-111

猜你喜欢

转载自blog.csdn.net/qq_38697681/article/details/81474709