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 Standard template Library in C++

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

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you how to use the vector standard template library in C++ related knowledge points, the content is detailed, the logic is clear, I believe most people are too aware of this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.

One: introduction

Vector is the C++ standard template library, is a container, the bottom is an array, for continuous memory.

The namespace is std, and the header file is note: no

When vector stores data, it allocates a storage space. If it continues to store, the allocated space is full, it will allocate a larger piece of memory, copy the original data, and continue to store it. This performance will also be lost to some extent.

Second, common operations

Capacity:

A.vector size: vector.size ()

Actual amount of memory occupied by b.vector: vector.capacity ()

Modify:

a. Add element to the tail: vector.push_back ()

b. Tail delete element: vector.pop_back ()

c. Exchange two vector elements: vector.swap ()

d. Clear the vector element: vector.clear ()

e. Delete the specified element: vector.erase (it)

Iterator:

A.vector start pointer: vector.begin ()

B.vector tail pointer: vector.end () Note: the next position of the last element, similar to NULL, is not the last element of the container

Access elements:

a. Subscript access: vector [1] / / does not check whether it is out of bounds

B.at method access: vector.at (1) / / automatically checks whether it is out of bounds, and throws an exception if it is out of bounds

c. Access the first element: vector.front ()

d. Access the last element: vector.back ()

Three: storage

Simple storage

/ / Storage mode 1 vector v1 (10); for (int item0; I

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