[C++] STL container - vector dynamic array container ③ (vector container initialization - initialization list | vector container assignment - assign function/swap function)






1. Vector container initialization



1. Vector container initialization


The vector container is a dynamic array container in the C++ Standard Template Library (STL). The container has the following initialization method:

  • Default initialization: Create an empty vector container; the container is empty by default;
// 创建一个空的 vector 容器 , 元素类型是 int 类型
vector<int> vec;
  • Use std::initializer_list to initialize the list: when creating a vector container, directly specify the element value;
// 创建一个 vector 容器 , 元素类型是 int 类型
// 为其初始化 1, 2, 3 三个元素值 
vector<int> vec {
    
    1, 2, 3};
  • Use array initialization: Pass an array and the number of arrays to the vector container constructor to initialize the vector container.
// 先声明一个数组
int array[] = {
    
    1, 2, 3, 4, 5};  

// 将整个数组的值 初始化给 vector 容器
vector<int> vec(array, array + sizeof(array) / sizeof(int)); 
  • Use iterator range initialization: specify the range of elements to be copied by passing two iterators;
// 初始化一个 vector 容器
vector<int> vec1 {
    
    1, 2, 3};  

// 使用 范围构造函数 从 vec1 容器中 复制元素到 vec2 容器
vector<int> vec2(vec1.begin(), vec1.end());

2. Use std::initializer_list initialization list to initialize the vector container


In the previous parameterized constructor, several vector initialization methods were introduced, here

std::initializer_listIt is a template class introduced in C++11, which is used to initialize container objects;

Very useful if you need to initialize an std::vectoror std::list container with a set of values;std::initializer_list


To use std::initializer_listthe initialized vector container, you can declare it first std::initializer_list, and then use the declared std::initializer_listinitialized vector container; the following code example:

// 声明 initializer_list 
std::initializer_list<int> initList = {
    
    1, 2, 3, 4, 5};  

// 使用 initializer_list 初始化 vector
std::vector<int> vec(initList); 

You can also specify it directly during initialization std::initializer_list;

	// 使用 initializer_list 初始化 vector
	// 下面两种方式是等价的
	std::vector<int> vec5{
    
     1, 2, 3, 4, 5 };
	std::vector<int> vec6 = {
    
     1, 2, 3, 4, 5 };

3. Code example - vector container initialization


Code example:

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

// 自定义类
class Student{
    
    };

int main() {
    
    

	// 1. 默认初始化
	// 创建一个空的 vector 容器 , 元素类型是 int 类型
	vector<int> vec;

	// 2. 创建一个 vector 动态数组容器 
	// 该容器中 有 3 个元素 , 每个元素值为 int 类型值 1
	vector<int> vec1(3, 1);

	// 3. 使用 范围构造函数 从 vec1 容器中 复制元素到 vec2 容器
	vector<int> vec2(vec1.begin(), vec1.end());

	// 4. 使用 拷贝构造函数 创建 vec3 容器 
	// 将其初始化为 vec1 的副本
	vector<int> vec3(vec1);

	// 5. 使用初始化列表初始化
	// 声明 initializer_list 
	std::initializer_list<int> initList = {
    
     1, 2, 3, 4, 5 };
	// 使用 initializer_list 初始化 vector
	std::vector<int> vec4(initList);

	// 6. 使用初始化列表初始化
	// 使用 initializer_list 初始化 vector
	// 下面两种方式是等价的
	std::vector<int> vec5{
    
     1, 2, 3, 4, 5 };
	std::vector<int> vec6 = {
    
     1, 2, 3, 4, 5 };
	
	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

Results of the :

Insert image description here





2. Vector container assignment



When the vector container is initialized, the initialization value can be set, which has been discussed in the previous chapter;

After the vector container is initialized, if you want to modify the contents of the vector container,

  • In addition to assignment,
  • You can also add and delete elements, such as: insert to insert elements, push_back to add elements at the end, etc.;

In this chapter, we mainly discuss the assignment situation after initialization;


1. Vector container assignment method


vector container assignment method:

  • assign function assignment: clear all elements in the container and fill the container with newly allocated elements; n represents the number of elements to be allocated, and val represents the element value to be allocated;
void assign(size_type n, const value_type& val);

// 1. 将 vec2 容器中的值替换为 3 个 int 类型数据 8
vector<int> vec2;
vec2.assign(3, 8);
  • assign function assignment: clear all elements in the container and fill the container with newly allocated elements; first and last are iterators, indicating the range of elements to be allocated;
void assign(InputIt first, InputIt last);

// 2. 将 vec3 容器中的值替换为 vec1 容器中的 指定范围数据
vector<int> vec3;
vec3.assign(vec1.begin(), vec1.end());
  • The swap function exchanges data: exchanges the data in this vector container with the data in other containers;
void swap(vector& other);

// 3. 将 vec1 与 vec2 容器中的数据进行交换
vec1.swap(vec2);
  • Overload the equal sign operator function: use the equal sign operator of the vector container to assign the contents of another vector container to the current container;
vector& operator=(const vector& vec);	

// 4. 使用重载等号操作符函数 进行赋值
vector<int> vec4;
vec4 = vec1;

2. Code example - vector container assignment


Code example:

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

// 自定义类
class Student{
    
    };

int main() {
    
    

	// 创建一个 vector 容器 , 使用初始化列表进行初始化
	vector<int> vec1{
    
     1, 2, 3, 4, 5 };

	// 1. 将 vec2 容器中的值替换为 3 个 int 类型数据 8
	vector<int> vec2;
	vec2.assign(3, 8);

	// 2. 将 vec3 容器中的值替换为 vec1 容器中的 指定范围数据
	vector<int> vec3;
	vec3.assign(vec1.begin(), vec1.end());

	// 3. 将 vec1 与 vec2 容器中的数据进行交换
	vec1.swap(vec2);

	// 4. 使用重载等号操作符函数 进行赋值
	vector<int> vec4;
	vec4 = vec1;

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

	return 0;
};

Results of the :

Insert image description here

Guess you like

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