简体   繁体   English

向量之间有什么区别 <int> ()与向量 <int> {} vs NULL vs size = 0?

[英]What's the difference among vector<int>() vs vector<int>{} vs NULL vs size=0?

I want to return an empty vector at the end of my function. 我想在函数末尾返回一个空向量。 Does the following all equivalent? 以下所有是否等同? How to understand each of them? 如何理解他们每个人?

return vector<int>();
return vector<int>{};
return NULL;

The following is my understanding: 以下是我的理解:

1. vector<int>() means creating an empty vector object, which is uninitialized so it's NULL. 1. vector<int>()意味着创建一个空的矢量对象,该对象未初始化,因此为NULL。

2. vector<int>{} means creating an empty vector, which has a size 0. 2. vector<int>{}表示创建一个大小为0的空向量。

Is there a difference between size 0 and NULL? 大小0和NULL之间有区别吗? Thanks a lot~ 非常感谢〜

The first two are both doing value initialization , and are the same. 前两个都在进行值初始化 ,并且是相同的。

NULL is the old backward-compatible symbolic constant for null pointers . NULL是null 指针的旧的向后兼容符号常量。 C++ doesn't have the concept of "null" values. C ++没有“空”值的概念。 Unless you return a pointer, it's invalid. 除非返回指针,否则它是无效的。


Regarding NULL , in C++ before the C++11 standard you should really be using 0 for null pointers. 关于NULL ,在C ++ 11标准之前的C ++中,您实际上应该将0用作空指针。 And since the C++11 standard you should be using the nullptr literal. 从C ++ 11标准开始,您应该使用nullptr文字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM