C++ 检查向量是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3863282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking whether a vector is empty
提问by Prasoon Saurav
Suppose I have a std::vector
say Vector
假设我有std::vector
发言权Vector
Now after performing some operations on the vector(either insertion or deletion) I want to check if the vector is empty and on the basis of that I want to perform some operations.
现在在对向量执行一些操作(插入或删除)之后,我想检查向量是否为空,并在此基础上我想执行一些操作。
Which approach is better
哪种方法更好
Approach 1
方法一
if (Vector.size() == 0){ /* operations */ }
Approach 2
方法二
if (Vector.empty()) { /* operations */ }
Which is a better approach, 1
or 2
?
哪个是更好的方法,1
或者2
?
回答by sbi
v.size() == 0
says "I'm comparing the size", but does so to check whether the container empty. There's a small algorithm to digest (very small, as it only consists of a comparison) before you know what it does.
OTOH, v.empty()
does exactly what it says: it checks whether v
is empty.
Due to this, I clearly prefer #2, as it does what it says. That's why empty()
was invented, after all.
v.size() == 0
说“我正在比较大小”,但这样做是为了检查容器是否为空。在你知道它做什么之前,有一个小算法需要消化(非常小,因为它只包含一个比较)。
OTOH,v.empty()
正如它所说的那样:它检查是否v
为空。
由于这个原因,我显然更喜欢#2,因为它按照它所说的去做。empty()
毕竟,这就是发明的原因。
But there's also an algorithmic reason to prefer empty()
: If someone later changes std::vector
into a std::list
, v.size()
mighthave O(n). (In C++ 03 it's guaranteed to be O(1) for std::vector
, but not for std::list
. According to James' comment to Prasoon's answerit will be O(1) for allcontainers in C++1x.)
但也有一个更喜欢的算法原因empty()
:如果有人后来std::vector
变成了 a std::list
,则v.size()
可能有 O(n)。(在 C++ 03 中,保证为 O(1) for std::vector
,但不保证为 O(1) std::list
。根据 James 对Prasoon 的回答的评论,C++1x 中的所有容器都是 O(1) 。)
回答by Prasoon Saurav
Approach (2)
would be better because empty()
always runs in a constant time [i.e O(1)] irrespective of the container type.
方法(2)
会更好,因为无论容器类型如何,empty()
始终以恒定时间运行 [即O(1)]。
size()
too runs in O(1)
[for std::vector] although it might run in O(n)
for std:list
[thats implementation defined to be honest]
size()
在太运行O(1)
[性病::矢量]尽管它可能运行O(n)
于std:list
[定义这就是实施老实说]
In Effective STL
[Item 4] Scott Meyers says
在Effective STL
[项目 4] 斯科特迈耶斯说
You should prefer the construct using empty, and the reason is simple: empty is a constant-time operation for all standard containers, but for some list implementations, size takes linear time.
.....
No matter what happens, you can't go wrong if you call empty instead of checking to see if size() == 0. So call empty whenever you need to know whether a container has zero elements.
您应该更喜欢使用 empty 的构造,原因很简单:对于所有标准容器来说,empty 是一个恒定时间操作,但对于某些列表实现,size 需要线性时间。
.....
不管发生什么,如果你调用 empty 而不是检查 size() == 0,你就不会出错。所以当你需要知道一个容器是否有零个元素时调用 empty 。
回答by Katalonis
I would say approch no 2, as method empty() was intentionally designed to check if an vector is empty. You may also check the efficiance of both approaches, and then decide which one is better.
我会说没有 2,因为方法 empty() 是有意设计来检查向量是否为空的。您还可以检查两种方法的效率,然后决定哪一种更好。
回答by codaddict
Typically a vector is internally implemented as a pointer to a dynamically allocated array,and data members holding the capacity
and size
of the vector. The size
of the vector is the actual number of elements, while the capacity refers to the size of the dynamic array.
通常,载体在内部作为指针实现为动态分配的数组,和数据成员保持capacity
和size
载体。所述size
载体的是元件的实际数目,而容量指的是动态数组的大小。
Given this implementation, the member function size()
will simply be a getter to the member size
.
鉴于此实现,成员函数size()
将只是成员的吸气剂size
。
The empty()
will return the result of the comparison size == 0
.
在empty()
将返回比较的结果size == 0
。
So both are equally efficient O(1)
but its recommended to empty()
if you want to check if vector is empty. Because that is what the function is there for. It'll make your code easier to read.
所以两者都同样有效,O(1)
但empty()
如果您想检查 vector 是否为空,建议您这样做。因为这就是函数的用途。这将使您的代码更易于阅读。
回答by Deathlymad
Actually vector.empty() and vector.size()==0 are doing the same thing. empty compares the beginning and the end and returns true if they are the same, size calculates begin - end therefor returning 0 if it is empty therefor doing the same thing using another calculation.
实际上 vector.empty() 和 vector.size()==0 正在做同样的事情。空比较开始和结束,如果它们相同则返回真,大小计算开始 - 结束因此返回 0 如果它是空的,因此使用另一个计算做同样的事情。
回答by Daniel Mo?mondor
If you are new to the programming, use one that has more meaning to you. For example if ==0 is more meaningful to you than .empty(), use that.
如果您不熟悉编程,请使用对您更有意义的编程。例如,如果 ==0 对您来说比 .empty() 更有意义,请使用它。
Later, if you have performance problems (which I strongly doubt that you will have here) use one that satisfies your performance targets.
稍后,如果您有性能问题(我强烈怀疑您会在这里遇到),请使用满足您的性能目标的问题。
回答by Benoit
Just for fun: why not:
只是为了好玩:为什么不:
if(Vector.begin() == Vector.end())
?
?
回答by nakiya
Go for empty().
去空()。