在 C++中,<deque> 是标准模板库(STL)的一部分,它提供了双端队列(double-ended queue)的实现。
双端队列是一种允许在两端进行插入和删除操作的线性数据结构。
<deque> 的全称是 "double-ended queue",它在C++中以模板类的形式存在,允许存储任意类型的数据。
<deque> 是一个动态数组,它提供了快速的随机访问能力,同时允许在两端进行高效的插入和删除操作。这使得 <deque> 成为处理需要频繁插入和删除元素的场景的理想选择。
// <deque> -*- C++ -*-
// Copyright (C) 2001-2020 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/*
*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Hewlett-Packard Company makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
*
* Copyright (c) 1997
* Silicon Graphics Computer Systems, Inc.
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Silicon Graphics makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*/
/** @file include/deque
* This is a Standard C++ Library header.
*/
#ifndef _GLIBCXX_DEQUE
#define _GLIBCXX_DEQUE 1
#pragma GCC system_header
#include <bits/stl_algobase.h>
#if __cplusplus > 201703L
# include <bits/stl_algo.h> // For remove and remove_if
#endif // C++20
#include <bits/allocator.h>
#include <bits/stl_construct.h>
#include <bits/stl_uninitialized.h>
#include <bits/stl_deque.h>
#include <bits/range_access.h>
#include <bits/deque.tcc>
#ifdef _GLIBCXX_DEBUG
# include <debug/deque>
#endif
#if __cplusplus >= 201703L
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
namespace pmr
{
template<typename _Tp> class polymorphic_allocator;
template<typename _Tp>
using deque = std::deque<_Tp, polymorphic_allocator<_Tp>>;
} // namespace pmr
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
#endif // C++17
#if __cplusplus > 201703L
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
#define __cpp_lib_erase_if 202002L
template<typename _Tp, typename _Alloc, typename _Predicate>
inline typename deque<_Tp, _Alloc>::size_type
erase_if(deque<_Tp, _Alloc>& __cont, _Predicate __pred)
{
const auto __osz = __cont.size();
__cont.erase(std::remove_if(__cont.begin(), __cont.end(), __pred),
__cont.end());
return __osz - __cont.size();
}
template<typename _Tp, typename _Alloc, typename _Up>
inline typename deque<_Tp, _Alloc>::size_type
erase(deque<_Tp, _Alloc>& __cont, const _Up& __value)
{
const auto __osz = __cont.size();
__cont.erase(std::remove(__cont.begin(), __cont.end(), __value),
__cont.end());
return __osz - __cont.size();
}
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
#endif // C++20
#endif /* _GLIBCXX_DEQUE */
deque() | 默认构造函数,创建一个空的 deque 容器。 |
deque(size_type n) | 创建一个包含 n 个默认值元素的 deque 容器。 |
deque(size_type n, const T& value) | 创建一个包含 n 个值为 value 的 deque 容器。 |
deque(initializer_list<T> il) | 使用初始化列表 il 构造 deque 容器。 |
operator= | 赋值操作符,赋值给 deque 容器。 |
assign() | 用新值替换 deque 容器中的所有元素。 |
at(size_type pos) | 返回 pos 位置的元素,并进行范围检查。 |
operator[](size_type pos) | 返回 pos 位置的元素,不进行范围检查。 |
front() | 返回第一个元素的引用。 |
back() | 返回最后一个元素的引用。 |
begin() | 返回指向第一个元素的迭代器。 |
end() | 返回指向末尾元素后一位置的迭代器。 |
rbegin() | 返回指向最后一个元素的逆向迭代器。 |
rend() | 返回指向第一个元素之前位置的逆向迭代器。 |
empty() | 检查容器是否为空。 |
size() | 返回容器中的元素个数。 |
max_size() | 返回容器可容纳的最大元素个数。 |
clear() | 清除容器中的所有元素。 |
insert(iterator pos, const T& value) | 在 pos 位置插入 value 元素。 |
erase(iterator pos) | 移除 pos 位置的元素。 |
push_back(const T& value) | 在容器末尾添加 value 元素。 |
pop_back() | 移除容器末尾的元素。 |
push_front(const T& value) | 在容器前端添加 value 元素。 |
pop_front() | 移除容器前端的元素。 |
resize(size_type count) | 调整容器大小为 count,多出部分用默认值填充。 |
swap(deque& other) | 交换两个 deque 容器的内容。 |
get_allocator() | 返回一个用于构造双端队列的分配器对象的副本。 |
8764

被折叠的 条评论
为什么被折叠?



