std::experi...th::compare(3)
π std::experi...th::compare(3) π
okay so bestie, this page is std::experi...th::compare(3), and it comes from the C++ Standard Libary (yes, that spelling is literally whatβs in the manual π). Itβs all about comparing the lexical representations of paths in the filesystem TS. Letβs get into it π«Ά.
β¨ NAME β¨
std::experimental::filesystem::path::compare β std::experimental::filesystem::path::compare
π¦ Synopsis π¦
int compare( const path& p ) const noexcept; (1) (filesystem TS)
int compare( const string_type& str ) const; (2) (filesystem TS)
int compare( const value_type* s ) const; (3) (filesystem TS)
Compares the lexical representations of the path and another path.
- Returns a value less than, equal to or greater than
0if the native representation of the path (native()) is respectively lexicographically less than, equal to or greater than the native representation ofp(p.native()). Comparison is performed element-wise, as if by iterating both paths frombegin()toend(). - Equivalent to
compare(path(str)). - Equivalent to
compare(path(s)).
πΈ Parameters πΈ
pβ a path to compare tostrβ a string representing path to compare tosβ a null-terminated string representing path to compare to
π Return value π
- A value less than
0if the path is lexicographically less than the given path. - A value equal to
0if the path is lexicographically equal to the given path. - A value greater than
0if the path is lexicographically greater than the given path.
π Exceptions π
2,3)May throw implementation-defined exceptions.
π₯ Notes π₯
For two-way comparisons, binary operators may be more suitable.
π Example π
// Run this code
#include <experimental/filesystem>
#include <iostream>
namespace fs = std::experimental::filesystem;
void demo(int rc, fs::path p1, fs::path p2)
{
if (rc < 0)
std::cout << p1 << " < " << p2 << '\n';
else if (rc > 0)
std::cout << p1 << " > " << p2 << '\n';
else if (rc == 0)
std::cout << p1 << " = " << p2 << '\n';
}
int main()
{
fs::path p1 = "/a/b/"; // as if "a/b/." for lexicographical iteration
fs::path p2 = "/a/b/#";
demo(p1.compare(p2), p1, p2);
demo(p1.compare("a/b/_"), p1, "a/b/_");
}
Output:
"/a/b/" > "/a/b/#"
"/a/b/" < "a/b/_"
π See also π
operator==operator!=operator<β lexicographically compares two paths (function)operator<=β (function)operator>operator>=
π§ Category π§
- Noindexed pages
byeee bestie π β this iconic page came from http://cppreference.com, and its version/date stamp is 2024.06.10 (page: std::experi...th::compare(3)). slay πβ¨
π¦ 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