C++ 11(1)

前面的文章中我们讲解了STL中一些容器及其使用,如unordered_map、map等,在下面的文章中我们将要来介绍C++ 11中一些新的内容。

统一的列表初始化

{}初始化

在C++98中,标准允许使用花括号对数组或者结构体元素进行统一的列表初始化。例如下面的代码

struct Point
{
    int _x;
    int _y;
};
int main()
{
    int array1[] = { 1, 2, 3, 4, 5 };
    int array2[5] = { 0 };
    Point p = { 1, 2 };
    return 0;
}

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

int main()
{
	int x = 1;
	int x2 = { 1 };
	int x3{ 3 }; // 可以省略赋值符号
	int array1[]{ 1, 2, 3, 4, 5 };

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

创建自定义对象的时候也可以使用列表初始化方式调用构造函数进行初始化

class Date
{
public:
	//explicit Date(int year, int month, int day)
	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(2022, 1, 1); // old style
	// C++11支持的列表初始化,这里会调用构造函数初始化
	Date d2{ 2022, 1, 2 };
	Date d3 = { 2022, 1, 3 }; // explicit
	return 0;
}

std::initializer_list

与上面的自定义类型同理,vector也可以使用列表初始化,在这里就有这样的一个问题就是vector是怎样实现列表初始化的?在我们自己仿真实现的vector中并没有进行使用std::initializer_list进行构造的构造函数。这里我们就要先提出一个类型std::initializer_list。使用下面的例子:

auto i1 = { 10,20,30,1,1,2,2,2,2,2,2,1,1,1,1,1,1,1,1,2,1,1,2 };
auto i2 = { 10,20,30 };
cout << typeid(i1).name() << endl; // class std::initializer_list<int>
cout << typeid(i2).name() << endl; // class std::initializer_list<int>

通过查看相关的文档,可以得到这个类型也是有begin与end的。

initializer_list<int>::iterator it1 = i1.begin();

//(*it1)++;
cout << *it1 << endl; // 不能修改,说明数据存储在常量区

那么同样我们可以在自己编写的vector中添加使用这个类型的构造函数:

vector(initializer_list<T> il)
{
	// 在C++11中我们自己编写的vector有这样的问题:没有实例化的类模板取内嵌类型需要进行typename声明
	//typename initializer_list<T>::iterator it = il.begin();
	//while (it != il.end())
	//{ 
	//	push_back(*it);
	//	++it;
	//}

	for (auto& e : il)
	{
		push_back(e);
	}
}

假如我们要在vector中存储Date类型的数据之前我们是无法直接在构造的时候直接给数据的,现在使用initializer_list就可以进行构造。

Date d3( 2023, 5, 28 );
Date d4( 2023, 5, 29 );
// initializer_list<Date>
vector<Date> v3 = { d3, d4 };
vector<Date> v4 = { Date ( 2023, 5, 28 ), Date ( 2023, 5, 29 ) };

同样我们还可以使用一种更加简洁的方法:对于map等其他的类型也是同理。

vector<Date> vd3 = { {2023,5,20}, {2023,5,20} }; // 调用Date的构造 + 调用initializer_list构造
map<string, string> dict = { {"sort", "排序"},{"string", "字符串"},{"Date", "日期"} };
pair<string, string> kv1 = { "Date", "日期" };
pair<string, string> kv2 { "Date", "日期" };

声明

auto

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

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

decltype

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

// 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');

	// vector存储的类型跟x*y表达式返回值类型一致
	// decltype推导表达式类型,用这个类型实例化模板参数或者定义对象
	vector<decltype(x* y)> v;
	return 0;
}

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

#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值