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

STL vector usage

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Memory management in embedded development is a very important part. How to use vector efficiently can not only expand the storage space quickly, but also maximize the use of storage space, and avoid the out-of-bounds problem of C array.

Scheme

Vector provides multiple constructors

1 vector vec

Vec.reserve (10)

The default constructor does not allocate any memory space until it calls reserve (10) to allocate 10 units of storage space. If the subsequent storage space is not enough, it will reallocate a large enough memory to avoid insertion failure.

2 vector vec (10)

Add elements. The number of elements in vec is 11.

Test code

# include

Int TestVector ()

{

Int v1 [10] = {1,2,3,4,5,6,7,8,9,10}

Std::vector v2

V2.reserve (10)

For (int I = 0; I < 10; iTunes +)

{

V2.push_back (v1 [I])

}

Std::vector::iterator iter = v2.begin ()

While (iter! = v2.end ())

{

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

Servers

Wechat

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

12
Report