STL deque的at方法(4)
来源:互联网 发布:知乎句子 编辑:程序博客网 时间:2024/04/29 14:59
std::deque::at
reference at (size_type n);const_reference at (size_type n) const;
返回在特定位置元素的引用。
The function automatically checks whether n is within the bounds of valid elements in the container, throwing an out_of_range exception if it is not (i.e., if n is greater or equal than its size). This is in contrast with member operator[], that does not check against bounds.
该方法将自动检测n的合法性,如果n超出了范围,将抛出out_of_range异常。另一个相似的是operator[]方法,但是不会检测合法性。
例子:
#include <iostream>#include <deque>#include <vector>using namespace std;int main(){deque<int> di{1,2,3,4,5};for(int i:di)cout<<i<<" ";cout<<endl;cout<<"di.at(0)="<<di.at(0)<<endl;cout<<"di.at(2)="<<di.at(2)<<endl;cout<<"di.at(3)="<<di.at(3)<<endl;cout<<"di.at(4)="<<di.at(4)<<endl;cout<<"di.at(5)="<<di.at(5)<<endl;cout<<"di.at(-1)="<<di.at(-1)<<endl;}
返回deque容器特定位置的元素的引用。
The difference between this member function and member operator function operator[] is that deque::at signals if the requested position is out of range by throwing an out_of_range exception.
和operator[]方法不同的是at在n不合法的时候会跑出out_of_range方法。
Parameters
- n
- Position of an element in the container.
If this is greater than the deque size, an exception of type out_of_range is thrown.
Notice that the first element has a position of 0 (not 1).
Member type size_type is an unsigned integral type.
容器内元素的位置。
需要注意第一个元素的位置为0.
Return value
The element at the specified position in the container.返回特定位置元素的引用。
Member types reference and const_reference are the reference types to the elements of the container (see deque member types).
Example
1234567891011121314151617181920
// deque::at#include <iostream>#include <deque>int main (){ std::deque<unsigned> mydeque (10); // 10 zero-initialized unsigneds // assign some values: for (unsigned i=0; i<mydeque.size(); i++) mydeque.at(i)=i; std::cout << "mydeque contains:"; for (unsigned i=0; i<mydeque.size(); i++) std::cout << ' ' << mydeque.at(i); std::cout << '\n'; return 0;}
Output:
mydeque contains: 0 1 2 3 4 5 6 7 8 9
Complexity
Constant.Iterator validity
No changes.Data races
The container is accessed (neither the const nor the non-const versions modify the container).Element n is potentially accessed or modified by the caller. Concurrently accessing or modifying other elements is safe.
Exception safety
Strong guarantee: if an exception is thrown, there are no changes in the container.It throws out_of_range if n is out of bounds.
如果n不再范围内将抛出out_of_range异常。
——————————————————————————————————————————————————————————————————
//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
//今后的翻译将以简洁为主,翻译其主要意思,不再每句翻译。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:coderguang@gmail.com
2014-9-1
于GDUT
——————————————————————————————————————————————————————————————————
- STL deque的at方法(4)
- STL deque的back方法(5)
- STL deque的begin方法(6)
- STL deque的cend方法(8)
- STL deque的clear方法(9)
- STL deque的crbegin方法(10)
- STL deque的crend方法(11)
- STL deque的emplace方法(12)
- 【STL】deque的常用方法
- STL array的at方法(2)
- STL deque的方法assign(3)
- STL deque的方法cbegin(7)
- STL deque的介绍(1)
- STL deque的构造函数(2)
- STL的Deque介绍
- STL的deque类
- STL deque的使用
- STL的deque读书笔记
- 转载一篇关于Servlet过滤器的详细讲解
- Hadoop快速入门
- 火狐firebug显示错误SyntaxError: missing ) after argument list的问题
- pt-query-digest官方介绍与实际示例
- vs2010 coff
- STL deque的at方法(4)
- 一边打码一边看片
- iOS应用中打开 手电筒/闪光灯 的方法
- CF 463D Gargari and Permutations [dp]
- android按返回健提示是否退出
- 查看linux机器是32位还是64位的方法
- VirtualBox中共享文件夹的设置
- 分析100万个CEO简历后,整理出20项顶级技能----你应该学习!
- Ubuntu更新过程被中断后的问题