- string_view[meta header]
- std[meta namespace]
- basic_string_view[meta class]
- function[meta id-type]
- cpp23[meta cpp]
constexpr bool contains(basic_string_view x) const noexcept; // (1) C++23
constexpr bool contains(charT x) const noexcept; // (2) C++23
constexpr bool contains(const charT* x) const; // (3) C++23
指定の文字・文字列が含まれているかを判定する。
- (1) :
*this
が参照する文字列範囲に、x
が参照する文字列範囲を含んでいるかを判定する - (2) :
*this
が参照する文字列範囲に、文字x
が含まれているかを判定する - (3) :
*this
が参照する文字列範囲に、文字列x
が含まれているかを判定する
この関数は、find
(x) != npos
の代わりに使用できる。
- 以下と等価である
return find(x) != npos;
- find[link find.md]
- (1), (2) : 投げない
#include <cassert>
#include <string_view>
int main() {
std::string_view sv = "Hello World";
// (1)
{
std::string_view target = "rl";
assert(sv.contains(target));
}
// (2)
{
assert(sv.contains('W'));
}
// (3)
{
assert(sv.contains("rl"));
}
}
- contains[color ff0000]
- C++20
- Clang: 12.0 [mark verified]
- GCC:
- Visual C++: ??