std::basic_string_view<CharT,Traits>::compare
From cppreference.com
< cpp | string | basic string view
| constexpr int compare( basic_string_view v ) const noexcept; |
(1) | (C++17以降) |
| constexpr int compare( size_type pos1, size_type count1, basic_string_view v ) const; |
(2) | (C++17以降) |
| constexpr int compare( size_type pos1, size_type count1, basic_string_view v, size_type pos2, size_type count2 ) const; |
(3) | (C++17以降) |
| constexpr int compare( const CharT* s ) const; |
(4) | (C++17以降) |
| constexpr int compare( size_type pos1, size_type count1, const CharT* s ) const; |
(5) | (C++17以降) |
| constexpr int compare( size_type pos1, size_type count1, const CharT* s, size_type count2 ) const; |
(6) | (C++17以降) |
2つの文字シーケンスを比較します。
1) 比較対象のシーケンスの長さ
rlenは、size()とv.size()の小さい方です。この関数は、traits::compare(data(), v.data(), rlen)を呼び出して2つのビューを比較し、以下の表に従って値を返します。| 条件 | 結果 | 戻り値 | |
|---|---|---|---|
Traits::compare(data(), v.data(), rlen) < 0
|
this は v より 小さい |
< 0 | |
Traits::compare(data(), v.data(), rlen) == 0
|
size() < v.size()
|
this は v より 小さい |
< 0 |
size() == v.size()
|
this は v と 等しい |
0 | |
size() > v.size()
|
this は v より 大きい |
> 0 | |
Traits::compare(data(), v.data(), rlen) > 0
|
this は v より 大きい |
> 0 | |
2) substr(pos1, count1).compare(v) と同等です。
3) substr(pos1, count1).compare(v.substr(pos2, count2)) と同等です。
4) compare(basic_string_view(s)) と同等です。
5) substr(pos1, count1).compare(basic_string_view(s)) と同等です。
6) substr(pos1, count1).compare(basic_string_view(s, count2)) と同等です。
目次 |
[編集] Parameters
| v | - | 比較対象のビュー |
| s | - | 比較対象の C 文字列へのポインタ |
| count1 | - | このビューのうち比較する文字数 |
| pos1 | - | このビューで比較する最初の文字の位置 |
| count2 | - | 指定されたビューのうち比較する文字数 |
| pos2 | - | 指定されたビューの最初の文字の位置 |
[編集] Return value
このビューが他の文字シーケンスより小さい場合は負の値、両方の文字シーケンスが等しい場合はゼロ、このビューが他の文字シーケンスより大きい場合は正の値を返します。
[編集] Complexity
1) 比較される文字数に対して線形。
[編集] Example
このコードを実行
#include <string_view> int main() { using std::operator""sv; static_assert("abc"sv.compare("abcd"sv) < 0); static_assert("abcd"sv.compare("abc"sv) > 0); static_assert("abc"sv.compare("abc"sv) == 0); static_assert(""sv.compare(""sv) == 0); }
[編集] See also
| 2つの文字列を比較する ( std::basic_string<CharT,Traits,Allocator> の public member function) | |
| (C++17)(C++20で削除)(C++20で削除)(C++20で削除)(C++20で削除)(C++20で削除)(C++20) |
2つのstring viewを辞書順で比較する (function template) |