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-

  1. empty() function does not use any comparison operators, thus it is more convenient to use函数empty()没有使用比较操作,更便捷。
  2. 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:

猜你喜欢

转载自blog.csdn.net/qq_27009517/article/details/86503825