std::deque<T,Allocator>::operator[]
来自cppreference.com
reference operator[]( size_type pos ); | (1) | (C++26 起为 constexpr) |
const_reference operator[]( size_type pos )const; | (2) | (C++26 起为 constexpr) |
返回位于指定位置 pos 的元素的引用。
如果 pos < size() 是 false,那么行为未定义。 | (C++26 前) |
如果 pos < size() 是 false,那么: | (C++26 起) |
目录 |
[编辑]参数
pos | - | 要返回的元素的位置 |
[编辑]返回值
到所需元素的引用。
[编辑]复杂度
常数。
[编辑]注解
与 std::map::operator[] 不同,此运算符不会向容器插入新元素。如果实现未被硬化,那么(C++26 起)通过此运算符访问不存在的元素是未定义行为。
[编辑]示例
下列代码使用 operator[] 读取并写入 std::deque<int>:
运行此代码
#include <deque>#include <iostream> int main(){std::deque<int> numbers{2, 4, 6, 8}; std::cout<<"第二个元素:"<< numbers[1]<<'\n'; numbers[0]=5; std::cout<<"所有数:";for(auto i : numbers)std::cout<<' '<< i;std::cout<<'\n';}
输出:
第二个元素:4 所有数:5 4 6 8
[编辑]参阅
带越界检查访问指定的元素 (公开成员函数) |