In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Xiaobian to share with you how to use the STL standard library std::vector in C++, I believe most people still don't know how to use it, so share this article for your reference, I hope you have a lot of harvest after reading this article, let's get to know it together!
1. profile
vector is a sequence container that represents an array that can vary in size.
Like arrays, vectors use contiguous storage locations for elements, which means that their elements can also be accessed using offsets on regular pointers to their elements, and as efficiently as in arrays. But unlike arrays, their size can change dynamically, and containers handle their storage automatically.
Internally, vectors use a dynamically allocated array to store their elements. This array may need to be reallocated to increase the size when new elements are inserted, which means allocating a new array and moving all elements into it. This is a relatively expensive task in terms of processing time, so vectors are not reassigned each time an element is added to the container.
Conversely, a vector container can allocate some extra storage space to accommodate possible growth, so the actual capacity of the container may be greater than the strictly required storage capacity (i.e., the size of the container). Libraries can implement different growth strategies to balance memory usage and reallocation, but in any case reallocation should only occur at logarithmic growth intervals of size so that inserting a single element at the end of the vector provides amortized constant time complexity (see Backward).
As a result, vectors consume more memory than arrays in exchange for the ability to manage storage and grow dynamically in an efficient manner.
Compared to other dynamic sequence containers (deques, lists, and forward_lists), vectors can access their elements very efficiently (just like arrays) and add or delete elements from their ends relatively efficiently. Operations that involve inserting or deleting elements beyond the end perform worse than others, and iterators and references are less consistent than lists and forward lists.
2. #include #include using namespace std;void main(){ vectormyvt; //Define template class objects myvt.reserve(4); //Set size 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.