第9.14章: 序列容器适配器

English

概述

适配器是包裹现有容器、提供受限接口的类。fast_io 提供三种适配器,我们会在第10章更详细地介绍它们。

1. 适配器一览

适配器 默认容器 访问模式
::fast_io::stack<T> deque<T> LIFO(后进先出)
::fast_io::queue<T> deque<T> FIFO(先进先出)
::fast_io::priority_queue<T> vector<T> 优先级最高的元素在顶部

2. 示例


#include <fast_io_dsal/stack.h>
#include <fast_io_dsal/queue.h>
#include <fast_io_dsal/priority_queue.h>
#include <fast_io.h>

int main() {
    // Stack: LIFO
    ::fast_io::stack<::std::size_t> stk;
    stk.push(1zu);
    stk.push(2zu);
    stk.push(3zu);
    ::fast_io::io::println("Top: ", stk.top()); // 3
    stk.pop(); // removes 3

    // Queue: FIFO
    ::fast_io::queue<::std::size_t> q;
    q.push(1zu);
    q.push(2zu);
    q.push(3zu);
    ::fast_io::io::println("Front: ", q.front()); // 1
    q.pop(); // removes 1

    // Priority queue: largest on top
    ::fast_io::priority_queue<::std::size_t> pq;
    pq.push(3zu);
    pq.push(1zu);
    pq.push(2zu);
    ::fast_io::io::println("Top: ", pq.top()); // 3
}

所有适配器都提供 pop_element() — 它一步完成“把顶部元素移出并弹出”,并返回被移出的值。这比分别调用 top() 再 pop() 更高效。它们还提供 get_container() 来访问底层容器。

pop_element() 示例


// Stack: pop_element() moves and returns the top element
::fast_io::stack<::fast_io::string> stk;
stk.push(::fast_io::string{"hello"});
stk.push(::fast_io::string{"world"});

// Move the top element out and pop in one step
::fast_io::string top = stk.pop_element(); // top == "world", stk has 1 element

// Queue: pop_element() moves and returns the front element
::fast_io::queue<::fast_io::string> q;
q.push(::fast_io::string{"first"});
q.push(::fast_io::string{"second"});

::fast_io::string front = q.pop_element(); // front == "first", q has 1 element

// Priority queue: pop_element() moves and returns the highest-priority element
::fast_io::priority_queue<::std::size_t> pq;
pq.push(3zu);
pq.push(1zu);
pq.push(2zu);

::std::size_t highest = pq.pop_element(); // highest == 3, pq has 2 elements

还有 pop_element_unchecked(),它跳过空检查,适用于性能关键的代码。

get_container() ——访问底层容器

get_container() 返回底层容器的引用。这让你可以遍历所有元素、查询 size(),或使用容器提供的任何方法:


#include <fast_io_dsal/stack.h>
#include <fast_io_dsal/queue.h>
#include <fast_io.h>

int main() {
    // Stack backed by deque (default)
    ::fast_io::stack<::std::size_t> stk;
    stk.push(10zu);
    stk.push(20zu);
    stk.push(30zu);

    // Query the underlying container
    ::fast_io::io::println("Size: ", stk.get_container().size()); // 3

    // Iterate over all elements in the underlying container
    for (auto const& elem : stk.get_container()) {
        ::fast_io::io::println(elem);
    }
    // Prints: 10, 20, 30 (bottom to top of stack)

    // Queue: iterate over elements in FIFO order
    ::fast_io::queue<::fast_io::string> q;
    q.push(::fast_io::string{"first"});
    q.push(::fast_io::string{"second"});
    q.push(::fast_io::string{"third"});

    for (auto const& elem : q.get_container()) {
        ::fast_io::io::println(elem);
    }
    // Prints: first, second, third (front to back of queue)
}

非 const 重载返回可变引用,因此你可以直接修改底层容器。const 与非 const 两个重载都提供。

要点