std::basic_string<CharT,Traits,Allocator>::rend, std::basic_string<CharT,Traits,Allocator>::crend

< cpp‎ | string‎ | basic string
(1)
reverse_iterator rend();
(C++11 前)
reverse_iterator rend() noexcept;
(C++11 起)
(C++20 前)
constexpr reverse_iterator rend() noexcept;
(C++20 起)
(2)
const_reverse_iterator rend() const;
(C++11 前)
const_reverse_iterator rend() const noexcept;
(C++11 起)
(C++20 前)
constexpr const_reverse_iterator rend() const noexcept;
(C++20 起)
(3)
const_reverse_iterator crend() const;
(C++11 前)
const_reverse_iterator crend() const noexcept;
(C++11 起)
(C++20 前)
constexpr const_reverse_iterator crend() const noexcept;
(C++20 起)

返回指向后随逆向字符串末字符的字符的逆向迭代器。它对应前驱非逆向字符串首字符的字符。此字符表现为占位符,试图访问它会导致未定义行为。

range-rbegin-rend.svg

参数

(无)

返回值

指向后随末字符的字符的逆向迭代器。

复杂度

常数。

注解

在 libstdc++ 中,crend() 不能在 C++98 模式中使用。

示例

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
 
int main()
{
    std::string p("[A man, a plan, a canal: Panama]");
    std::string q;
 
    std::copy(p.crbegin(), p.crend(), std::back_inserter(q));
    std::cout << "q = " << q << '\n';
 
    std::copy(q.crbegin(), q.crend(), p.rbegin());
    std::cout << "p = " << p << '\n';
}

输出:

q = ]amanaP :lanac a ,nalp a ,nam A[
p = ]amanaP :lanac a ,nalp a ,nam A[

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 出版时的行为 正确行为
LWG 1192 C++98 std::string 没有成员函数 crend() 已添加

参阅

返回指向起始的逆向迭代器
(公开成员函数)
返回指向结尾的反向迭代器
(std::basic_string_view<CharT,Traits> 的公开成员函数)