STL deque的at方法(4)

原文地址:http://www.cplusplus.com/reference/deque/deque/at/
public member function
<deque>

std::deque::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 deque container object.

返回在特定位置元素的引用。


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;
	


}
STL deque的at方法(4)_第1张图片


Returns a reference to the element at position n in the deque container object.

返回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.

返回特定位置元素的引用。


If the deque object is const-qualified, the function returns a const_reference. Otherwise, it returns a reference.

Member types reference and const_reference are the reference types to the elements of the container (see deque member types).

Example

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 
// 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; }
Edit & Run


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:[email protected]

2014-9-1

于GDUT

——————————————————————————————————————————————————————————————————





你可能感兴趣的:(STL,STL,C++11,deque,deque)