C++ —— 数据类型 ::容器类

本篇文章内容总结自:http://www.cplusplus.com/reference/stl/

1. std::array——固定长度的顺序容器

array是固定长度的,所以它不能动态扩展或收缩。

template < class T, size_t N > class array;

相关操作如下:
在这里插入图片描述

2. std::deque——双向队列

template < class T, class Alloc = allocator<T> > class deque;

在这里插入图片描述在这里插入图片描述

3. std::forward_list——前向列表,保留指向下一个元素的链接

前向列表,允许在任何位置进行插入和擦除操作,但无法通过位置直接访问元素。

  1. forward_list容器和list容器之间的主要设计差异在于:
  • forward_list在内部仅保留指向下一个元素的链接;
  • list每个元素上保留两个链接:一个指向下一个元素,一个指向上一个元素,从而实现高效 双向迭代,但每个元素消耗更多的存储空间,并且插入和删除元素的时间开销略高。
  • 尽管forward_list对象只能向前迭代,但它们比list对象更高效。
  1. 与其他基本标准序列容器(数组,向量和双端队列)相比:
    forward_list通常在插入,提取和移动容器内任何位置的元素方面表现更好,因此在诸如排序算法之类的大量使用这些元素的算法中也表现更好。
template < class T, class Alloc = allocator<T> > class forward_list;

在这里插入图片描述在这里插入图片描述

4. std::list——双向链接列表

双向链接列表。

template < class T, class Alloc = allocator<T> > class list;

在这里插入图片描述在这里插入图片描述

5. std::map——键值对,pair,对数据自动排序

template < class Key,                                     // map::key_type
           class T,                                       // map::mapped_type
           class Compare = less<Key>,                     // map::key_compare
           class Alloc = allocator<pair<const Key,T> >    // map::allocator_type
           > class map;

其中map中会用到一种数据形式pair,用来链接两个不同类型的数据。
例如:

std::map<char,int> mymap;
mymap.insert ( std::pair<char,int>('a',100) );

在这里插入图片描述在这里插入图片描述

6. std::multimap——具有相同键的元素序列,自动排序

multimap 中会出现具有相同键的元素序列,元素是按键的升序排列的,键相同的元素的顺序和它们输入的顺序相反。

template < class Key,                                     // multimap::key_type
           class T,                                       // multimap::mapped_type
           class Compare = less<Key>,                     // multimap::key_compare
           class Alloc = allocator<pair<const Key,T> >    // multimap::allocator_type
           > class multimap;

在这里插入图片描述

7.std::queue——先进先出队列

先进先出

template <class T, class Container = deque<T> > class queue;

在这里插入图片描述

8. std::priority_queue——有序(大顶堆/小顶堆)队列

是一个有序的队列,队列头部的元素优先级最高。其中第三个参数是定义优先级的参数,若为std::less,则最大值将在头部,如果为greater,则最小元素会排在队列最前面。
底层就是采用堆来组织数据存储的,(这里的堆不是内存分区中的栈和堆,是一种以二叉树的方式存储的数据结构)。
大顶堆中根节点是整个树中最大的值,当pop出根节点后,堆会重新排序使得,现在的根节点永远是树中最大的值。
同理,小顶堆中根节点是整个树中最小的值。

template <class T, class Container = vector<T>,
  class Compare = less<typename Container::value_type> > class priority_queue;

在这里插入图片描述

class Solution {
public:
    vector<int> getLeastNumbers(vector<int>& arr, int k) {
        priority_queue<int,vector<int>,std::greater<int>> queque_arr;
        vector<int> ans;
        for(int ar:arr)
        {
            queque_arr.push(ar);
        }
        for(int i=0;i<k;i++)
        {
            ans.push_back(queque_arr.top());
            queque_arr.pop();
        }
        return ans;
    }
};

9. set——根据唯一的value值进行严格排序

set/multiset是以rb_tree为底层机构,因此有元素自动排序的特性。
存储同一数据类型的数据类型,在set中每个元素的值都唯一,而且系统能根据元素的值自动进行排序。set中数元素的值不能直接被改变,但是可以被插入或删除。

template < class T,                        // set::key_type/value_type
           class Compare = less<T>,        // set::key_compare/value_compare
           class Alloc = allocator<T>      // set::allocator_type
           > class set;

在这里插入图片描述

10. multiset——根据value严格排序好的集合,并且允许有相同的元素

template < class T,                        // multiset::key_type/value_type
           class Compare = less<T>,        // multiset::key_compare/value_compare
           class Alloc = allocator<T> >    // multiset::allocator_type
           > class multiset;

在这里插入图片描述使用示例

class KthLargest {
private:
    multiset<int> set_multi;
    int kk;
public:

    KthLargest(int k, vector<int>& nums) {
        kk=k;
        for(int num:nums)
        {
            set_multi.insert(set_multi.end(),num);
            if(set_multi.size()>k)
            {
                set_multi.erase(set_multi.begin());
            }
        }
    }
    
    int add(int val) {
        set_multi.insert(set_multi.end(),val);
        if(set_multi.size()>kk)
        {
            set_multi.erase(set_multi.begin());
        }
        
        return *set_multi.begin();


    }
};

/**
 * Your KthLargest object will be instantiated and called as such:
 * KthLargest* obj = new KthLargest(k, nums);
 * int param_1 = obj->add(val);
 */

11. stack——后进先出,从容器top开始pop

在这里插入图片描述

12. unordered_map——无序的键值对

无序map。

template < class Key,                                    // unordered_map::key_type
           class T,                                      // unordered_map::mapped_type
           class Hash = hash<Key>,                       // unordered_map::hasher
           class Pred = equal_to<Key>,                   // unordered_map::key_equal
           class Alloc = allocator< pair<const Key,T> >  // unordered_map::allocator_type
           > class unordered_map;

unordered_map的容器用法:

unordered_map<Key,T>::iterator it;
(*it).first;             // the key value (of type Key)
(*it).second;            // the mapped value (of type T)
(*it);                   // the "element value" (of type pair<const Key,T>)it->first;               // same as (*it).first   (the key value)
it->second;              // same as (*it).second  (the mapped value) 

在这里插入图片描述
在这里插入图片描述

13. unordered_multimap——无序的允许重复键的键值对

template < class Key,                                    // unordered_multimap::key_type
           class T,                                      // unordered_multimap::mapped_type
           class Hash = hash<Key>,                       // unordered_multimap::hasher
           class Pred = equal_to<Key>,                   // unordered_multimap::key_equal
           class Alloc = allocator< pair<const Key,T> >  // unordered_multimap::allocator_type
           > class unordered_multimap;

在这里插入图片描述
在这里插入图片描述

14. unordered_set——无序的set

template < class Key,                        // unordered_set::key_type/value_type
           class Hash = hash<Key>,           // unordered_set::hasher
           class Pred = equal_to<Key>,       // unordered_set::key_equal
           class Alloc = allocator<Key>      // unordered_set::allocator_type
           > class unordered_set;

在这里插入图片描述
在这里插入图片描述

15. unordered_multiset——无序的multiset

template < class Key,                         // unordered_multiset::key_type/value_type
           class Hash = hash<Key>,            // unordered_multiset::hasher
           class Pred = equal_to<Key>,        // unordered_multiset::key_equal
           class Alloc = allocator<Key>       // unordered_multiset::allocator_type
           > class unordered_multiset;

在这里插入图片描述
在这里插入图片描述

16. vector——一个能够存放任意类型的动态数组

template < class T, class Alloc = allocator<T> > class vector; // generic template~

在这里插入图片描述
在这里插入图片描述

17. vector< bool> ——不推荐使用

template < class T, class Alloc = allocator<T> > class vector; // generic template
template <class Alloc> class vector<bool,Alloc>;               // bool specialization~

在这里插入图片描述
具体来讲,不推荐使用 vector< bool> 的原因有以下 2 个:

  1. 严格意义上讲,vector< bool> 并不是一个 STL 容器;
  2. vector< bool> 底层存储的并不是 bool 类型值。

那么,如果在实际场景中需要使用 vector< bool> 这样的存储结构,该怎么办呢?很简单,可以选择使用 deque< bool> 或者 bitset 来替代 vector< bool>
要知道,deque 容器几乎具有 vecotr 容器全部的功能(拥有的成员方法也仅差 reserve() 和 capacity()),而且更重要的是,deque 容器可以正常存储 bool 类型元素。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值