Checking whether a vector is empty

C++StlVector

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

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.)

Solution 2 - C++

Approach (2) would be better because empty() always runs in a constant time [i.e [O(1)][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. [1]: http://en.wikipedia.org/wiki/Time_complexity

Solution 3 - C++

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.

Solution 4 - C++

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.

Solution 5 - C++

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.

Solution 6 - C++

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.

Solution 7 - C++

Just for fun: why not:

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

?

Solution 8 - C++

Go for empty().

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPrasoon SauravView Question on Stackoverflow
Solution 1 - C++sbiView Answer on Stackoverflow
Solution 2 - C++Prasoon SauravView Answer on Stackoverflow
Solution 3 - C++KatalonisView Answer on Stackoverflow
Solution 4 - C++codaddictView Answer on Stackoverflow
Solution 5 - C++Daniel MošmondorView Answer on Stackoverflow
Solution 6 - C++DeathlymadView Answer on Stackoverflow
Solution 7 - C++BenoitView Answer on Stackoverflow
Solution 8 - C++nakiyaView Answer on Stackoverflow