[C++] STL container - vector dynamic array container ② (vector parameterized constructor | range constructor | initialize n specified elements | copy constructor)






1. Vector parameterized constructor



1. Initialize using another vector object - range constructor


vector is a dynamic array container. When initializing, you can use another vector object to initialize;

The following vector has a parameter constructor that copies the elements between begin and end to the newly created vector container;

template <class InputIterator>  
vector (InputIterator begin, InputIterator end);
  • InputIterator parameter type description: This type is an iterator type, used to specify the element range of the vector container to be copied;
  • begin parameter: an iterator pointing to the starting position of the range of other vector container elements to be copied;
  • end parameter: an iterator pointing to the end position of the range of other vector container elements to be copied;

Special note: This constructor does not check whether begin and end are valid. Be sure to verify whether the range of the iterator is legal before using it. If it is out of bounds, an exception will occur;


Code example: In the following code, first initialize the vec1 container, which has 3 elements {1, 2, 3}, and then call the range constructor to copy the elements in the specified range of the vec1 container to the vec2 container for use in the vec2 container. initialization;

// 初始化一个 vector 容器
vector<int> vec1 {
    
    1, 2, 3};  

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

2. The vector container initializes n specified elements


Initialize a vector container containing n specified elements. The constructor prototype is as follows:

template <class T, class Allocator = std::allocator<T>>  
class vector {
    
      
public:  
    // 构造函数  
    explicit vector(size_type n, 
    	const value_type& value = value_type(), 
    	const allocator_type& a = allocator_type());  
};

Parameter Description :

  • size_type n parameter: number of elements, copy the following value parameter value n times, unsigned integer type, indicating the size of the vector;
  • const value_type& value parameter: the element value to be copied; constant reference, representing the value of each element, the default type is T, which is the type of the element in the vector;
  • const allocator_type& a parameter: the memory allocator that allocates memory for elements. The standard allocator std::allocator<T> is used by default;

This constructor is slightly different from the one initialized with two iterator ranges;

  • When initializing with two iterator ranges, all elements in the specified range will be copied to the newly created vector;
  • When this constructor is initialized with n and element values, it will copy the specified number of identical elements to the newly created vector;

Code example: In the following code, a vector dynamic array container is created. There are 5 elements in the container, and each element value is an int type value 6;

// 创建一个 vector 动态数组容器 
// 该容器中 有 5 个元素 , 每个元素值为 int 类型值 6
std::vector<int> vec(5, 6); 

// 创建一个 vector 动态数组容器 
// 该容器中 有 3 个元素 , 每个元素值为 字符 `A`
std::vector<int> vec(3, 'A'); 

3. Vector container copy constructor


The vector container copy constructor is used to create a new vector container object and initialize it as a copy of another existing vector object;

The prototype of the vector container copy constructor is as follows: when performing copy construction, first allocate enough memory to store the copied elements, and then use the allocator to copy all elements;

template <class T, class Allocator = std::allocator<T>>  
class vector {
    
      
public:  
    // 拷贝构造函数  
    vector(const vector& other);  
};

Make sure that all elements in the copy constructor are copyable, and the default is shallow copy;


Code example:

// 创建 vector 容器 1 , 并初始化
std::vector<int> vec1 {
    
    1, 2, 3};  

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

4. Code example - vector container parameterized constructor


Code example:

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

// 自定义类
class Student{
    
    };

int main() {
    
    

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

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

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

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

	return 0;
};

Results of the :

Insert image description here

Guess you like

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