STL哪些事

Verify Vector capacity
# cat vector_capacity.cpp
#include <iostream>
#include <new>
#include <vector>


int main() {

std::vector<int> vec;
for(int i=0;i<30;++i)
{
        vec.push_back(i);
        std::cout<<"i:" << i  <<",capacity = "<<vec.capacity() << std::endl;
}

}

# ./a.out
i:0,capacity = 1
i:1,capacity = 2
i:2,capacity = 4
i:3,capacity = 4
i:4,capacity = 8
i:5,capacity = 8
i:6,capacity = 8
i:7,capacity = 8
i:8,capacity = 16
i:9,capacity = 16
i:10,capacity = 16
i:11,capacity = 16
i:12,capacity = 16
i:13,capacity = 16
i:14,capacity = 16
i:15,capacity = 16
i:16,capacity = 32
i:17,capacity = 32
i:18,capacity = 32
i:19,capacity = 32
i:20,capacity = 32
i:21,capacity = 32
i:22,capacity = 32
i:23,capacity = 32
i:24,capacity = 32
i:25,capacity = 32
i:26,capacity = 32
i:27,capacity = 32
i:28,capacity = 32
i:29,capacity = 32

STL::sort
// sort algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector

bool myfunction (int i,int j) { return (i<j); }

struct myclass {
  bool operator() (int i,int j) { return (i<j);}
} myobject;

int main () {
  int myints[] = {32,71,12,45,26,80,53,33};
  std::vector<int> myvector (myints, myints+8);               // 32 71 12 45 26 80 53 33

  // using default comparison (operator <):
  std::sort (myvector.begin(), myvector.begin()+4);           //(12 32 45 71)26 80 53 33

  // using function as comp
  std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)

  // using object as comp
  std::sort (myvector.begin(), myvector.end(), myobject);     //(12 26 32 33 45 53 71 80)

  // print out content:
  std::cout << "myvector contains:";
  for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}
# ./a.out
myvector contains: 12 26 32 33 45 53 71 80

升序:sort(s.begin(),s.end(),less<data_type>())
降序:sort(s.begin(),s.end(),greater<data_type>())

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;


int main()
{
	vector<int> s = {1,9,2,8,3,7,4,6,5};
	sort(s.begin(),s.end(),less<int>());
	for(auto &num :s)
	{
		cout<<num<<"   ";
	}
	cout<<endl;
	
	sort(s.begin(),s.end(),greater<int>());
	for(auto &num :s)
	{
		cout<<num<<"   ";
	}
}
vector vs list
vector

向量(vector)是一个封装了动态大小数组的顺序容器。是一个连续存储的容器,在堆上分配空间。拥有一段连续的空间,并且起始地址不变。可以简单的认为,vector就是一个能够存放任意类型的动态数组。

vector底层以数组的方式实现。以两倍容量增长方式实现容量增长。

vector 增加(插入)新元素时,如果未超过当时的容量,则还有剩余空间,那么直接添加到最后(插入指定位置),然后调整迭代器。如果没有剩余空间了,则会重新配置原有元素个数的两倍空间,然后将原空间元素通过复制的方式初始化新空间,再向新空间增加元素,最后析构并释放原空间,之前的迭代器会失效。

它的实现方式,决定了它能够高效的进行数据访问。

性能: 访问:O(1)

list

list是一个线性链表结构,它的数据由若干个节点构成,每一个节点都包括一个信息块(即实际存储的数据)、一个前驱指针和一个后驱指针。它无需分配指定的内存大小且可以任意伸缩,这是因为它存储在非连续的内存空间中,并且由指针将有序的元素链接起来。

由于其结构的原因,list 随机检索的性能非常的不好,因为它不像vector那样直接找到元素的地址,而是要从头一个一个的顺序查找,这样目标元素越靠后,它的检索时间就越长。检索时间与目标元素的位置成正比。

虽然随机检索的速度不够快,但是它可以迅速地在任何节点进行插入和删除操作。因为list 的每个节点保存着它在链表中的位置,插入或删除一个元素仅对最多三个元素有所影响,不像vector 会对操作点之后的所有元素的存储地址都有所影响,这一点是vector 不可比拟的。

SORT

partial_sortsorts the first N elements of a range(function template)
stable_sortsorts a range of elements while preserving order between equal elements(function template)
ranges::sort (C++20)sorts a range into ascending order (niebloid)

The C++ standard requires that a call to sort performs O(N log N) comparisons when applied to a range of N elements.[4] In previous versions of C++, such as C++03, only average complexity was required to be O(N log N).[5] This was to allow the use of algorithms like (median-of-3) quicksort, which are fast in the average case, indeed significantly faster than other algorithms like heap sort with optimal worst-case complexity, and where the worst-case quadratic complexity rarely occurs.[6] The introduction of hybrid algorithms such as introsort allowed both fast average performance and optimal worst-case performance, and thus the complexity requirements were tightened in later standards.

sort and stable_sort

sort is not stable: equivalent elements that are ordered one way before sorting may be ordered differently after sorting. stable_sort ensures stability of result at expense of worse performance (in some cases), requiring only quasilinear time with exponent 2 – O(n log2 n) – if additional memory is not available, but linearithmic time O(n log n) if additional memory is available.[8] This allows the use of in-place merge sort for in-place stable sorting and regular merge sort for stable sorting with additional memory.

STL中的sort并非只是普通的快速排序,除了对普通的快速排序进行优化,它还结合了插入排序和堆排序。根据不同的数量级别以及不同情况,能自动选用合适的排序方法。当数据量较大时采用快速排序,分段递归。一旦分段后的数据量小于某个阀值,为避免递归调用带来过大的额外负荷,便会改用插入排序。而如果递归层次过深,有出现最坏情况的倾向,还会改用堆排序。

Introsort

Introsort or introspective sort (内观排序) is a hybrid sorting algorithm that provides both fast average performance and (asymptotically) optimal worst-case performance. It begins with quicksort, it switches to heapsort when the recursion depth exceeds a level based on (the logarithm of) the number of elements being sorted and it switches to insertion sort when the number of elements is below some threshold. This combines the good parts of the three algorithms, with practical performance comparable to quicksort on typical data sets and worst-case O(n log n) runtime due to the heap sort. Since the three algorithms it uses are comparison sorts, it is also a comparison sort.

unordered_map

  • unordered_map是一个将key和value关联起来的容器,它可以高效的根据单个key值查找对应的value。
  • key值应该是唯一的,key和value的数据类型可以不相同。
  • unordered_map存储元素时是没有顺序的,只是根据key的哈希值,将元素存在指定位置,所以根据key查找单个value时非常高效,平均可以在常数时间内完成。
  • unordered_map查询单个key的时候效率比map高,但是要查询某一范围内的key值时比map效率低。
  • 可以使用[]操作符来访问key值对应的value值。
c++中map与unordered_map的区别
  • 运行效率方面:unordered_map最高,而map效率较低但 提供了稳定效率和有序的序列。
  • 占用内存方面:map内存占用略低,unordered_map内存占用略高,而且是线性成比例的。

map
优点:有序性,这是map结构最大的优点,其元素的有序性在很多应用中都会简化很多的操作。红黑树,内部实现一个红黑书使得map的很多操作在lgn的时间复杂度下就可以实现,因此效率非常的高。
缺点:空间占用率高,因为map内部实现了红黑树,虽然提高了运行效率,但是因为每一个节点都需要额外保存父节点,孩子节点以及红/黑性质,使得每一个节点都占用大量的空间
适用处:对于那些有顺序要求的问题,用map会更高效一些。

unordered_map
优点:内部实现了哈希表,因此其查找速度是常量级别的。
缺点:哈希表的建立比较耗费时间
适用处:对于查找问题,unordered_map会更加高效一些,因此遇到查找问题,常会考虑一下用unordered_map

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值