定义于头文件 <deque>
std::deque
迭代器
返回指向容器第一个元素的迭代器
std::deque<T,Allocator>::begin,
std::deque<T,Allocator>::cbegin
iterator begin(); | (C++11 前) | |
iterator begin() noexcept; | (C++11 起) | |
const_iterator begin() const; | (C++11 前) | |
const_iterator begin() const noexcept; | (C++11 起) | |
const_iterator cbegin() const noexcept; | (C++11 起) |
返回指向容器首元素的迭代器。
若容器为空,则返回的迭代器将等于 end() 。
参数
(无)
返回值
指向首元素的迭代器。
复杂度
常数。
返回指向容器尾端的迭代器
std::deque<T,Allocator>::end,
std::deque<T,Allocator>::cend
iterator end(); | (C++11 前) | |
iterator end() noexcept; | (C++11 起) | |
const_iterator end() const; | (C++11 前) | |
const_iterator end() const noexcept; | (C++11 起) | |
const_iterator cend() const noexcept; | (C++11 起) |
返回指向容器末元素后一元素的迭代器。
此元素表现为占位符;试图访问它导致未定义行为。
参数
(无)
返回值
指向后随最后元素的迭代器。
复杂度
常数。
返回指向容器最后元素的逆向迭代器
std::deque<T,Allocator>::rbegin,
std::deque<T,Allocator>::crbegin
reverse_iterator rbegin(); | (C++11 前) | |
reverse_iterator rbegin() noexcept; | (C++11 起) | |
const_reverse_iterator rbegin() const; | (C++11 前) | |
const_reverse_iterator rbegin() const noexcept; | (C++11 起) | |
const_reverse_iterator crbegin() const noexcept; | (C++11 起) |
返回指向逆向容器首元素的逆向迭代器。它对应非逆向容器的末元素。
参数
(无)
返回值
指向首元素的逆向迭代器。
复杂度
常数。
返回指向前端的逆向迭代器
std::deque<T,Allocator>::rend,
std::deque<T,Allocator>::crend
reverse_iterator rend(); | (C++11 前) | |
reverse_iterator rend() noexcept; | (C++11 起) | |
const_reverse_iterator rend() const; | (C++11 前) | |
const_reverse_iterator rend() const noexcept; | (C++11 起) | |
const_reverse_iterator crend() const noexcept; | (C++11 起) |
返回指向逆向容器末元素后一元素的逆向迭代器。它对应非逆向容器首元素的前一元素。此元素表现为占位符,试图访问它导致未定义行为。
参数
(无)
返回值
指向末元素后一元素的逆向迭代器。
复杂度
常数。
调用示例
#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(6);
for (std::deque<Cell>::iterator it = deque1.begin(); it != deque1.end(); it++)
{
*it = generate();
}
for (std::deque<Cell>::const_iterator cit = deque1.cbegin(); cit != deque1.cend(); cit++)
{
std::cout << *cit << " ";
}
std::cout << std::endl;
std::cout << std::endl;
std::deque<Cell> deque2(6);
for (std::deque<Cell>::reverse_iterator it = deque2.rbegin(); it != deque2.rend(); it++)
{
*it = generate();
}
for (std::deque<Cell>::const_reverse_iterator cit = deque2.crbegin(); cit != deque2.crend(); cit++)
{
std::cout << *cit << " ";
}
std::cout << std::endl;
return 0;
}