C++ STL的基本基本原理

STL都是在内存的堆区分配的,但是其析构也是STL帮我们做好的,不用手动去delete。

1.vector

  逻辑地址连续的一片内存空间,当空间不足,重新申请新的地址空间,将原有的数据复制过去,而新的地址空间的大小C++没有规定,依赖于编译器的实现。在VC++中是原来的1.5倍,而g++中则是原来的2倍。

  关于这一点可以从简单的实验来验证,vector的capacity成员函数返回vector实际申请的空间大小,可以通过不断向vector中插入100个数据,观察capacity的大小变化情况来判断数据的整张情况。代码如下:

#include<iostream>
#include<vector>
using namespace std;
int main() {
	vector<int> vec;
	for (int i = 0; i<100; i++) {
		vec.push_back(i);
		cout << "capacity is " << vec.capacity() << endl;
	}
}

下图左右分别是在VS2015和g++ 4.8的运行结果图,可以发现证实了我们之前的说法:当内存空间不足时新分配的内存空间在VC++下是原来的1.5倍,在g++下是原来的2倍。

 

 

 

2.map

  底层红黑树,有较快的查找插入删除速度,均为O(lgn)时间复杂度。另外红黑树结构决定了其不能同时插入两个key值一样的节点,而是会被直接丢弃掉,也就是插不进去。这一点可以从其是平衡二叉树的性质看出来。

转载于:https://www.cnblogs.com/shenshenlei/p/5528426.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
神书-STL实现原理,对于强化数据结构-算法的程序员必备、必读书籍。The Best-Selling Programmer Resource–Now Updated for C++11 The C++ standard library provides a set of common classes and interfaces that greatly extend the core C++ language. The library, however, is not self-explanatory. To make full use of its components - and to benefit from their power - you need a resource that does far more than list the classes and their functions. The C++ Standard Library - A Tutorial and Reference, 2nd Edition describes this library as now incorporated into the new ANSI/ISO C++ language standard (C++11). The book provides comprehensive documentation of each library component, including an introduction to its purpose and design; clearly written explanations of complex concepts; the practical programming details needed for effective use; traps and pitfalls; the exact signature and definition of the most important classes and functions; and numerous examples of working code. The book focuses on the Standard Template Library (STL), examining containers, iterators, function objects, and STL algorithms. You will also find detailed coverage of strings, concurrency, random numbers and distributions, special containers, numerical classes, internationalization, and the IOStreams library. An insightful introduction to fundamental concepts and an overview of the library will help bring newcomers quickly up to speed. A comprehensive index will support the C++ programmer in his/her day-to-day life.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值