How to use the std::vector container for C++
This article mainly introduces how C++ uses std::vector container related knowledge, detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that everyone will read this C++ how to use std::vector container article will have some gains, let's take a look at it.
preface
Vector is essentially a C++ class, similar to arrays, but the advantage of vector is that it can be dynamically extended without considering its memory size.
Definition:
A vector is a Sequence Container that encapsulates a dynamically sized array. Like any other type of container, it can hold various types of objects. A vector can simply be thought of as a dynamic array capable of storing any type.
Features:
1. sequential series
The elements in a sequential container are sorted in strict linear order. You can access an element by its position in the sequence.
2. dynamic array
Fast direct access to any element in a sequence is supported, even through pointer arithmetic. Operation provides for relatively fast addition/deletion at the end of the sequence
Except for the operation of elements.
3. Allocator-aware
A container uses a memory allocator object to dynamically handle its storage requirements.
1. Vector constructor prototype:
vector data; //Use template class, default constructor
vector(data.begin(),data.end()); //copy the elements in the data [begin(),end()) interval to itself, note that it is a closed and open interval in mathematics
vector(n,elem); //copy n elem elements to itself
vector(const vector &data); //copy construct
#include #include using namespace std; void fun_print_vector(std::vector&data){ //std::vector::iterator it; //can be replaced by auto. for(auto it_data = data.begin();it_data != data.end();it_data++){ std::cout