检查向量是否为空

发布于 2024-09-26 14:56:55 字数 342 浏览 1 评论 0原文

假设我有一个 std::vectorVector

现在对向量执行一些操作(插入或删除)后,我想检查向量是否为空以及在此基础上我想执行一些操作。

哪种方法更好

方法 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

鲜血染红嫁衣 2024-10-03 14:56:55

v.size() == 0 表示“我正在比较大小”,但这样做是为了检查容器是否为空。在您知道它的作用之前,需要消化一个小算法(非常小,因为它只包含比较)。
OTOH,v.empty() 的作用正如它所说:它检查 v 是否为空。
因此,我显然更喜欢#2,因为它做到了它所说的。毕竟,这就是发明 empty() 的原因。

但还有一个更喜欢 empty() 的算法原因:如果后来有人将 std::vector 更改为 std::listv.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 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.

But there's also an algorithmic reason to prefer empty(): If someone later changes std::vector into a std::list, v.size() might have 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 answer it will be O(1) for all containers in C++1x.)

你的呼吸 2024-10-03 14:56:55

方法 (2) 会更好,因为 empty() 总是在恒定时间内运行 [即 O(1)] 无论容器类型如何。

size() 也以 O(1) 运行[对于 std::vector],尽管对于 它可能以 O(n) 运行>std:list [这是诚实定义的实现]

Effective STL[Item 4] Scott Meyers 中说

您应该更喜欢使用empty的构造,原因很简单:empty对于所有标准容器来说是一个恒定时间操作,但对于某些列表实现来说,
大小需要线性时间。

......

无论发生什么,如果你调用empty而不是检查to,你就不会出错
查看 size() == 0 是否。因此,每当您需要知道容器是否有
零元素。

Approach (2) would be better because empty() always runs in a constant time [i.e O(1)] irrespective of the container type.

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]

In Effective STL[Item 4] Scott Meyers says

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.

‖放下 2024-10-03 14:56:55

我想说的是方法二,因为方法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.

套路撩心 2024-10-03 14:56:55

通常,向量在内部实现为指向动态分配数组的指针,以及保存向量的容量大小的数据成员。向量的大小是实际的元素数量,而容量是指动态数组的大小。

给定此实现,成员函数 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 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.

Given this implementation, the member function size() will simply be a getter to the member size.

The empty() will return the result of the comparison 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.

深海不蓝 2024-10-03 14:56:55

实际上向量.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.

感情废物 2024-10-03 14:56:55

如果您是编程新手,请使用对您更有意义的编程。例如,如果 ==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.

熟人话多 2024-10-03 14:56:55

寻找空()。

Go for empty().

淡看悲欢离合 2024-10-03 14:56:55

只是为了好玩:为什么不:

if(Vector.begin() == Vector.end())

Just for fun: why not:

if(Vector.begin() == Vector.end())

?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文