当前位置:网站首页>How to use vector_ How to use vector pointer
How to use vector_ How to use vector pointer
2022-07-06 23:57:00 【Java architects must see】
One 、 What is? vector?
vector (Vector) Is a sequential container that encapsulates dynamic size arrays (Sequence Container). Like any other type of container , It can hold all kinds of objects . We can simply think that , A vector is a dynamic array that can hold any type of data .
Two 、 Container characteristics
1. Sequential sequence
The elements in the order container are ordered in strict linear order . You can access the corresponding element by its position in the sequence .
2. The dynamic array
It supports fast and direct access to any element in the sequence , You can even do this through pointer arithmetic . Provides a relatively fast way to add... At the end of a sequence / Delete element operation .
3. Memory allocator aware (Allocator-aware)
The container uses a memory allocator object to dynamically handle its storage requirements .
3、 ... and 、 Basic function implementation
1. Constructors
vector(): Create an empty vector vector(int nSize): Create a vector, The number of elements is nSize vector(int nSize,const t& t): Create a vector, The number of elements is nSize, And the values are t vector(const vector&): copy constructor vector(begin,end): Copy [begin,end) The element of another array in the interval to vector in
2. Add function
void push_back(const T& x): Add an element to the tail of the vector X emplace_back: Add an element to the tail of the vector X The following two are equal
std::vector<Vertex> MM;
MM.push_back(Vertex(a));I only heard the voice of the architect from the architect's office : The mountains and rocks are really small , Bats fly in the temple at dusk . Who's going to answer the first couplet or the second couplet ?
This code is created by Java Architects must see the net - Structure Sorting
std::vector<Vertex> MM;
MM.emplace_back(a);iterator insert(iterator it,const T& x): An element is added before the iterator points to the element in the vector x iterator insert(iterator it,int n,const T& x): The iterator in the vector is added before the element n The same elements x iterator insert(iterator it,const_iterator first,const_iterator last): Vector in which the iterator inserts another vector of the same type before pointing to the element [first,last) Data between
3. Delete function
iterator erase(iterator it): Delete the iterator pointing element in the vector iterator erase(iterator first,iterator last): Delete the vector [first,last) Medium element void pop_back(): Delete the last element in the vector void clear(): Empty all the elements in the vector
4. Traversal function
reference at(int pos): return pos References to location elements reference front(): Returns the reference to the first element reference back(): Returns the reference to the tail element iterator begin(): Returns the vector header pointer , Point to first element iterator end(): Return vector tail pointer , Point to the next position of the last element of the vector reverse_iterator rbegin(): reverse iterator , Point to last element reverse_iterator rend(): reverse iterator , Point to the position before the first element
5. Judgment function
bool empty() const: Judge whether the vector is empty , If it is empty , Then there are no elements in the vector
6. The size function
int size() const: Returns the number of elements in a vector int capacity() const: Returns the maximum element value that the current vector can hold int max_size() const: Return the maximum allowed vector The number of elements
7. Other functions
void swap(vector&): Exchange data between two vectors of the same type void assign(int n,const T& x): Set the front of the vector n The value of each element is x void assign(const_iterator first,const_iterator last): Vector [first,last) Set the element in the current vector element
8. Look at it clearly
1.push_back Add a data at the end of the array
2.pop_back Remove the last data of the array
3.at Get the data of the number position
4.begin Get a pointer to the array header
5.end Get the last cell of the array +1 The pointer to
6.front Get a reference to the array header
7.back Get a reference to the last cell of the array
8.max_size obtain vector The biggest is how big
9.capacity At present vector The size of the allocation
10.size The size of the data currently used
11.resize Change the size of the currently used data , If it's bigger than what's currently in use , Fill in the default value
12.reserve To change the current vecotr The size of the space allocated
13.erase Delete the data item that the pointer points to
14.clear Clear the current vector
15.rbegin take vector The reverse start pointer returns ( In fact, it is the original end-1)
16.rend take vector The end pointer of the reverse construct returns ( In fact, it is the original begin-1)
17.empty Judge vector Is it empty
18.swap And another vector Exchange data
That's the end of today's article , Thank you for reading ,Java Architects must see I wish you a promotion and a raise , Good luck every year .
边栏推荐
- DAY SIX
- Quickly use various versions of PostgreSQL database in docker
- GEO数据挖掘(三)使用DAVID数据库进行GO、KEGG富集分析
- 《LaTex》LaTex数学公式简介「建议收藏」
- Local deployment Zeppelin 0.10.1
- Wasserstein Slim GAIN with Gradient Penalty(WSGAIN-GP)介绍及代码实现——基于生成对抗网络的缺失数据填补
- 【OFDM通信】基于深度学习的OFDM系统信号检测附matlab代码
- I've been laid off, and I'll lose money for everything. The days when I once made a monthly salary of 20000 are not coming back
- 自动化测试工具Katalon(Web)测试操作说明
- pinia 模块划分
猜你喜欢

Introduction au GPIO

GEO数据挖掘(三)使用DAVID数据库进行GO、KEGG富集分析

The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance

DAY SIX

I've been laid off, and I'll lose money for everything. The days when I once made a monthly salary of 20000 are not coming back

1000字精选 —— 接口测试基础

pytest多进程/多线程执行测试用例

Résumé des connaissances de gradle

leetcode:236. 二叉树的最近公共祖先

达晨史上最大单笔投资,今天IPO了
随机推荐
达晨史上最大单笔投资,今天IPO了
[automated testing framework] what you need to know about unittest
数据运营平台-数据采集[通俗易懂]
Quickly use various versions of PostgreSQL database in docker
短链的设计
DAY TWO
[OFDM communication] OFDM system signal detection based on deep learning with matlab code
Gold three silver four, don't change jobs
内网穿透zerotier 外网(手机、电脑等)访问内网设备(树莓派、NAS、电脑等)
Newsletter L Huobi ventures is in-depth contact with genesis public chain
[boutique] Pinia Persistence Based on the plug-in Pinia plugin persist
app通用功能測試用例
DevOps可以帮助减少技术债务的十种方式
Wasserstein Slim GAIN with Gradient Penalty(WSGAIN-GP)介绍及代码实现——基于生成对抗网络的缺失数据填补
为什么完全背包要用顺序遍历?简要解释一下
Experiment 6: installing eve-ng
Pytest multi process / multi thread execution test case
PostgreSQL使用Pgpool-II实现读写分离+负载均衡
DAY SIX
英国都在试行4天工作制了,为什么BAT还对996上瘾?