deque crend в C ++ STL

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

Deque :: crend () - это встроенная функция в C ++ STL, которая возвращает постоянный обратный итератор, указывающий на позицию перед первым элементом двухсторонней очереди.
Синтаксис

deque_name.crend ()

Параметры: эта функция не принимает никаких параметров.
Тип возвращаемого значения: эта функция возвращает постоянный обратный итератор двухсторонней очереди.

Пример-1:

// C++ program to illustrate the
// deque::crend() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
deque< int > dq = { 10, 20, 30, 40, 50 };
cout << "The deque in reverse order: " ;
// prints the elements in reverse order
for ( auto it = dq.crend() - 1; it >= dq.crbegin(); --it)
cout << *it << endl;
return 0;
}
Выход:
Дека в обратном порядке: 
10
20
30
40
50

Пример-2: Поскольку итератор постоянный, попытка изменить его приведет к ошибке.

// C++ program to illustrate the
// deque::crend() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
deque< char > dq = { 'a' , 'b' , 'c' , 'd' , 'e' , 'f' };
cout << "The deque in reverse order: " ;
// prints the elements in reverse order
for ( auto it = dq.crend() - 1; it >= dq.crbegin(); --it)
*it = 'g'
return 0;
}

Compilation Error in CPP code :- prog.cpp: In function ‘int main()’:
prog.cpp:15:13: error: assignment of read-only location ‘it.std::reverse_iterator<_iterator>::operator* >()’
*it = ‘g’
^
prog.cpp:17:5: error: expected ‘;’ before ‘return’
return 0;
^

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 и многому другому, см. Полный курс подготовки к собеседованию .