C ++ Deque at()函數(shù)用于訪問(wèn)指定位置pos的元素。
reference at(size_type pos);
pos:它定義要返回的元素的位置。
其中,size_type是無(wú)符號(hào)整數(shù)類型。
它返回指定元素的引用。
讓我們看一個(gè)簡(jiǎn)單的實(shí)例
#include <iostream>
#include<deque>
using namespace std;
int main()
{
deque<char> ch={'n','h','o','o','o','.','c','o','m'};
for(int i=0;i<ch.size();i++)
cout<<ch.at(i);
return 0;
}輸出:
(cainiaoplus.com)
讓我們看一個(gè)簡(jiǎn)單的實(shí)例
#include <iostream>
#include<deque>
using namespace std;
int main()
{
deque<int> k={1,2,3,4,5};
cout<<k.at(5);
return 0;
}輸出:
terminate called after throwing an instance of 'std::out_of_range'
在此示例中,at()函數(shù)嘗試訪問(wèn)超出容器大小的元素。因此,它將引發(fā)異常,即超出范圍。