std::filesystem::path::compare
文件系统::path::比较
int compare( const path& p ) const; | (1) | (since C++17) |
---|---|---|
int compare( const string_type& str ) const; int compare( std::basic_string_view<value_type> str ) const; | (2) | (since C++17) |
int compare( const value_type* s ) const; | (3) | (since C++17) |
比较路径和其他路径的词法表示形式。
如果路径%28的本机表示形式小于、等于或大于0,则1%29返回一个值土生土长%28%29%29在字典上分别小于、等于或大于p
%28p.native()
29%。比较是按元素执行的,就好像通过从开始%28%29端部%28%29
2%29相当于compare(path(str))
...
3%29相当于compare(path(s))
...
参数
p | - | a path to compare to |
---|---|---|
str | - | a string or string view representing path to compare to |
s | - | a null-terminated string representing path to compare to |
返回值
如果路径按字典顺序小于给定路径,则为小于0的值。
如果路径按字典顺序等于给定路径的值,则等于0。
如果路径按字典顺序大于给定路径,则为大于0的值。
例外
1%29
noexcept
规格:
noexcept
2-3%29%280%29
注记
为了进行双向比较,二元算子也许更合适。
例
二次
#include <iostream>
#include <filesystem>
namespace fs = std::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/_"
}
二次
产出:
二次
"/a/b/" > "/a/b/#"
"/a/b/" < "a/b/_"
二次
另见
operator==operator!=operator | lexicographically compares two paths (function) |
---|
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。