检查向量是否为空
假设我有一个 std::vector
说 Vector
现在对向量执行一些操作(插入或删除)后,我想检查向量是否为空以及在此基础上我想执行一些操作。
哪种方法更好
方法 1
if (Vector.size() == 0){ /* operations */ }
方法 2
if (Vector.empty()) { /* operations */ }
哪种方法更好,1
还是 2
?
Suppose I have a std::vector
say 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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
v.size() == 0
表示“我正在比较大小”,但这样做是为了检查容器是否为空。在您知道它的作用之前,需要消化一个小算法(非常小,因为它只包含比较)。OTOH,
v.empty()
的作用正如它所说:它检查v
是否为空。因此,我显然更喜欢#2,因为它做到了它所说的。毕竟,这就是发明
empty()
的原因。但还有一个更喜欢
empty()
的算法原因:如果后来有人将std::vector
更改为std::list
,v.size()
可能 的时间复杂度为 O(n)。 (在 C++ 03 中,std::vector
的复杂度保证为 O(1),但std::list
则不然。根据 James 对 Prasoon 的答案全部 C++1x 中的容器。)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 whetherv
is empty.Due to this, I clearly prefer #2, as it does what it says. That's why
empty()
was invented, after all.But there's also an algorithmic reason to prefer
empty()
: If someone later changesstd::vector
into astd::list
,v.size()
might have O(n). (In C++ 03 it's guaranteed to be O(1) forstd::vector
, but not forstd::list
. According to James' comment to Prasoon's answer it will be O(1) for all containers in C++1x.)方法
(2)
会更好,因为empty()
总是在恒定时间内运行 [即 O(1)] 无论容器类型如何。size()
也以O(1)
运行[对于 std::vector],尽管对于它可能以
[这是诚实定义的实现]O(n)
运行>std:list在
Effective STL
[Item 4] Scott Meyers 中说Approach
(2)
would be better becauseempty()
always runs in a constant time [i.e O(1)] irrespective of the container type.size()
too runs inO(1)
[for std::vector] although it might run inO(n)
forstd:list
[thats implementation defined to be honest]In
Effective STL
[Item 4] Scott Meyers says我想说的是方法二,因为方法empty()是故意设计来检查向量是否为空。您还可以检查两种方法的效率,然后决定哪一种更好。
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.
通常,向量在内部实现为指向动态分配数组的指针,以及保存向量的
容量
和大小
的数据成员。向量的大小是实际的元素数量,而容量是指动态数组的大小。给定此实现,成员函数
size()
将只是成员size
的 getter。empty()
将返回比较结果size == 0
。因此两者的效率相同
O(1)
,但如果您想检查向量是否为空,建议使用empty()
。因为这就是该函数的用途。它将使您的代码更易于阅读。Typically a vector is internally implemented as a pointer to a dynamically allocated array,and data members holding the
capacity
andsize
of the vector. Thesize
of the vector is the actual number of elements, while the capacity refers to the size of the dynamic array.Given this implementation, the member function
size()
will simply be a getter to the membersize
.The
empty()
will return the result of the comparisonsize == 0
.So both are equally efficient
O(1)
but its recommended toempty()
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.实际上向量.empty() 和向量.size()==0 正在做同样的事情。
empty 比较开始和结束,如果它们相同则返回 true,size 计算开始 - 结束,因此如果为空则返回 0,因此使用另一个计算执行相同的操作。
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.
如果您是编程新手,请使用对您更有意义的编程。例如,如果 ==0 对您来说比 .empty() 更有意义,请使用它。
稍后,如果您遇到性能问题(我强烈怀疑您在这里会遇到这种问题),请使用满足您的性能目标的解决方案。
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.
Later, if you have performance problems (which I strongly doubt that you will have here) use one that satisfies your performance targets.
寻找空()。
Go for empty().
只是为了好玩:为什么不:
?
Just for fun: why not:
?