- string_view[meta header]
- std[meta namespace]
- basic_string_view[meta class]
- function[meta id-type]
- cpp20[meta cpp]
constexpr bool starts_with(basic_string_view x) const noexcept; // (1)
constexpr bool starts_with(CharT x) const noexcept; // (2)
constexpr bool starts_with(const CharT* x) const; // (3)指定の文字列で始まるかを判定する。
- (1) :
*thisが参照する文字列範囲の先頭が、xが参照する文字列範囲と一致するかを判定する - (2) :
*thisが参照する文字列範囲の先頭が、文字xと一致するかを判定する - (3) :
*thisが参照する文字列範囲の先頭が、文字列xと一致するかを判定する
-
(1) : 以下と等価である
return substr(0, x.size()) == x;
- substr[link substr.md]
- x.size()[link size.md]
-
(2) : 以下と等価である
return !empty() && Traits::eq(front(), x);- empty()[link empty.md]
- Traits::eq[link /reference/string/char_traits/eq.md]
- front()[link front.md]
-
(3) : 以下と等価である
return starts_with(basic_string_view(x));
- (1), (2) : 投げない
#include <cassert>
#include <string_view>
int main()
{
const std::string_view sv = "aaabbbcccdddeee";
// (1)
{
std::string_view svx = "aaa";
assert(sv.starts_with(svx));
}
// (2)
{
assert(sv.starts_with('a'));
}
// (3)
{
assert(sv.starts_with("aaa"));
}
}- starts_with[color ff0000]
- C++20
- Clang: 7.0 [mark verified]
- GCC: 9.1 [mark verified]
- Visual C++: ??