跳到主要内容

std::string_view ends_with() 方法

// (1) Const version only
constexpr bool ends_with( std::basic_string_view<CharT,Traits> sv ) const noexcept;

// (2) Const version only
constexpr bool ends_with( CharT c ) const noexcept;

// (3) Const version only
constexpr bool ends_with( const CharT* s ) const;

检查字符串视图是否以给定后缀结尾,其中:

  • (1) 后缀是一个字符串视图。
    实际返回 size() >= sv.size() && compare(size() - sv.size(), npos, sv) == 0

  • (2) 后缀是单个字符。
    实际返回 !empty() && Traits::eq(back(), c)

  • (3) 后缀是空终止字符字符串。
    实际返回 ends_with(basic_string_view(s))

参数

  • s - 一个字符串视图,可能是从另一个 std::basic_string_view 隐式转换的结果。
  • c - 一个字符
  • s - 一个空终止字符字符串视图。

返回值

如果视图以提供的后缀结尾,则为 true,否则为 false

复杂度

  • (1) 线性于 sv 的大小 - O(sv.size())
  • (2) 常数 - O(1)
  • (3) 线性于 s 的大小 - O(std::strlen(s))

备注

特性测试宏:__cpp_lib_ends_ends_with

示例

#include <iostream>
#include <string_view>

auto main() -> int
{
using namespace std::literals;

std::cout
<< std::boolalpha

// (1) bool ends_with( basic_string_view sv ) const noexcept;
<< std::string_view("https://cppreference.cn").ends_with(".com"sv) << ' ' // true
<< std::string_view("https://cppreference.cn").ends_with(".org"sv) << ' ' // false

// (2) bool ends_with( CharT c ) const noexcept;
<< std::string_view("C++20").ends_with('0') << ' ' // true
<< std::string_view("C++20").ends_with('3') << ' ' // false

// (3) bool ends_with( const CharT* s ) const;
<< std::string_view("string_view").ends_with("view") << ' ' // true
<< std::string_view("string_view").ends_with("View") << ' ' // false
<< '\n';
}
输出
true false true false true false
本文来源于此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”查看此文档的所有更改。
悬停查看原始许可证。

std::string_view ends_with() 方法

// (1) Const version only
constexpr bool ends_with( std::basic_string_view<CharT,Traits> sv ) const noexcept;

// (2) Const version only
constexpr bool ends_with( CharT c ) const noexcept;

// (3) Const version only
constexpr bool ends_with( const CharT* s ) const;

检查字符串视图是否以给定后缀结尾,其中:

  • (1) 后缀是一个字符串视图。
    实际返回 size() >= sv.size() && compare(size() - sv.size(), npos, sv) == 0

  • (2) 后缀是单个字符。
    实际返回 !empty() && Traits::eq(back(), c)

  • (3) 后缀是空终止字符字符串。
    实际返回 ends_with(basic_string_view(s))

参数

  • s - 一个字符串视图,可能是从另一个 std::basic_string_view 隐式转换的结果。
  • c - 一个字符
  • s - 一个空终止字符字符串视图。

返回值

如果视图以提供的后缀结尾,则为 true,否则为 false

复杂度

  • (1) 线性于 sv 的大小 - O(sv.size())
  • (2) 常数 - O(1)
  • (3) 线性于 s 的大小 - O(std::strlen(s))

备注

特性测试宏:__cpp_lib_ends_ends_with

示例

#include <iostream>
#include <string_view>

auto main() -> int
{
using namespace std::literals;

std::cout
<< std::boolalpha

// (1) bool ends_with( basic_string_view sv ) const noexcept;
<< std::string_view("https://cppreference.cn").ends_with(".com"sv) << ' ' // true
<< std::string_view("https://cppreference.cn").ends_with(".org"sv) << ' ' // false

// (2) bool ends_with( CharT c ) const noexcept;
<< std::string_view("C++20").ends_with('0') << ' ' // true
<< std::string_view("C++20").ends_with('3') << ' ' // false

// (3) bool ends_with( const CharT* s ) const;
<< std::string_view("string_view").ends_with("view") << ' ' // true
<< std::string_view("string_view").ends_with("View") << ' ' // false
<< '\n';
}
输出
true false true false true false
本文来源于此 CppReference 页面。它可能为了改进或编辑者的偏好而进行了修改。点击“编辑此页面”查看此文档的所有更改。
悬停查看原始许可证。