Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use vector in C++

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use vector in C++. I hope you will get something after reading this article. Let's discuss it together.

First, what is vector?

Vector (vector) is a sequential container that encapsulates dynamic size arrays. Can store various types of objects (Note: all objects in a container must be of the same type). You can think of vector as a dynamic array that can hold any type of array and can add and delete data (because it is dynamic, it saves space compared to arrays).

The author of C++ primer said that in actual programming, we as programmers should avoid using low-level arrays and pointers and instead use high-level vector and iterators.

Second, container characteristics 1, sequence sequence

The elements in the order container are sorted in strict linear order. You can access the corresponding element through its position in the sequence.

2, dynamic array

Supports quick and direct access to any element in the sequence, which can be done through a pointer. Provides the operation to add / remove elements at the end of the sequence.

3, aware of memory allocator

The container uses a memory allocator object to handle its storage requirements dynamically.

Third, the implementation of common basic functions.

Vector (): constructor to create an empty vector

Vector (int nSize): create a vector with nSize as the number of elements

Vector (int nSize,const t & t): create a vector with the number of elements nSize and the value t

Vector (begin,end): copy elements from another array in the [begin,end) interval to vector

~ vector (): destructor that destroys container objects and reclaims all allocated memory

Void push_back (const T & x): add an element X to the tail of the vector

Iterator insert (iterator it,const T & x): add an element x before the iterator points to the element in the vector

Iterator insert (iterator it,int NMagne Const T & x): add n identical elements x before the iterator points to the element in the vector.

Iterator insert (iterator it,const_iterator first,const_iterator last): data between [first,last) in a vector where the iterator points to the element and inserts another vector of the same type.

Iterator erase (iterator it): delete an iterator pointing to an element in a vector

Iterator erase (iterator first,iterator last): delete elements in [first,last) in a vector

Void pop_back (): deletes the last element in the vector

Void clear (): clear all elements in the vector, that is, the size values are all 0, but the storage space has not changed (freed)

Reference at (int pos): returns a reference to the pos location element

Reference front (): returns a reference to the first element

Reference back (): returns a reference to the tail element

Iterator begin (): returns the vector header pointer to the first element

Iterator end (): returns the vector tail pointer, pointing to the next position of the last element of the vector

Bool empty () const: determines whether the vector is empty. If so, there are no elements in the vector.

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: returns the maximum allowed number of vector elements

Void swap (vector&): exchanging data of two vectors of the same type, exchanging the contents of two containers, involving storage space allocation

Void assign (int NMagne Const T & x): sets the value of the first n elements in the vector to x

Void assign (const_iterator first,const_iterator last): the element in [first,last) in the vector is set to the current vector element.

Fourth, basic usage 1, header file

Vector is the data structure in the C++ standard library STL, so to use vector, you need to add:

# include using namespace std;2, create and use

Create a vector object

/ / create a normal one-dimensional dynamic array vector vec;// create a two-dimensional dynamic array vector vec2;// trailing insert digits: vec.push_back (1); / / trailing delete digits: vec.pop_back (); / / use the subscript to access the array, with the subscript starting at 0 cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report