[C++] STL container - vector dynamic array container ④ (vector container capacity size operation | vector container capacity determination | vector container re-specifies the container size | insert/delete elements at the end of the container)






1. Vector container capacity size operation



1. Vector container capacity determination


vector container capacity determination:

  • Get the number of elements: The size() function returns the number of elements in the vector container. The type is size_type, which is an unsigned integer type; the noexcept specifier indicates that the function will not throw an exception; the function prototype and sample code are as follows:
// 函数原型
size_type size() const noexcept;

// 代码示例
std::vector<int> vec = {
    
    1, 2, 3, 4, 5};  
// vec.size() = 5
std::cout << "vec.size() = " << vec.size() << std::endl; 
  • Determine whether it is empty: The empty() function returns a Boolean value, indicating whether the vector container is empty; if there are no elements in the container, it returns true, otherwise it returns false; the noexcept specifier indicates that the function will not throw an exception;
// 函数原型
bool empty() const noexcept;

// 代码示例
std::vector<int> vec;  
if (vec.empty()) {
    
      
    std::cout << "vec empty" << std::endl;  
}

2. Vector container resizes the container


Respecify the length: parameter n represents the new container size;

  • If n is greater than the size of the current container, the element will be added to the end of the container and the new element will be created using the default constructor of the element type;
  • If n is less than the size of the current container, the element will be deleted at the beginning of the container;
// 重新指定容器大小
void resize(size_type n) noexcept;  

Code example:

// 创建一个包含 3 个元素的 vector  
std::vector<int> vec = {
    
    1, 2, 3};  

// 将 vector 的大小增加到 5  
vec.resize(5);  

Respecify the length and fill it: parameter n represents the new container size;

  • If n is greater than the size of the current container, the element specified by the val parameter will be added to the end of the container;
  • If n is less than the size of the current container, the element will be deleted at the beginning of the container;
// 重新指定容器大小 并进行填充
void resize(size_type n, const value_type& val) noexcept;

Code example:

// 创建一个包含 3 个元素的 vector  
std::vector<int> vec = {
    
    1, 2, 3};  

// 将 vector 的大小增加到 5 , 并使用 6 填充剩余元素
vec.resize(5, 6);  

Neither of the above functions changes the order of the elements;


3. Code examples


Code example:

#include "iostream"
using namespace std;
#include "vector"

int main() {
    
    

	std::vector<int> vec = {
    
     1, 2, 3 };

	// 1. 获取元素个数
	// vec.size() = 5
	std::cout << "vec.size() = " << vec.size() << std::endl;

	// 2. 判断容器是否为空
	if ( !vec.empty() ) {
    
    
		std::cout << "vec empty" << std::endl;
	}

	// 3. 将 vector 的大小增加到 5  
	vec.resize(5);


	// 4. 将 vector 的大小增加到 8 , 并使用 6 填充剩余元素
	vec.resize(8, 6);

	
	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

Results of the :

vec.size() = 3
vec empty
Press any key to continue . . .

Insert image description here





2. Insert/delete elements at the end of the vector container



1. Insert elements at the end of the vector container


To insert elements at the end of the vector container, you can call the push_back function. The prototype of this function is as follows:

void push_back(const value_type& val);  

Accepts a constant reference val and adds val to the end of the vector container;


Code example:

#include "iostream"
using namespace std;
#include "vector"

int main() {
    
    

    // 创建空的 vector 容器
    std::vector<int> vec;

    // 向容器尾部添加元素
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);

    // 输出 vector 的内容  
    for (int x : vec) {
    
    
        std::cout << x << ' ';
    }
    std::cout << std::endl;

	
	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

Results of the :

1 2 3
Press any key to continue . . .

Insert image description here


2. Delete elements at the end of the vector container


To delete elements from the tail of the vector container, you can call the pop_back member function of the vector class. This function is used to delete the last element in the vector container. The function prototype is as follows:

void pop_back();

Code example:

#include "iostream"
using namespace std;
#include "vector"

int main() {
    
    

    // 创建空的 vector 容器
    std::vector<int> vec{
    
    1, 2, 3};

    // 移除容器尾部一个元素
    vec.pop_back();

    // 输出 vector 的内容  
    for (int x : vec) {
    
    
        std::cout << x << ' ';
    }
    std::cout << std::endl;

	
	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

Results of the :

Insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/135069581