C/C++教程

C++ STL

本文主要是介绍C++ STL,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Sequence constainers(array and linked list)

–vector , deque, list , array

Associative Containers(binary tree)

–set, multiset
–map, multimap

Unordered Containers(hash table)

–unordered set/ multiset
–unordered map/multimap

在这里插入图片描述

vector

vector<int> vec(10,0) //创建容量为10的vector,并初始化为0
vector<int> vec = {1,4,8}
vector<int> vec2(vec)  //copy constructor, vec2: {1,4,8}
vec.push_back(4);
vec.pop_back();  //移除最后一个元素
vec.size();   //vector元素数量大小
vec.clear();   //清空
//vec.front();  //返回第一个元素 == vec[0]
------------------------------------------------------------------------
vec.erase(vec.begin());  //删除迭代器所指向的向量元素
vec.insert(vec.begin(),100); //把100插入到向量中迭代器所指的位置
------------------------------------------------------------------------
for(vector<int>::iterator it = vec.begin(); it != vec.end(); it++)
	cout << *it <<endl;
sort(vec.begin(),vec.end());
for(int i : vec)  cout << i << endl;
  • fast insert/remove at the end: O(1)
  • slow insert/remove at the begin or int the middle: O(n)
  • slow search: O(n)

deque

deque<int> deq  = {3,4,6};
deq.push_front(2);
deq.push_back(9);

  • fast insert/remove at the begining and the end O(1);
  • slow insert/remove in the middle: O(n)
  • slow search: O(n)

list

	list<int> li = {1,2,3};
	li.push_back(6);
	li.push_front(0);
  • fast insert/remove at any place: O(1)
  • slow serarch:O(n)
  • no random access, no [] operator
这篇关于C++ STL的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!