c++11 标准模板(STL)(std::deque)(九)

定义于头文件 <deque>

std::deque

修改器 

移除末元素

std::deque<T,Allocator>::pop_back

void pop_back();

移除容器的最末元素。

在空容器上调用 pop_back 是未定义的。

指向被擦除元素的迭代器和引用被非法化。尾后迭代器是否被非法化是未指定的。其他迭代器和引用不受影响。

(C++11 前)

指向被擦除元素的迭代器和引用被非法化。尾后迭代器亦被非法化。其他引用和迭代器不受影响。

(C++11 起)

参数

(无)

返回值

(无)

复杂度

常数。

异常

(无)

调用示例

        std::deque<Cell> deque1(6);
        std::generate(deque1.begin(), deque1.end(), generate);
        std::cout << "deque1 :  ";
        std::copy(deque1.begin(), deque1.end(), std::ostream_iterator<Cell>(std::cout, " "));
        std::cout << std::endl;

        for (size_t index = 0; index < 3; index++)
        {
            //移除容器的最末元素。
            deque1.pop_back();
            std::cout << "deque1 :  ";
            std::copy(deque1.begin(), deque1.end(), std::ostream_iterator<Cell>(std::cout, " "));
            std::cout << std::endl;
        }

 

插入元素到容器起始

std::deque<T,Allocator>::push_front

void push_front( const T& value );

void push_front( T&& value );

(C++11 起)

前附给定元素 value 到容器起始。

所有迭代器,包含尾后迭代器,都被非法化。没有引用被非法化。

参数

value-要前附的元素值

返回值

(无)

复杂度

常数。

异常

若抛出异常,则此函数无效果(强异常保证)。

 调用示例

        std::deque<Cell> deque1;
        for (size_t index = 0; index < 3; index++)
        {
            //前附给定元素 value 到容器起始。
            deque1.push_front(generate());
            std::cout << "deque1 :  ";
            std::copy(deque1.begin(), deque1.end(), std::ostream_iterator<Cell>(std::cout, " "));
            std::cout << std::endl;
        }

        std::cout << std::endl;

        for (size_t index = 0; index < 3; index++)
        {
            //前附给定元素 value 到容器起始。
            Cell cell = generate();
            deque1.push_front(std::move(cell));
            std::cout << "deque1 :  ";
            std::copy(deque1.begin(), deque1.end(), std::ostream_iterator<Cell>(std::cout, " "));
            std::cout << std::endl;
        }

在容器头部就地构造元素

std::deque<T,Allocator>::emplace_front

template< class... Args >
void emplace_front( Args&&... args );

(C++11 起)
(C++17 前)

template< class... Args >
reference emplace_front( Args&&... args );

(C++17 起)

插入新元素到容器起始。通过 std::allocator_traits::construct 构造元素,它典型地用布置 new 在容器所提供的位置原位构造元素。将参数 args... 作为 std::forward<Args>(args)... 转发给构造函数。

所有迭代器,包含尾后迭代器,都被非法化。没有引用被非法化。

参数

args-转发给元素构造函数的参数
类型要求
- T (容器元素类型) 必须满足可就位构造 (EmplaceConstructible) 的要求。

返回值

(无)(C++17 前)
到被插入元素的引用。(C++17 起)

复杂度

常数。

异常

若抛异常,则此函数无效果(强异常保证)。

 调用示例

        std::deque<Cell> deque1;
        for (size_t index = 0; index < 3; index++)
        {
            //前附给定元素 value 到容器起始。
            int n = std::rand() % 10 + 100;
            deque1.emplace_front(n, n);
            std::cout << "deque1 :  ";
            std::copy(deque1.begin(), deque1.end(), std::ostream_iterator<Cell>(std::cout, " "));
            std::cout << std::endl;
        }

调用示例

#include <iostream>
#include <deque>
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>
#include <time.h>

using namespace std;

struct Cell
{
    int x;
    int y;

    Cell() = default;
    Cell(int a, int b): x(a), y(b) {}

    Cell &operator +=(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator +(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator *(const Cell &cell)
    {
        x *= cell.x;
        y *= cell.y;
        return *this;
    }

    Cell &operator ++()
    {
        x += 1;
        y += 1;
        return *this;
    }

    bool operator <(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y < cell.y;
        }
        else
        {
            return x < cell.x;
        }
    }

    bool operator ==(const Cell &cell) const
    {
        return x == cell.x && y == cell.y;
    }
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

int main()
{
    std::mt19937 g{std::random_device{}()};

    srand((unsigned)time(NULL));;

    std::cout << std::boolalpha;

    auto generate = []()
    {
        int n = std::rand() % 10 + 100;
        Cell cell{n, n};
        return cell;
    };

    {
        std::deque<Cell> deque1(6);
        std::generate(deque1.begin(), deque1.end(), generate);
        std::cout << "deque1 :  ";
        std::copy(deque1.begin(), deque1.end(), std::ostream_iterator<Cell>(std::cout, " "));
        std::cout << std::endl;

        for (size_t index = 0; index < 3; index++)
        {
            //移除容器的最末元素。
            deque1.pop_back();
            std::cout << "deque1 :  ";
            std::copy(deque1.begin(), deque1.end(), std::ostream_iterator<Cell>(std::cout, " "));
            std::cout << std::endl;
        }
        std::cout << std::endl;
    }

    {
        std::deque<Cell> deque1;
        for (size_t index = 0; index < 3; index++)
        {
            //前附给定元素 value 到容器起始。
            deque1.push_front(generate());
            std::cout << "deque1 :  ";
            std::copy(deque1.begin(), deque1.end(), std::ostream_iterator<Cell>(std::cout, " "));
            std::cout << std::endl;
        }

        std::cout << std::endl;

        for (size_t index = 0; index < 3; index++)
        {
            //前附给定元素 value 到容器起始。
            Cell cell = generate();
            deque1.push_front(std::move(cell));
            std::cout << "deque1 :  ";
            std::copy(deque1.begin(), deque1.end(), std::ostream_iterator<Cell>(std::cout, " "));
            std::cout << std::endl;
        }
        std::cout << std::endl;
    }

    {
        std::deque<Cell> deque1;
        for (size_t index = 0; index < 3; index++)
        {
            //前附给定元素 value 到容器起始。
            int n = std::rand() % 10 + 100;
            deque1.emplace_front(n, n);
            std::cout << "deque1 :  ";
            std::copy(deque1.begin(), deque1.end(), std::ostream_iterator<Cell>(std::cout, " "));
            std::cout << std::endl;
        }
    }

    return 0;
}

输出

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值