命名空间
变体
操作

std::experimental::filesystem::path::compare

来自 cppreference.cn
< cpp‎ | experimental‎ | fs‎ | path
 
 
实验性
技术规范
文件系统库 (filesystem TS)
库基础 (library fundamentals TS)
库基础 2 (library fundamentals TS v2)
库基础 3 (library fundamentals TS v3)
并行扩展 (parallelism TS)
并行扩展 2 (parallelism TS v2)
并发扩展 (concurrency TS)
并发扩展 2 (concurrency TS v2)
概念 (concepts TS)
范围 (ranges TS)
反射 (reflection TS)
数学特殊函数 (special functions TR)
实验性非 TS
模式匹配
线性代数
std::execution
契约
2D 图形
 
 
 
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)

比较路径和另一个路径的词法表示。

1) 如果路径的本机表示 (native()) 分别在字典序上小于、等于或大于 p 的本机表示 (p.native()),则返回值小于、等于或大于 0。比较是逐个元素进行的,如同从 begin() 到 end() 迭代两个路径一样。
2) 等效于 compare(path(str))
3) 等效于 compare(path(s))

目录

[编辑] 参数

p - 要比较的路径
str - 表示要比较的路径的字符串
s - 表示要比较的路径的空终止字符串

[编辑] 返回值

如果路径在字典序上小于给定路径,则返回小于 0 的值。

如果路径在字典序上等于给定路径,则返回值等于 0

如果路径在字典序上大于给定路径,则返回值大于 0

[编辑] 异常

2,3) 可能抛出实现定义的异常。

[编辑] 注释

对于双向比较,二进制运算符可能更适合。

[编辑] 示例

#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/_");
}

输出

"/a/b/" > "/a/b/#"
"/a/b/" < "a/b/_"

[编辑] 参见

按字典序比较两个路径
(函数) [编辑]