C++ vector Container Analysis
Category Programming Techniques
I. What is a vector?
A vector is a sequence container that encapsulates a dynamic array with a variable size. Like any other container, it can hold objects of various types. Essentially, a vector can be considered a dynamic array that can store any type of object.
II. Container Characteristics
1. Sequential Sequence
Elements in a sequence container are strictly ordered in a linear sequence. You can access the corresponding element by its position in the sequence.
2. Dynamic Array
Supports fast direct access to any element in the sequence, and even allows this operation through pointer arithmetic. Provides relatively fast operations for adding/removing elements at the end of the sequence.
3. Allocator-Aware
The container uses a memory allocator object to dynamically handle its storage requirements.
III. Basic Function Implementation
vector(): Creates an empty vector
vector(int nSize): Creates a vector with nSize elements
vector(int nSize, const t& t): Creates a vector with nSize elements, all initialized to the value t
vector(const vector&): Copy constructor
vector(begin, end): Copies elements from another array within the range [begin, end) into the vector
2. Addition Functions
void push_back(const T& x): Adds an element X to the end of the vector
iterator insert(iterator it, const T& x): Inserts an element x before the element pointed to by the iterator
iterator insert(iterator it, int n, const T& x): Inserts n copies of the element x before the element pointed to by the iterator
iterator insert(iterator it, const_iterator first, const_iterator last): Inserts data from another vector of the same type within the range [first, last) before the element pointed to by the iterator
3. Deletion Functions
iterator erase(iterator it): Deletes the element pointed to by the iterator in the vector
iterator erase(iterator first, iterator last): Deletes the elements within the range [first, last) in the vector
void pop_back(): Deletes the last element in the vector
void clear(): Clears all elements in the vector
4. Traversal Functions
reference at(int pos): Returns a reference to the element at position pos
reference front(): Returns a reference to the first element
reference back(): Returns a reference to the last element
iterator begin(): Returns a pointer to the head of the vector, pointing to the first element
iterator end(): Returns a pointer to the end of the vector, pointing to the position after the last element
reverse_iterator rbegin(): A reverse iterator, pointing to the last element
reverse_iterator rend(): A reverse iterator, pointing to the position before the first element
5. Judgment Functions
- bool empty() const: Determines whether the vector is empty. If it is, there are no elements in the vector.
6. Size Functions
int size() const: Returns the number of elements in the vector
int capacity() const: Returns the maximum number of elements the vector can currently hold
int max_size() const: Returns the maximum allowable number of vector elements
7. Other Functions
void swap(vector&): Swaps the data of two vectors of the same type
void assign(int n, const T& x): Sets the value of the first n elements in the vector to x
void assign(const_iterator first, const_iterator last): Sets the elements in the range [first, last) to the current vector elements
8. Clear at a Glance
>
push_back Adds a data to the end of the array
pop_back Removes the last data from the array
at Gets the data at the specified position
begin Gets the pointer to the head of the array
end Gets the pointer to the last unit +1 of the array
front Gets a reference to the head of the array
back Gets a reference to the last unit of the array
max_size Gets the maximum size of the vector
capacity The size currently allocated by the vector
size The size of the data currently in use
resize Changes the size of the data currently in use, filling with default values if it is larger than the current usage
reserve Changes the size of the space currently allocated by the vector
erase Deletes the data item pointed to by the pointer
clear Clears the current vector
rbegin Returns the English:
// Method Two #include <string.h> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int N=5, M=6; vector<vector<int> > obj(N, vector<int>(M)); // Define a 2D dynamic array with 5 rows and 6 columns for(int i=0; i< obj.size(); i++) // Output the 2D dynamic array { for(int j=0;j<obj[i].size();j++) { cout<<obj[i][j]<<" "; } cout<<"\n"; } return 0; }
Output result:
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
Original article link: http://blog.csdn.net/w_linux/article/details/71600574