简体   繁体   English

boost中的path::string()和path::generic_string()有什么区别?

[英]What is the difference between path::string() and path::generic_string() in boost?

boost::path::string()boost::path::generic_string()之间有什么区别,我应该什么时候使用它们?

This is clearly stated in the documentation ;这在文档中明确说明; you need only read the documentation to gain knowledge and understanding.您只需要阅读文档即可获得知识和理解。 Please get into the habit of doing that, starting from now.请养成这样做的习惯,从现在开始。

boost::path::string提升::路径::字符串

Returns a std::string in the native pathname format .本机路径名格式返回std::string

boost::path::generic_string boost::path::generic_string

Returns a std::string in the generic pathname format . 以通用路径名格式返回std::string

When to use each of them何时使用它们中的每一个

Well, that's up to you, and depends on your needs!好吧,这取决于您,取决于您的需求! The following quotation, again from the documentation, may help…以下引用再次来自文档,可能会有所帮助......

[Note: For ISO/IEC 9945, no conversion occurs, since the native format and generic format are the same. [注意:对于 ISO/IEC 9945,不会发生转换,因为本机格式和通用格式是相同的。 For Windows, backslashes are converted to slashes --end note]对于 Windows,反斜杠转换为斜杠 --end note]

In day-to-day use, you can effectively say:在日常使用中,您可以有效地说:

  • On Windows, native format has backslashes and generic format has slashes;在 Windows 上,本机格式有反斜杠,通用格式有斜杠;
  • On Linux, both formats have slashes.在 Linux 上,两种格式都有斜杠。

Reading your mind, you are programming on a Windows system.读你的心,你是在 Windows 系统上编程。

On your system, as far as boost can tell, the preferred separator between path elements is \\ .在您的系统上,就 boost 而言,路径元素之间的首选分隔符是\\ However, / is an acceptable separator.但是, /可接受的分隔符。

The constructor to boost::fs::path docs state: boost::fs::path文档的构造函数声明:

[Note: For ISO/IEC 9945 and Windows implementations, the generic format is already acceptable as a native format, so no generic to native conversion is performed. [注意:对于 ISO/IEC 9945 和 Windows 实现,通用格式已经可以作为本机格式接受,因此不会执行通用到本机的转换。 --end note] --结束注]

Note the clause about Windows implementations -- the generic format (with / separators) is already acceptable, so no conversion is done.请注意有关 Windows 实现的条款——通用格式(带/分隔符)已经可以接受,因此没有进行转换。

Then when you invoke t/fn the appends or / or /= operator is used.然后,当您调用t/fnappends//=运算符。 It states:它指出:

[Note: For ISO/IEC 9945-like implementations, including Unix variants, Linux, and Mac OS X, the preferred directory separator is a single forward slash. [注意:对于类似 ISO/IEC 9945 的实现,包括 Unix 变体、Linux 和 Mac OS X,首选的目录分隔符是单个正斜杠。

For Windows-like implementations, including Cygwin and MinGW, the preferred directory separator is a single backslash.--end note]对于类似 Windows 的实现,包括 Cygwin 和 MinGW,首选的目录分隔符是单个反斜杠。--end note]

And the preferred separator is \\ on windows systems.在 Windows 系统上,首选的分隔符是\\

So at construction, no conversion from generic to system occurs -- but on appending with operator/ or similar, it is.所以在构造时,不会发生从泛型到系统的转换——但是在附加operator/或类似的东西时,它是。

This results in your string looking ugly.这会导致您的字符串看起来很难看。

If you want to fix the problem, you could iterate over your 'malformed' path using begin and end , and store/append the elements into a new path using operator/ .如果你想解决这个问题,你可以使用beginend迭代你的“畸形”路径,并使用operator/将元素存储/附加到新路径中。

boost::fs::path fix( boost::fs::path in ) {
  boost::fs::path retval;
  for ( auto element : in ) {
    if (retval.empty())
      retval = in;
    else
      retval /= in;
  }
  return retval;
}

which if I read the docs right will take your mixed-slash path and generate a clean one.如果我正确阅读文档,它将采用您的混合斜杠路径并生成一个干净的路径。

If you are stuck in C++03 iterate over in using in.begin() and in.end() and boost::fs::path::iterator .如果您被困在 C++03 in使用in.begin()in.end()以及boost::fs::path::iterator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM