无涯教程-C++ Deque - begin()函数

37 阅读1分钟

C++ Deque begin()函数返回一个指向deque集合第一个元素的迭代器。如果集合为空,则返回的迭代器将等于end()。

begin - 语法

iterator begin(); 

begin - 返回值

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

begin - 例子1

让我们看一个简单的例子

#include <iostream>
#include<deque>
using namespace std;
int main()
{
  deque<int> n={1,2,3};
  deque<int>::iterator itr;
  itr=n.begin();
  cout<<"first element of the deque:"<<*itr;
  return 0;
}

输出:

first element of the deque:1

在此示例中,begin()函数返回第一个元素的迭代器。

begin - 例子2

让我们看一个简单的例子

#include <iostream>
#include<deque>
using namespace std;
int main()
{
  deque<char> ch={C,+,+};
  deque<char>::iterator itr;
  itr=ch.begin()+2;
  cout<<*itr;
  return 0;
}

在此示例中,begin()函数递增2。因此,begin()函数返回第三个元素的迭代器。

参考链接

www.learnfk.com/c++/cpp-deq…