at
语法:
1 |
reference at( size_type pos ); |
at()函数返回一个引用,指向双向队列中位置pos上的元素。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
源代码 /** * @brief Provides access to the data contained in the %deque. * @param n The index of the element for which data should be accessed. * @return Read/write reference to data. * @throw std::out_of_range If @a n is an invalid index. * * This function provides for safer data access. The parameter is first * checked that it is in the range of the deque. The function throws * out_of_range if the check fails. */ reference at(size_type __n) { _M_range_check(__n); return (*this)[__n]; } /** * @brief Provides access to the data contained in the %deque. * @param n The index of the element for which data should be accessed. * @return Read-only (constant) reference to data. * @throw std::out_of_range If @a n is an invalid index. * * This function provides for safer data access. The parameter is first * checked that it is in the range of the deque. The function throws * out_of_range if the check fails. */ const_reference at(size_type __n) const { _M_range_check(__n); return (*this)[__n]; } |
测试代码:
测试结果:
dq3双向对列:
1 1 1 1 1
请按任意键继续. . .
注:at函数和下标操作符[]返回内容一样,但at会检查索引下标值,更安全。