定义于头文件 <deque>
std::deque
容量
检查容器是否为空
std::deque<T,Allocator>::empty
bool empty() const; | (C++11 前) | |
bool empty() const noexcept; | (C++11 起) (C++20 前) | |
[[nodiscard]] bool empty() const noexcept; | (C++20 起) |
检查容器是否无元素,即是否 begin() == end() 。
参数
(无)
返回值
若容器为空则为 true ,否则为 false
复杂度
常数
调用示例
std::deque<Cell> deque1;
//检查容器是否为空
std::cout << "deque1 empty: " << deque1.empty() << std::endl;
deque1.resize(6);
std::generate(deque1.begin(), deque1.end(), generate);
std::cout << "deque1 empty: " << deque1.empty() << std::endl;
std::copy(deque1.begin(), deque1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::cout << std::endl;
返回容纳的元素数
std::deque<T,Allocator>::size
size_type size() const; | (C++11 前) | |
size_type size() const noexcept; | (C++11 起) |
返回容器中的元素数,即 std::distance(begin(), end()) 。
参数
(无)
返回值
容器中的元素数量。
复杂度
常数。
调用示例
std::deque<Cell> deque1;
for (size_t index = 0; index < 6; index ++)
{
std::cout << "deque1.size : " << deque1.size() << std::endl;
deque1.push_back(generate());
}
std::cout << std::endl;
返回可容纳的最大元素数
std::deque<T,Allocator>::max_size
size_type max_size() const; | (C++11 前) | |
size_type max_size() const noexcept; | (C++11 起) |
返回根据系统或库实现限制的容器可保有的元素最大数量,即对于最大容器的 std::distance(begin(), end()) 。
参数
(无)
返回值
元素数量的最大值。
复杂度
常数。
注意
此值通常反映容器大小上的理论极限,至多为 std::numeric_limits<difference_type>::max() 。运行时,可用 RAM 总量可能会限制容器大小到小于 max_size()
的值。
调用示例
std::deque<int> deque1;
std::cout << "std::deque<int> max_size: " << deque1.max_size() << std::endl;
std::deque<string> deque2;
std::cout << "std::deque<string> max_size: " << deque2.max_size() << std::endl;
std::deque<long long> deque3;
std::cout << "std::deque<long long> max_size: " << deque3.max_size() << std::endl;
std::deque<double> deque4;
std::cout << "std::deque<double> max_size: " << deque4.max_size() << std::endl;
std::deque<Cell> deque5;
std::cout << "std::deque<Cell> max_size: " << deque5.max_size() << std::endl;
std::deque<char> deque6;
std::cout << "std::deque<char> max_size: " << deque6.max_size() << std::endl;
std::cout << std::endl;
通过释放未使用的内存减少内存的使用
std::deque<T,Allocator>::shrink_to_fit
void shrink_to_fit(); | (C++11 起) |
请求移除未使用的容量。
它是减少使用内存而不更改序列的大小非强制性请求。请求是否达成依赖于实现。
所有迭代器和引用都被非法化。尾后迭代器亦被非法化。
参数
(无)
类型要求 | ||
- T 必须满足可移动插入 (MoveInsertable) 的要求。 |
返回值
(无)
复杂度
至多与容器大小成线性。
注意
若 T 移动构造函数以外的操作抛出异常,则无效果。
调用示例
size_t count = 10;
std::deque<Cell> deque1;
for (size_t index = 0; index < 3; index++)
{
deque1.push_back(generate());
}
//请求移除未使用的容量。
deque1.shrink_to_fit();
for (size_t index = 0; index < count; index++)
{
//超过范围仍然打印
std::cout << "index: " << index << " " << deque1[index] << 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 &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;
//检查容器是否为空
std::cout << "deque1 empty: " << deque1.empty() << std::endl;
deque1.resize(6);
std::generate(deque1.begin(), deque1.end(), generate);
std::cout << "deque1 empty: " << deque1.empty() << std::endl;
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 < 6; index ++)
{
std::cout << "deque1.size : " << deque1.size() << std::endl;
deque1.push_back(generate());
}
std::cout << std::endl;
}
{
std::deque<int> deque1;
std::cout << "std::deque<int> max_size: " << deque1.max_size() << std::endl;
std::deque<string> deque2;
std::cout << "std::deque<string> max_size: " << deque2.max_size() << std::endl;
std::deque<long long> deque3;
std::cout << "std::deque<long long> max_size: " << deque3.max_size() << std::endl;
std::deque<double> deque4;
std::cout << "std::deque<double> max_size: " << deque4.max_size() << std::endl;
std::deque<Cell> deque5;
std::cout << "std::deque<Cell> max_size: " << deque5.max_size() << std::endl;
std::deque<char> deque6;
std::cout << "std::deque<char> max_size: " << deque6.max_size() << std::endl;
std::cout << std::endl;
}
{
size_t count = 10;
std::deque<Cell> deque1;
for (size_t index = 0; index < 3; index++)
{
deque1.push_back(generate());
}
//请求移除未使用的容量。
deque1.shrink_to_fit();
for (size_t index = 0; index < count; index++)
{
//超过范围仍然打印
std::cout << "index: " << index << " " << deque1[index] << std::endl;
}
}
return 0;
}
输出