vector::empty() and vector::size() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.
vector::empty()
empty() function is used to check if the vector container is empty or not. 函数empty()检测vector是否为空
Syntax :
vectorname.empty() Parameters : No parameters are passed. Returns : True, if vector is empty False, Otherwise
Examples:
Input : myvector = 1, 2, 3, 4, 5 myvector.empty(); Output : False Input : myvector = {} myvector.empty(); Output : True
Errors and Exceptions
1. It has a no exception throw guarantee.
2. Shows error when a parameter is passed.
// CPP program to illustrate
// Implementation of empty() function
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> myvector{};
if (myvector.empty()) {
cout << "True";
}
else {
cout << "False";
}
return 0;
}
Output:
True
Application :
Given a list of integers, find the sum of the all the integers. 给定一系列整数,获得所有整数的和。
Input : 1, 5, 6, 3, 9, 2 Output : 26 Explanation - 1+5+6+3+9+2 = 26
Algorithm
1. Check if the vector is empty, if not add the back element to a variable initialised as 0, and pop the back element.检查vector是否为空,不空pop最后一个元素。
2. Repeat this step until the vector is empty.循环直到vector为空。
3. Print the final value of the variable.
// CPP program to illustrate
// Application of empty() function
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int sum = 0;
vector<int> myvector{ 1, 5, 6, 3, 9, 2 };
while (!myvector.empty()) {
sum = sum + myvector.back();
myvector.pop_back();
}
cout << sum;
return 0;
}
Output:
26
vector::size()
size() function is used to return the size of the vector container or the number of elements in the vector container. 函数size()获得vector的大小,即vector中元素的个数。
Syntax :
vectorname.empty() Parameters : No parameters are passed. Returns : Number of elements in the container.
Examples:
Input : myvector = 1, 2, 3, 4, 5 myvector.size(); Output : 5 Input : myvector = {} myvector.size(); Output : 0
Errors and Exceptions
1. It has a no exception throw guarantee.
2. Shows error when a parameter is passed.
// CPP program to illustrate
// Implementation of size() function
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> myvector{ 1, 2, 3, 4, 5 };
cout << myvector.size();
return 0;
}
Output:
5
Why is empty() preferred over size()为什么empty()比size()好。
empty() function is often said to be preferred over the size() function due to some of these points-
- empty() function does not use any comparison operators, thus it is more convenient to use函数empty()没有使用比较操作,更便捷。
- empty() function is implemented in constant time, regardless of container type, whereas some implementations of size() function require O(n) time complexity such as list::size(). 函数empty()的时间效率是常数, size()是O(n),
Application :
Given a list of integers, find the sum of the all the integers.
Input : 1, 5, 6, 3, 9, 2 Output : 26 Explanation - 1+5+6+3+9+2 = 26
Algorithm
1. Check if the size of the vector is 0, if not add the back element to a variable initialised as 0, and pop the back element.
2. Repeat this step until the size of the vector becomes 0.
3. Print the final value of the variable.
// CPP program to illustrate
// Application of size() function
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int sum = 0;
vector<int> myvector{ 1, 5, 6, 3, 9, 2 };
while (myvector.size() > 0) {
sum = sum + myvector.back();
myvector.pop_back();
}
cout << sum;
return 0;
}
Output:
26
Recommended Posts:
- Rearrange Odd and Even values in Alternate Fashion in Ascending Order
- Similarities and Differences between Ruby and C++
- Is body of a Default Constructor blank in C++?
- clocale header file in C++
- Commonly used String functions in C/C++ with Examples
- Difference between C and C++
- unordered_map emplace() in C++ STL
- Find Maximum and Minimum element in a Set in C++ STL
- deque emplace in C++ STL
- valarray atan2() function in C++
- How to delete an element from the Set by passing its value in C++
- unordered_multimap insert() in C++ STL
- How to delete a range of values from the Set using Iterator
- unordered_map cbegin in C++ STL
- unordered_map size() in C++ STL
上一篇: STL string类