C/C ++ string processing (4): STD: vector and STD: stringbuilder

Source: Internet
Author: User
C/C ++ string processing (4): STD: vector and STD: stringbuilder

Xu Shiwei
2008-3-28

Introduction

STD: stringbuilder is implemented based on STD: vector. Therefore, although STD: vector is discussed in this article, all conclusions are equally effective for STD: stringbuilder.

Implementation Overview

In short, STD: vector is a dynamic array that manages a linear, dynamically increasing memory.

How to accelerate STD: vector? Use Vector: Reserve

Before we estimate the size of a vector, we should call reserve (size) to pre-allocate the memory (size here is the estimated number of vector elements) before inserting data ).

Avoid inserting/deleting data from a vector (BEGIN ).

That is to say, we should try to use vector: insert (end (),...) Or vector: push_back/pop_back to add/delete data. Instead of using vector: insert (begin (),...) Operation. The push_front operation is not provided for the vector. There is only one reason: inserting/deleting data from the vector is inefficient.

If you need to insert a large amount of data at the beginning of the container (BEGIN), you can select STD: deque (which has the advantage of random vector access and efficient list insertion) or STD: list.

When can't STD: vector be used for STD: vector?

When the container needs to hold massive data and the number of elements is unpredictable, STD: vector cannot be used. All linear memory-based data structures (such as STD: vector, STD: string) encounter performance bottlenecks when handling massive data volumes.

Memory fragmentation

A typical problem with linear memory-based data structures (such as STD: vector, STD: string) Is that memory fragments are easily generated. After a large number of operations on STD: vector or STD: String, memory fragments will be severe.

STD: vector and allocator

We know that the prototype of STD: vector is:

template <class DataT, class AllocT = std::allocator<DataT> >
class vector;

So do we need to change alloct to GC Allocator like MAP/multimap, set/Multiset, list/slist, hashmap/hashmultimap, hashset/hashmultiset, and deque?

The answer is: no. GC allocator is helpful for improving small memory allocation. However, the data structure in the dynamic linear memory is invalid. In addition to STD: vector, a typical data structure is STD: string (STD: basic_string ).

Recommended reading
  • STD: deque-a Data Structure between STD: vector and STD: List, which can be randomly located, the performance of massive data is still very stable (in fact, its push_back/push_front performance is almost constant: O (1), unlike STD: vector, with the increase of elements, insert speed decreases sharply ).
  • STD: List-two-way linked list.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.