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 for C++

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

Share

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

This article mainly introduces how C++ uses std::vector container related knowledge, detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that everyone will read this C++ how to use std::vector container article will have some gains, let's take a look at it.

preface

Vector is essentially a C++ class, similar to arrays, but the advantage of vector is that it can be dynamically extended without considering its memory size.

Definition:

A vector is a Sequence Container that encapsulates a dynamically sized array. Like any other type of container, it can hold various types of objects. A vector can simply be thought of as a dynamic array capable of storing any type.

Features:

1. sequential series

The elements in a sequential container are sorted in strict linear order. You can access an element by its position in the sequence.

2. dynamic array

Fast direct access to any element in a sequence is supported, even through pointer arithmetic. Operation provides for relatively fast addition/deletion at the end of the sequence

Except for the operation of elements.

3. Allocator-aware

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

1. Vector constructor prototype:

vector data; //Use template class, default constructor

vector(data.begin(),data.end()); //copy the elements in the data [begin(),end()) interval to itself, note that it is a closed and open interval in mathematics

vector(n,elem); //copy n elem elements to itself

vector(const vector &data); //copy construct

#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

Development

Wechat

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

12
Report