首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
您找到你想要的搜索结果了吗?
是的
没有找到

STL小结

STL就是Standard Template Library,标准模板库。这可能是一个历史上最令人兴奋的工具的最无聊的术语。从根本上说,STL是一些“容器”的集合,这些“容器”有list, vector,set,map等,STL也是算法和其它一些组件的集合。这里的“容器”和算法的集合指的是世界上很多聪明人很多年的杰作。是C++标准库的一个重要组成部分,它由Stepanov and Lee等人最先开发,它是与C++几乎同时开始开发的;一开始STL选择了Ada作为实现语言,但Ada有点不争气,最后他们选择了C++,C++中已经有了模板。STL又被添加进了C++库。1996年,惠普公司又免费公开了STL,为STL的推广做了很大的贡献。STL提供了类型安全、高效而易用特性的STL无疑是最值得C++程序员骄傲的部分。每一个C++程序员都应该好好学习STL。大体上包括container(容器)、algorithm(算法)和iterator(迭代器),容器和算法通过迭代器可以进行无缝连接。

01

C++ map 和 hashmap 区别

1. stl map is an associative array where keys are stored in sorted order using balanced trees. while hash_map is a hashed associated container, where keys are not stored in an ordered way. key, value pair is stored using a hashed function.        2. insertion and lookup takes ologn time in map, also performance would degrade as the key size increases. mainly balance operations on large key ranges would kill performance. while lookup is very efficient o(1) in hash_map.        3. map is useful where you want to store keys in sorted order, hash_map is used where keys order is not important and lookup is very efficient.        4. one more difference is map has the important property that inserting a new element into a map does not invalidate iterators that point to existing elements. erasing an element from a map also does not invalidate any iterators. performance would mostly be o(lgn) due to the implementation of a balanced tree. for map custom objects you would need at the minimum the following operators to store data in a map "<" ">" "==" and of course the other stuff for deep copy.

00
领券