C ++ STL中的deque :: begin()和deque :: end

在本文中,我们将讨论C ++ STL中deque::begin()和deque::end()函数的工作原理,语法和示例。

什么是双端队列?

双端队列是双端队列,它是序列容器,在两端都提供扩展和收缩功能。队列数据结构允许用户仅在END插入数据,并从FRONT删除数据。让我们以在公交车站排队的类比为例,那里的人只能从END插入队列,而站在FRONT的人是第一个被移走的人,而在双头队列中,可以同时插入和删除数据结束。

什么是deque::begin()?

deque::begin()是C ++ STL中的内置函数,在头文件中声明。deque::begin()返回一个迭代器,该迭代器引用与该函数关联的双端队列容器的第一个元素。二者begin()end()通过双端队列容器用于迭代。

语法

mydeque.begin();

参数

此函数不接受任何参数

返回值

它返回一个指向双端队列容器中第一个元素的迭代器。

示例

Input: deque<int> mydeque = {10, 20, 30, 40};
   mydeque.begin();
Output:
   Element at the beginning is =10

示例

#include <deque>
#include <iostream>
using namespace std;
int main(){
   deque<int> Deque = {2, 4, 6, 8, 10 };
   cout<<"Elements are : ";
   for (auto i = Deque.begin(); i!= Deque.end(); ++i)
      cout << ' ' << *i;
   return 0;
}

输出结果

如果我们运行上面的代码,它将生成以下输出-

Elements are : 2 4 6 8 10

什么是deque::end()?

deque::end()是C ++ STL中的内置函数,在<deque>头文件中声明。deque::end()返回一个迭代器,该迭代器在与该函数关联的双端队列容器的最后一个元素旁边进行引用。二者begin()end()通过双端队列容器用于迭代。

语法

mydeque.end();

参数

此函数不接受任何参数

返回值

它返回一个指向deque容器中最后一个元素旁边的迭代器。

示例

Input: deque<int> mydeque = {10, 20, 30, 40};
   mydeque.end();
Output:
   Element at the ending is =5 //Random value which is next to the last element.

示例

#include <deque>
#include <iostream>
using namespace std;
int main(){
   deque<int> Deque = { 10, 20, 30, 40};
   cout<<"Elements are : ";
   for (auto i = Deque.begin(); i!= Deque.end(); ++i)
      cout << ' ' << *i;
   return 0;
}

输出结果

如果我们运行上面的代码,它将生成以下输出-

Elements are : 10 20 30 40