STLarray的at方法(2)
原文地址:http://www.cplusplus.com/reference/array/array/at/
public member function
std::array::at
reference at ( size_type n );
const_reference at ( size_type n ) const;
Access element
Returns a reference to the element at position n in the array.
返回一个指向位置为n的元素的引用。
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在该array中是否是一个有效的值,当n大于或者等于它的size的时候,将会抛出out_of_range异常,这和成员方法operato[]是一个对比,因为operator[]不会检测n的范围。例子:
#include
#include
using namespace std;
void at(){
array<int,5> ai{1,2,3,4,5};
cout<<”ai is “;
for(int &i:ai)
cout<<i<<” “;
cout<<endl;
cout<<”ai.at(3)=”<<ai.at(3)<<endl;
cout<<”ai.at(10)=”<<ai.at(10)<<endl;
}
运行截图:
Parameters
n
Position of an element in the array.
If this is greater than or equal to the array 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 alias of the unsigned integral type size_t.
n为元素在array中的位置。
如果n大于或者等于array的大小,将抛出out_of_range异常。
注意第一个元素的位置是0.
size_type是一个无符号整型。
Return value
The element at the specified position in the array.
返回array中一个在特定位置的元素。
If the array object is const-qualified, the function returns a const_reference. Otherwise, it returns a reference.如果array对象是const属性的,那么返回一个const_reference,否则,返回一个reference。
Member types reference and const_reference are the reference types to the elements of the array (see array member types).
reference以及constreference都是指向元素类型的引用。
Example
1 | 1 |
1 | // array::at |
Output:
myarray contains: 1 2 3 4 5 6 7 8 9 10
Complexity
Constant.
Iterator validity
No changes.
Data races
The reference returned can be used to access or modify elements. Concurrently accessing or modifying different elements is safe.
返回的引用可以用来访问以及修改元素,并且这些操作都是安全的。
例子:
void at2(){
array<int,5> ai{1,2,3,4,5};
cout<<”ai is “;
for(int &i:ai)
cout<<i<<” “;
cout<<endl;
int &r=ai.at(3);
cout<<”ai.at(3)=”<<r<<endl;
r=15;
cout<<”after alter ai is “;
for(int &i:ai)
cout<<i<<” “;
cout<<endl;
}
运行截图:
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异常。
例子:
#include
#include
using namespace std;
void at(){
array<int,5> ai{1,2,3,4,5};
cout<<”ai is “;
for(int &i:ai)
cout<<i<<” “;
cout<<endl;
cout<<”ai.at(3)=”<<ai.at(3)<<endl;
try{
cout<<”ai.at(10)=”<<ai.at(10)<<endl;
}catch(out_of_range)
{
cout<<”catch a out_of_range exceptions”<<endl;
cout<<”ai is “;
for(int &i:ai)
cout<<i<<” “;
cout<<endl;
}
}
运行结果:
——————————————————————————————————————————————————————————————————
//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
2014-8-26
于GDUT
——————————————————————————————————————————————————————————————————
- 本文作者: royalchen
- 本文链接: http://www.royalchen.com/2016/02/24/stlarray的at方法(2)/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!