首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::filesystem::path::compare

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

注记

为了进行双向比较,二元算子也许更合适。

二次

代码语言:javascript
复制
#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/_");
}

二次

产出:

二次

代码语言:javascript
复制
"/a/b/" > "/a/b/#"
"/a/b/" < "a/b/_"

二次

另见

operator==operator!=operator<operator<=operator>operator>=

lexicographically compares two paths (function)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券