Article directory
Part.I Attention
vector
Points to note when using :
- add citation
#include <vector>
- It can be called directly as an "array", but! If you want to modify the value in the function, you must add it
&
! ! !
Some tricks:
// 将 b 中的元素去重并从小到大排序
sort(b.begin(),b.end());
b.erase(unique(b.begin(),b.end()),b.end());
Part.II Function
Function list:
Function name | meaning |
---|---|
begin() |
Returns an iterator pointing to the first element in the container. |
end() |
Returns an iterator pointing to the position after the last element of the container, usually begin() used in conjunction with . |
rbegin() |
Returns an iterator pointing to the last element. |
rend() |
Returns an iterator pointing to the position preceding the position of the first element. |
size() |
Returns the actual number of elements. |
max_size() |
Returns the maximum number of elements. This is usually a large value, usually 232-1, so we rarely use this function. |
resize() |
Change the number of actual elements. |
capacity() |
Returns the current capacity. |
empty() |
Determine whether there is an element in the container, if there is no element, return true; otherwise, return false. |
reserve() |
Increase the capacity of the container. |
shrink _to_fit() |
Reduce memory to equal the size actually used by the current element. |
operator[] |
The [ ] operator is overloaded, and the elements in the vector container can be accessed or even modified through subscripts like accessing elements in an array. |
at() |
Elements are accessed using a bounds-checked index. |
front() |
Returns a reference to the first element. |
back() |
Returns a reference to the last element. |
data() |
Returns a pointer to the first element in the container. |
assign() |
Replace existing content with new elements. |
push_back() |
Adds an element to the end of the sequence. |
pop_back() |
Removes the element from the end of the sequence. |
insert() |
Inserts one or more elements at the specified positions. |
erase() |
Moves out an element or a block of elements. |
clear() |
All elements are removed and the container size becomes 0. |
swap() |
Swaps all elements of two containers. |
emplace() |
Generates an element directly at the specified position. |
emplace_back() |
Generates an element at the end of the sequence. |
Chap.I Constructor
vector()
: Create an empty vector- :
vector(int nSize)
Create a vector with nSize elements vector(int nSize,const t& t)
: Create a vector, the number of elements is nSize, and the value is t- :
vector(const vector&)
copy constructor vector(begin,end)
: Copy[begin,end)
the elements of another array in the range to the vector
Here is an example of calling the constructor:
// 定义了10个整型元素的向量,初始化为 1
vector<int> a(10,1);
// 定义一个二维 vector 向量,所有元素初始化为 1
vector<int> a(10,1);
vector<vector<int>> aa(10,a);
// 通过 int[] 构造并初始化 vector
int b[7] = {
1,2,3,4,5,9,8};
vector<int> a(b,b+7);
// 直接用数组构造
vector<int> a({
1,2,3,4,5,9,8});
Chap.II Add function
void push_back(const T& x)
: add an element X at the end of the vectoriterator insert(iterator it,const T& x)
: Add an element x before the iterator in the vector points to the elementiterator insert(iterator it,int n,const T& x)
: Add n identical elements x before the iterator in the vector points to the elementiterator insert(iterator it,const_iterator first,const_iterator last)
[first,last)
: Insert the data between another vector of the same type before the iterator in the vector points to the element
The following is a simple operation example:
vector<int> a(10,1); // 10个1
auto itr=a.begin()+3; // 得到第 3 个迭代器
a.insert(itr,2,8); // 在迭代器的位置处插入 2 个 8,a=[1 1 1 8 8 1 1 1 1 1 1 1]
Chap.III delete function
iterator erase(iterator it)
: Delete the element pointed to by the iterator in the vectoriterator erase(iterator first,iterator last)
: Delete[first,last)
elements in the vectorvoid pop_back()
: Delete the last element in the vectorvoid clear()
: Empty all elements in the vector
The following is a simple operation example:
auto tmp=a.erase(a.begin()+2,a.begin()+4);
a.pop_back(); // 删除最后一个元素
vector
The meaning of the first sentence: Two elements from the 3rd element to the 5th element (not included) are deleted , and the iterator of the 3rd element after deletion is returned.
Chap.IV traversal function
T at(int pos)
: Returns a reference to the element at position posT front()
: Returns a reference to the first elementT back()
: Returns a reference to the tail elementiterator begin()
: Returns the vector head pointer, pointing to the first elementiterator end()
: Returns the vector tail pointer, pointing to the next position of the last element of the vectorreverse_iterator rbegin()
: reverse iterator, pointing to the last elementreverse_iterator rend()
:Reverse iterator, pointing to the position before the first element
Notice:
- vector can
[]
be used to get the element value of a certain index, but it cannotpython
be sliced like that. end()
Returns the iterator after the last element,*(vec.end()-1)
which is the value of the last element
vector<int> b({
1,2,3,4,5,6,7,8});
cout<<b.front()<<" "<<b.back()<<endl; // 1 8
cout<<*b.begin()<<" "<<*(b.end()-1)<<endl; // 1 8
Chap.V judgment/size/other functions
bool empty() const
: Determine whether the vector is empty, if it is empty, there is no element in the vectorint size() const
: Returns the number of elements in the vectorint capacity() const
: Returns the maximum element value that the current vector can holdint max_size() const
: Returns the maximum allowable number of vector elementsvoid swap(vector&)
: Exchange the data of two vectors of the same typevoid assign(int n,const T& x)
: Set the value of the first n elements in the vector to xvoid assign(const_iterator first,const_iterator last)
:[first,last)
The element in the vector is set to the current vector element
cout<<b.empty()<<endl;
cout<<b.size()<<" "<<b.capacity()<<" "<<b.max_size()<<endl;
printVec(b);
b.assign(6,10); // 指定 b 中的元素为6个10
printVec(b);
b.assign(b.begin()+1,b.end()-1); // 去掉 b 第一个元素和最后一个元素
printVec(b);
Part.III Code
The following is Vector
the code used in the learning process (in fact, it is a summary of the above code):
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
int main()
{
auto printVec=[](vector<int> vec) {
for(auto& c:vec) cout << c << " ";cout << endl;};
// 构造
vector<int> a(10,1);
printVec(a);
// 插入
auto itr=a.begin()+3;
auto tmp = a.insert(itr,2,8);
cout << *(tmp+1)<<endl;
printVec(a);
// 删除
tmp=a.erase(a.begin()+2,a.begin()+4);// 删除的元素不包含 itr+1
cout <<* tmp<<endl;
printVec(a);
a.pop_back();
printVec(a);
// 修改
a[3]=3;
a.push_back(4);
printVec(a);
// 遍历
vector<int> b({
1,2,3,4,5,6,7,8});
cout<<b.front()<<" "<<b.back()<<endl;
cout<<*b.begin()<<" "<<*(b.end()-1)<<endl;
// 其他函数
cout<<b.empty()<<endl;
cout<<b.size()<<" "<<b.capacity()<<" "<<b.max_size()<<endl;
printVec(b);
b.assign(6,10); // 指定 b 中的元素为6个10
printVec(b);
b.assign(b.begin()+1,b.end()-1); // 去掉 b 第一个元素和最后一个元素
printVec(b);
getchar();
return 0;
}
The output is as follows:
1 1 1 1 1 1 1 1 1 1
8
1 1 1 8 8 1 1 1 1 1 1 1
8
1 1 8 1 1 1 1 1 1 1
1 1 8 1 1 1 1 1 1
1 1 8 3 1 1 1 1 1 4
1 8
1 8
0
8 8 2305843009213693951
1 2 3 4 5 6 7 8
10 10 10 10 10 10
10 10 10 10