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 the std::vector container of C++

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Today, the editor will share with you the relevant knowledge points about how to use C++ 's std::vector container. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Preface

Vector is essentially a class of C++, similar to arrays, but vector has the advantage of being scalable dynamically, regardless of its memory size.

Definition:

A Vector is a sequential container (Sequence Container) that encapsulates an array of dynamic sizes. Like any other type of container, it can hold various types of objects. You can simply think of a vector as a dynamic array that can hold any type of array.

Properties:

1. Sequential 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.

two。 Dynamic array

Supports quick and direct access to any element in the sequence, and can even be done through pointer arithmetic. Relatively quickly add / delete at the end of the sequence

The operation of removing elements.

3. Memory allocator aware (Allocator-aware)

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

1. The constructor prototype of vector:

Vector data; / / use template class, default constructor

Vector (data.begin (), data.end ()); / / copy the elements in the [begin (), end ()) interval of data to itself. Note that it is the pre-closed and post-open interval in mathematics.

Vector (nPowerelem); / / copy n elem elements to yourself

Vector (const vector & data); / / copy construction

# include # include using namespace std; void fun_print_vector (std::vector&data) {/ / std::vector::iterator it; / / can be replaced by auto. For (auto it_data = data.begin (); it_data! = data.end (); it_data++) {std::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

Internet Technology

Wechat

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

12
Report