Resource And Memory

本文深入探讨STL中的特殊容器,如内置数组、array、bitset和vector等,以及资源管理指针如unique_ptr、shared_ptr和weak_ptr。同时,介绍了内存管理的细节,包括默认分配器、分配器特性和未初始化内存的处理。这些工具在处理不同类型的数据和原始内存时提供了灵活性和效率。
摘要由CSDN通过智能技术生成

0.导读

• Introduction

• Almost Containers(拟容器)
array ; bitset ; vector<bool> ; Tuples

• Resource Management Pointers
unique_ptr ; shared_ptr ; weak_ptr

• Allocators
The Default Allocator; 
Allocator Traits; 
Pointer Traits; 
Scoped Allocators

• The Garbage Collection Interface

• Uninitialized Memory
Temporary Buffers; raw_storage_iterator

• Advice

1.Introduction

The STL (Chapter 31, Chapter 32, Chapter 33) is the most highly structured and 
general part standard-library facilities for the management and manipulation 
of data. This chapter presents facilitiesthat are more specialized or deal with 
raw memory (as opposed to typed objects).

STL(第31章、第32章、第33章)是标准库中最具高度结构化和部分用于管理和操作数据的通用性工具。
本章将介绍更特殊化的或处理原始内存的工具(与类型化对象相反).

2."almost containers"

The standard library provides several containers that don’t fit perfectly into the 
STL framework (§31.4, §32.2, §33.1). Examples are built-in arrays, array , and 
string. I sometimes refer to those as "almost containers" (§31.4), but that is 
not quite fair: they hold elements, so they are containers,but each has 
restrictions or added facilities that make them awkward in the context of the 
STL.Describing them separately also simplifies the description of the STL.

标准库提供了几个不完全适合STL框架的容器(§31.4, §32.2, §33.1). 
例如内置数组、数组和字符串。我有时把它们称为“几乎是容器”(§31.4),
但这不太公平: 它们包含元素,所以它们是容器,但每一个都有限制或增加的
方法,这使得它们在STL环境中显得尴尬。单独描述它们也简化了STL的描述。
                                                                   Almost Containers
    T[N]Built-in array: a fixed-size continuously allocated sequence of N elements of type T ; implicitly converts to a T∗

内置阵列:一个固定大小的连续分配的N序列

T型元素;隐式转换为T∗

array<T,N>A fixed-size continuously allocated sequence of N elements of type T ; like the built-in array, but with most problems solved

N个元素的固定大小连续分配序列

T型;与内置阵列类似,但大多数问题都已解决

bitset<N>A fixed-size sequence of N bitsN位的固定大小序列
vector<bool> A sequence of bits compactly stored in a specialization of vector在向量的特殊化中紧凑地存储的位序列
pair<T,U>Two elements of types T and UT型和U型两种元素
tuple<T...>A sequence of an arbitrary number of elements of arbitrary types任意数目的任意类型元素的序列
basic_string<C> 任意类型的任意数量的元素序列C类型的字符序列;提供字符串操作任意类型的任意数量的元素序列C类型的字符序列;提供字符串操作
valarray<T>An array of numeric values of type T ; provides numeric operationsT型数值数组;提供数字操作
Why does the standard library provide so many containers? They serve common 
but different (often overlapping) needs. If the standard library didn’t provide
them, many people would have to design and implement their own. For example:
• pair and tuple are heterogeneous; all other containers are homogeneous 
(all elements are of the same type).
• array , vector , and tuple elements are contiguously allocated; 
forward_list and map are linked structures.
• bitset and vector<bool> hold bits and access them through proxy objects; 
all other standardlibrary containers can hold a variety of types and access 
elements directly.
• basic_string requires its elements to be some form of character and to 
provide string manipulation, such as concatenation and locale-sensitive 
operations (Chapter 39) and valarray requires its elements to be numbers 
and to provide numerical operations.


All of these containers can be seen as providing specialized services needed 
by large communities of programmers. No single container could serve all of 
these needs because some needs are contradictory, for example,"ability to grow"
vs. "guaranteed to be allocated in a fixed location," and
"elements do not move when elements are added" vs. "contiguously allocated." 
Furthermore, a very general container would imply overhead deemed unacceptable 
for individual containers.


为什么标准库提供这么多容器?他们提供普通的服务但需求不同(往往重叠),如果标准库没有提供
很多人将不得不设计和实现他们自己的. 例如:
• 成对和元组是异质的;所有其他容器都是同质的(所有元件均为同一类型);
• array、vector和tuple是连续分配的,forward_list和map是链接结构;
• bitset和vector<bool> 含有比特们,并且通过代理对象访问这些比特,所有其他标准库容器都
可以容纳各种类型和访问权限并直接使用元素.
• basic_string要求其元素是某种形式的字符,并且提供字符串操作,例如连接和区分区域设置操
作(第39章),valarray要求其元素为数字并提供数字运算.


所有这些容器都可以被视为大量程序员需要的专门话的服务. 没有一个容器可以应付所有的这些需
求,因为有些需求之间是相互矛盾的,例如"增长能力"vs"保证在固定点分配",以及"元素添加"与"连
续分配"时,元素不会移动。此外,对于单个容器而言,一个非常通用的容器意味着不可接受的开销.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值