定义于头文件 <deque>
std::deque
修改器
移除首元素
std::deque<T,Allocator>::pop_front
void pop_front(); |
移除容器首元素。若容器中无元素,则行为未定义。
指向被擦除元素的迭代器和引用被非法化。若元素是容器中的最后元素,则尾后迭代器是否被非法化是未指定的。其他迭代器和引用不受影响。 | (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_front();
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<T,Allocator>::resize
void resize( size_type count, T value = T() ); | (C++11 前) | |
void resize( size_type count ); | (1) | (C++11 起) |
void resize( size_type count, const value_type& value ); | (2) | (C++11 起) |
重设容器大小以容纳 count
个元素。
若当前大小大于 count
,则减小容器为其首 count
个元素。
若当前大小小于 | (C++11 前) |
若当前大小小于 1) 则后附额外的默认插入的元素 2) 则后附额外的 | (C++11 起) |
参数
count | - | 容器的大小 |
value | - | 用以初始化新元素的值 |
类型要求 | ||
- 为使用重载 (1) , T 必须满足可移动插入 (MoveInsertable) 和 可默认插入 (DefaultInsertable) 的要求。 | ||
- 为使用重载 (2) , T 必须满足可复制插入 (CopyInsertable) 的要求。 |
返回值
(无)
复杂度
与当前大小和 count
间的差成线性。
调用示例
std::deque<Cell> deque1;
//重设容器大小以容纳 count 个元素。
deque1.resize(6);
std::cout << "deque1 : ";
std::copy(deque1.begin(), deque1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::deque<Cell> deque2;
//重设容器大小以容纳 count 个元素。
deque2.resize(6, Cell{303, 303});
std::cout << "deque2 : ";
std::copy(deque2.begin(), deque2.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
交换内容
std::deque<T,Allocator>::swap
void swap( deque& other ); | (C++17 前) | |
void swap( deque& other ) noexcept(/* see below */); | (C++17 起) |
将内容与 other
的交换。不在单个元素上调用任何移动、复制或交换操作。
所有迭代器和引用保持合法。尾后迭代器被非法化。
若 std::allocator_traits<allocator_type>::propagate_on_container_swap::value 为 true ,则用非成员 | (C++11 起) |
参数
other | - | 要与之交换内容的容器 |
返回值
(无)
异常
(无) | (C++17 前) |
noexcept 规定: noexcept(std::allocator_traits<Allocator>::is_always_equal::value) | (C++17 起) |
复杂度
常数。
调用示例
std::cout << "swap before:" << std::endl;
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;
std::deque<Cell> deque2(6);
std::generate(deque2.begin(), deque2.end(), generate);
std::cout << "deque2 : ";
std::copy(deque2.begin(), deque2.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
//将内容与 other 的交换。不在单个元素上调用任何移动、复制或交换操作。
deque1.swap(deque2);
std::cout << "swap after:" << std::endl;
std::cout << "deque1 : ";
std::copy(deque1.begin(), deque1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::cout << "deque2 : ";
std::copy(deque2.begin(), deque2.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_front();
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;
//重设容器大小以容纳 count 个元素。
deque1.resize(6);
std::cout << "deque1 : ";
std::copy(deque1.begin(), deque1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::deque<Cell> deque2;
//重设容器大小以容纳 count 个元素。
deque2.resize(6, Cell{303, 303});
std::cout << "deque2 : ";
std::copy(deque2.begin(), deque2.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::cout << std::endl;
}
{
std::cout << "swap before:" << std::endl;
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;
std::deque<Cell> deque2(6);
std::generate(deque2.begin(), deque2.end(), generate);
std::cout << "deque2 : ";
std::copy(deque2.begin(), deque2.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
//将内容与 other 的交换。不在单个元素上调用任何移动、复制或交换操作。
deque1.swap(deque2);
std::cout << "swap after:" << std::endl;
std::cout << "deque1 : ";
std::copy(deque1.begin(), deque1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::cout << "deque2 : ";
std::copy(deque2.begin(), deque2.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
}
return 0;
}
输出