C++11知识点——{}大括号的用法

这篇博客介绍了C++11中使用大括号{}

C++11提出{}大括号新用法,如下:

std::vector<cv::Point3d> row;
row.push_back({3, 0, 0}); 

这里大括号是C++11功能,可以使用大括号(而不是圆括号)括起来的列表调用构造函数,这里相当于是调用了cv::Point3d的构造函数等价于row.push_back(cv::Point3d(3, 0, 0));

例子代码如下:

#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;

class Stump
{
private:
    int roots;
    double weight;
public:
    Stump(int r, double w):roots(r), weight(w){}
	void show(){
		std::cout<<"roots: "<<roots<<", weight: "<<weight<<std::endl;
	}
};

int main(int argc, char** argv)
{
	std::vector<cv::Point3d> row;
	row.push_back({3, 0, 0}); 
	row.push_back(cv::Point3d(3, 1, 0)); 
	std::cout<<row[0]<<std::endl;
	row[0]={3,1,1}; 
	std::cout<<row[0]<<std::endl;
	std::cout<<row[1]<<std::endl;

	Stump s1(1,2.2);  	//old style
	Stump s2{4,3.2};  	//C++11
	Stump s3 = {2,3.3}; 	//C++11
	s3.show();
	return 0;
}

输出:

[3, 0, 0]
[3, 1, 1]
[3, 1, 0]
roots: 2, weight: 3.3
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值