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开发者_如何学C, 1
or 2
?
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.)
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.
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.
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.
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.
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.
Go for empty().
Just for fun: why not:
if(Vector.begin() == Vector.end())
?
精彩评论