三、容器--特殊容器

三、容器–特殊容器

标签(空格分隔): c++STL


特殊的几种容器:
- bitset
- queue
- priority_queue
- stack

1.bitset模板

  • bitset既定后,不能改变位的个数,可使用变长数组vector<bool>

1.构造函数

bitset<n> b;//默认,全部位置0
bitset(unsigned long val);//val整数,对应的二进制初始化
bitset(const string& str, size_t pos =0,size_t n =-1); //字符串初始化

第三个构造函数:
- pos:str的起始位置
- n: 从pos起选择n个字符

例子:

std::bitset<8> b1; // [0,0,0,0,0,0,0,0]

// unsigned long long constructor
std::bitset<8> b2(42); // [0,0,1,0,1,0,1,0]

// string constructor
std::string bit_string = "110010";
std::bitset<8> b3(bit_string);       // [0,0,1,1,0,0,1,0]
std::bitset<8> b4(bit_string, 2);    // [0,0,0,0,0,0,1,0]
std::bitset<8> b5(bit_string, 2, 3); // [0,0,0,0,0,0,0,1]

// 用默认字符替换字符串中的字符,如‘a’代表0,‘b’代表1
std::string alpha_bit_string = "aBaaBBaB";
std::bitset<8> b6(alpha_bit_string, 0, alpha_bit_string.size(),
                      'a', 'B');         // [0,1,0,0,1,1,0,1]

// char* constructor using custom digits
std::bitset<8> b7("XXXXYYYY", 0, 'X', 'Y'); // [0,0,0,0,1,1,1,1]

2.元素访问和容量

元素访问:
- operator[k]:返回k上的位值
- test(k):判断位k是否为“1”
- all(): 所有位是否置位1
- any(): 是否存在置位1的位
- none(): 是否不存在置位1的位
- count(): 置位1的位数

容量:
-size

3.修改器

  • set(pos,v):将pos位置为 v;
  • set():将所有位置位1
  • reset(pos):将pos复位0
  • reset():将所有位复位0
  • flip(pos):反转pos位
  • flip():反转所有位
operator操作函数:
执行二元与,或、异或、和取反运算
- operator&=
- operator|=
- operator^=
- operator~

执行二进制左移位和右移位
- operator<<=
- operator>>=
- operator<<
- operator>>

4.转换

  • to_string:返回数据的字符串表示
  • to_ulong :返回数据的 unsigned long 整数表示
  • to_ullong :返回数据的 unsigned long long 整数表示

2.stack类模板

后进先出
模板声明:

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

1.初始化

最简单:
stack<T> s;

2.元素访问和容量

  • top() :访问栈顶元素
  • empty()
  • size()

3.修改器

入栈:
- push : 向栈顶插入元素
- emplace: 在栈顶就地构造元素
出栈:
- pop:删除栈顶的元素

swap():
- swap

4.非成员函数

  • operator==
  • operator!=
  • operator<
  • operator<=
  • operator>
  • operator>=

3.queue类模板

先进先出
template<classT, class Container=deque<T> >class queue;

1.初始化

最简单:
queue<T> q;

2.元素访问和容量

  • front
  • back

容量:
- empty
- size

3.修改器

  • push
  • emplace
  • pop
  • swap

4.非成员函数

  • operator==
  • operator!=
  • operator<
  • operator<=
  • operator>
  • operator>=

4.priority_queue 类模板

元素按照优先级顺序被读取。按优先级存储。

模板声明:

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

最简单定义:
priority_queue<T> pq;
### 1.元素访问和容量
- top
- empty
- size

3.修改器

  • push
  • emplace
  • pop
  • swap

例子:

#include <functional>
#include <queue>
#include <vector>
#include <iostream>

template<typename T> void print_queue(T& q) {
    while(!q.empty()) {
        std::cout << q.top() << " ";
        q.pop();
    }
    std::cout << '\n';
}

int main() {
    std::priority_queue<int> q;

    for(int n : {1,8,5,6,3,4,0,9,7,2})
        q.push(n);

    print_queue(q);

    std::priority_queue<int, std::vector<int>, std::greater<int> > q2;

    for(int n : {1,8,5,6,3,4,0,9,7,2})
        q2.push(n);

    print_queue(q2);

    // 使用lambda表达式来比较元素
    auto cmp = [](int left, int right) { return (left ^ 1) < (right ^ 1);};
    std::priority_queue<int, std::vector<int>, decltype(cmp)> q3(cmp);

    for(int n : {1,8,5,6,3,4,0,9,7,2})
        q3.push(n);

    print_queue(q3);

}

输出:

9 8 7 6 5 4 3 2 1 0 
0 1 2 3 4 5 6 7 8 9 
8 9 6 7 4 5 2 3 0 1
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值