【C++航海王:追寻罗杰的编程之路】C++11(一)

目录


1 -> C++11简介

在2003年C++标准委员会曾经提交了一份技术勘误表(简称TC1),使得C++03这个名字已经取代了
C++98称为C++11之前的最新C++标准名称。不过由于C++03(TC1)主要是对C++98标准中的漏洞
进行修复,语言的核心部分则没有改动,因此人们习惯性的把两个标准合并称为C++98/03标准。
从C++0x到C++11,C++标准10年磨一剑,第二个真正意义上的标准珊珊来迟。相比于
C++98/03,C++11则带来了数量可观的变化,其中包含了约140个新特性,以及对C++03标准中
约600个缺陷的修正,这使得C++11更像是从C++98/03中孕育出的一种新语言。相比较而言,
C++11能更好地用于系统开发和库开发、语法更加泛华和简单化、更加稳定和安全,不仅功能更
强大,而且能提升程序员的开发效率,公司实际项目开发中也用得比较多。

2 -> 统一的列表初始化

2.1 -> {}初始化

在C++98中,标准允许使用花括号{}对数组或者结构体元素进行统一的列表初始值设定。

#define  _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
using namespace std;

struct fyd
{
	int x;
	int y;
};

int main()
{
	int array1[] = { 1, 2, 3, 4, 5 };
	int array2[5] = { 0 };
	fyd p = { 1, 2 };

	return 0;
}

C++11扩大了用大括号括起的列表(初始化列表)的使用范围,使其可用于所有的内置类型和用户自
定义的类型,使用初始化列表时,可添加等号(=),也可不添加。

#define  _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
using namespace std;

struct fyd
{
	int x;
	int y;
};

int main()
{
	int x1 = 1;
	int x2{ 2 };
	int array1[]{ 1, 2, 3, 4, 5 };
	int array2[5]{ 0 };
	fyd p{ 1, 2 };

	// C++11中列表初始化也可以适用于new表达式中
	int* pa = new int[4]{ 0 };

	return 0;
}

创建对象时也可以使用列表初始化方式调用构造函数初始化

#define  _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
using namespace std;

class Date
{
public:
	Date(int year, int month, int day)
		:_year(year)
		, _month(month)
		, _day(day)
	{
		cout << "Date(int year, int month, int day)" << endl;
	}

private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1(2024, 4, 1);

	// C++11支持的列表初始化,这里会调用构造函数初始化
	Date d2{ 2024, 4, 2 };
	Date d3 = { 2024, 4, 3 };

	return 0;
}

2.2 -> std::initializer_list

std::initializer_list的介绍文档

std::initializer_list是什么类型:

#define  _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
using namespace std;

int main()
{
	auto il = { 10, 20, 30 };

	cout << typeid(il).name() << endl;

	return 0;
}

std::initializer_list使用场景:

std::initializer_list一般是作为构造函数的参数,C++11对STL中的不少容器就增加
std::initializer_list作为参数的构造函数,这样初始化容器对象就更方便了。也可以作为operator=
的参数,这样就可以用大括号赋值。

list

vector

map

operator=

#define  _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <vector>
#include <map>
#include <list>
using namespace std;

int main()
{
	vector<int> v = { 1,2,3,4 };
	list<int> lt = { 1,2 };

	// 这里{"sort", "排序"}会先初始化构造一个pair对象
	map<string, string> dict = { {"sort", "排序"}, {"insert", "插入"} };

	// 使用大括号对容器赋值
	v = { 10, 20, 30 };

	return 0;
}

让模拟实现的vector也支持{}初始化和赋值

#define  _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <vector>
#include <map>
#include <list>
using namespace std;

namespace fyd
{
	template<class T>
	class vector 
	{
	public:
		typedef T* iterator;
		vector(initializer_list<T> l)
		{
			_start = new T[l.size()];
			_finish = _start + l.size();
			_endofstorage = _start + l.size();
			iterator vit = _start;

			typename initializer_list<T>::iterator lit = l.begin();
			while (lit != l.end())
			{
				*vit++ = *lit++;
			}
			//for (auto e : l)
			//   *vit++ = e;
		}

		vector<T>& operator=(initializer_list<T> l) 
		{
			vector<T> tmp(l);

			std::swap(_start, tmp._start);
			std::swap(_finish, tmp._finish);
			std::swap(_endofstorage, tmp._endofstorage);

			return *this;
		}

	private:
		iterator _start;
		iterator _finish;
		iterator _endofstorage;
	};
}

3 -> 声明

C++11提供了多种简化声明的方式,尤其是在使用模板时。

3.1 -> auto

在C++98中auto是一个存储类型的说明符,表明变量是局部自动存储类型,但是局部域中定义局
部的变量默认就是自动存储类型,所以auto就没什么价值了。C++11中废弃auto原来的用法,将
其用于实现自动类型腿断。这样要求必须进行显示初始化,让编译器将定义对象的类型设置为初
始化值的类型。

#define  _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <vector>
#include <map>
#include <list>
using namespace std;

int main()
{
	int i = 10;
	auto p = &i;
	auto pf = strcpy;

	cout << typeid(p).name() << endl;
	cout << typeid(pf).name() << endl;

	map<string, string> dict = { {"sort", "排序"}, {"insert", "插入"} };

	//map<string, string>::iterator it = dict.begin();
	auto it = dict.begin();

	return 0;
}

3.2 -> decltype

关键字decltype将变量的类型声明为表达式指定的类型。

#define  _CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <vector>
#include <map>
#include <list>
using namespace std;

// decltype的一些使用使用场景
template<class T1, class T2>
void F(T1 t1, T2 t2)
{
	decltype(t1 * t2) ret;

	cout << typeid(ret).name() << endl;
}

int main()
{
	const int x = 1;
	double y = 2.2;
	decltype(x * y) ret; // ret的类型是double
	decltype(&x) p;      // p的类型是int*

	cout << typeid(ret).name() << endl;
	cout << typeid(p).name() << endl;

	F(1, 'a');

	return 0;
}

3.3 -> nullptr

由于C++中NULL被定义成字面量0,这样就可能带来一些问题,因为0既能表示指针常量,又能表示整型常量。所以出于清晰和安全角度考虑,C++11中新增了nullptr,用于表示空指针。

#ifndef NULL #ifdef __cplusplus #define NULL   0 #else #define NULL   ((void *)0) #endif #endif

感谢大佬们支持!!!

互三啦!!!

版权声明:本文为博主作者:枫叶丹4原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/weixin_74809706/article/details/137976211

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2024年5月6日
下一篇 2024年5月6日

相关推荐