crbegin примерно до 100 ++ STL

Опубликовано: 3 Декабря, 2021

deque :: crbegin означает constant_reverse_beginner и, как следует из названия, возвращает constant_reverse_iterator, указывающий на последний элемент двухсторонней очереди.

Что такое постоянный итератор?
Постоянный итератор для модификации не используется. Он используется только для доступа к элементам. Вы можете использовать итераторы non_const для изменения элементов.

Синтаксис:

dequename.crbegin ()

Возвращаемое значение: возвращает const_reverse_iterator в обратное начало последовательности.

Заявка:

Given a deque with numbers in the increasing order, print them in non increasing order.
Input: deque{1, 2, 3, 4, 5, 6};
for (auto reverseit = deque.crbegin(); reverseit != deque.crend(); ++reverseit)
cout << " " << *reverseit;

Output : 6 5 4 3 2 1

Программа ниже иллюстрирует работу функции crbegin:

// deque::crbegin and crend
#include <deque>
#include <iostream>
using namespace std;
int main()
{
// Declare a deque with the name numdeque
deque< int > numdeque = { 1, 2, 3, 4, 5, 6 };
// Print the deque backwards using crbegin and crend
cout << "Printing the numdeque backwards:" ;
for ( auto rit = numdeque.crbegin(); rit != numdeque.crend(); ++rit)
cout << ' ' << *rit;
return 0;
}
Выход:
Печать числа в обратном направлении: 6 5 4 3 2 1

Поскольку возвращаемый итератор является постоянным, если мы попытаемся изменить значение, мы получим ошибку компилятора.

// deque::crbegin and crend
#include <deque>
#include <iostream>
using namespace std;
int main()
{
// Declare a deque with the name numdeque
deque< int > numdeque = { 1, 2, 3, 4, 5, 6 };
// Print the deque backwards using crbegin and crend
cout << "Printing the numdeque backwards:" ;
for ( auto rit = numdeque.crbegin(); rit != numdeque.crend(); ++rit)
*rit = 10;
return 0;
}

Выход:

prog.cpp: In function ‘int main()’:
prog.cpp:14:8: error: assignment of read-only location ‘rit.std::reverse_iterator<_iterator>::operator* >()’
*rit = 10;
     ^

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
Хотите узнать о лучших видео и практических задачах, ознакомьтесь с базовым курсом C ++ для базового и продвинутого уровня C ++ и курсом C ++ STL для базового уровня плюс STL. Чтобы завершить подготовку от изучения языка к DS Algo и многому другому, см. Полный курс подготовки к собеседованию .