C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ Deque at()

C++ Deque at()

C++ Deque at()函数用于访问指定位置pos的元素。
注意: 如果pos大于容器的大小,则该函数将引发异常,即"超出范围"。

语法

reference at(size_type pos); 

参数

pos : 它定义了要返回的元素的位置。
其中,size_type是无符号整数类型。

返回值

它返回指定元素的引用。

示例1

让我们看一个简单的示例
#include <iostream>
#include<deque>
using namespace std;
int main()
{
   deque<char> ch={'j','a','v','a','T','p','o','i','n','t'};
   for(int i=0;i<ch.size();i++)
   cout<<ch.at(i);
   return 0;
}
输出:
lidihuo

示例2

让我们看一个简单的示例
#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()函数尝试访问超出容器大小的元素。因此,它将引发异常,即超出范围。

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4