std::basic_...iew::compare(3)
π std::basic_...iew::compare(3) π
okay so this is the std::basic_...iew::compare(3) page from the C++ Standard Libary manual β yes, that's the spelling up top, and honestly iconic of it β and we're about to make string-view comparing make sense, bestie π
π NAME π
std::basic_...iew::compare β std::basic_...iew::compare β¨
that's the name, that's the whole name, and yes it repeats itself. we love a consistent queen π
β¨ Synopsis β¨
six overloads, all constexpr, all (since C++17), and here's the exact synopsis so you can copy-paste it like the main character you are π«Ά
constexpr int compare( basic_string_view v ) const noexcept;
(1) (since C++17)
constexpr int compare( size_type pos1, size_type count1,
(2) (since C++17)
basic_string_view v ) const;
constexpr int compare( size_type pos1, size_type count1,
basic_string_view v,
(3) (since C++17)
size_type pos2, size_type count2 ) const;
constexpr int compare( const CharT* s ) const;
(4) (since C++17)
constexpr int compare( size_type pos1, size_type count1,
(5) (since C++17)
const CharT* s ) const;
constexpr int compare( size_type pos1, size_type count1,
(6) (since C++17)
const CharT* s, size_type count2 ) const;
Compares two character sequences. periodt πΈ
- The length
rlenof the sequences to compare is the smaller ofsize()andv.size(). The function compares the two views by callingtraits::compare(data(), v.data(), rlen), and returns a value according to the following table β but tables are not it, so here it is as bullets, bestie:- If
Traits::compare(data(), v.data(), rlen) < 0: this is less thanv, return< 0. - If
Traits::compare(data(), v.data(), rlen) == 0andsize() < v.size(): this is less thanv, return< 0. - If
Traits::compare(data(), v.data(), rlen) == 0andsize() == v.size(): this is equal tov, return0. - If
Traits::compare(data(), v.data(), rlen) == 0andsize() > v.size(): this is greater thanv, return> 0. - If
Traits::compare(data(), v.data(), rlen) > 0: this is greater thanv, return> 0.
- If
- Equivalent to
substr(pos1, count1).compare(v). - Equivalent to
substr(pos1, count1).compare(v.substr(pos2, count2)). - Equivalent to
compare(basic_string_view(s)). - Equivalent to
substr(pos1, count1).compare(basic_string_view(s)). - Equivalent to
substr(pos1, count1).compare(basic_string_view(s, count2)).
πΈ Parameters πΈ
vβ view to comparesβ pointer to the character string to compare tocount1β number of characters of this view to comparepos1β position of the first character in this view to comparecount2β number of characters of the given view to comparepos2β position of the first character of the given view to compare
π¦ Return value π¦
Negative value if this view is less than the other character sequence, zero if the both character sequences are equal, positive value if this view is greater than the other character sequence. cute, clean, ordered β we love a predictable return type π
β Complexity β
1) Linear in the number of characters compared.
π₯ Example π₯
the page literally says // Run this code, so yeah β run it, bestie π
#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 π
compareβ compares two strings β public member function ofstd::basic_string<CharT,Traits,Allocator>operator==,operator!=,operator<,operator>,operator<=,operator>=,operator<=>β lexicographically compares two string views β(function template)- version tags stacked on the page, in order:
(C++17),(removed in C++20),(removed in C++20),(removed in C++20),(removed in C++20),(removed in C++20),(C++20)
mwah π this lil lesson came from http://cppreference.com, stamped 2024.06.10, and it's the std::basic_...iew::compare(3) page β bye bestie π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms