Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::filesystem::path::append,std::filesystem::path::operator/=

      From cppreference.com
      <cpp‎ |filesystem‎ |path
       
       
      Filesystem library
      Classes
      Functions
      File types
       
      std::filesystem::path
      Member types
      Member constants
      Member functions
      Path decomposition
      Non-member functions
      (until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
      Helper classes
       
      path& operator/=(const path& p);
      (1)(since C++17)
      template<class Source>
      path& operator/=(const Source& source);
      (2)(since C++17)
      template<class Source>
      path& append(const Source& source);
      (3)(since C++17)
      template<class InputIt>
      path& append( InputIt first, InputIt last);
      (4)(since C++17)
      1) Ifp.is_absolute()||(p.has_root_name()&& p.root_name()!= root_name()), then replaces the current path withp as if byoperator=(p) and finishes.
      * Otherwise, ifp.has_root_directory(), then removes any root directory and the entire relative path from the generic format pathname of*this.
      * Otherwise, ifhas_filename()||(!has_root_directory()&& is_absolute()), then appendspath::preferred_separator to the generic format of*this.
      * Either way, then appends the native format pathname ofp, omitting anyroot-name from its generic format, to the native format of*this.
      // Where "//host" is a root-namepath("//host")/"foo"// the result is      "//host/foo" (appends with separator)path("//host/")/"foo"// the result is also "//host/foo" (appends without separator) // On POSIX,path("foo")/""// the result is "foo/" (appends)path("foo")/"/bar";// the result is "/bar" (replaces) // On Windows,path("foo")/"C:/bar";// the result is "C:/bar" (replaces)path("foo")/"C:";// the result is "C:"     (replaces)path("C:")/"";// the result is "C:"     (appends, without separator)path("C:foo")/"/bar";// yields "C:/bar"        (removes relative path, then appends)path("C:foo")/"C:bar";// yields "C:foo/bar"     (appends, omitting p's root-name)
      2,3) Same as(1), but accepts anystd::basic_string,std::basic_string_view, null-terminated multicharacter string, or an input iterator pointing to a null-terminated multicharacter sequence. Equivalent toreturn operator/=(path(source));.
      4) Same as(1), but accepts any iterator pair that designates a multicharacter string. Equivalent toreturn operator/=(path(first, last));.

      (2) and(3) participate in overload resolution only ifSource andpath are not the same type, and either:

      Contents

      [edit]Parameters

      p - pathname to append
      source -std::basic_string,std::basic_string_view, null-terminated multicharacter string, or an input iterator pointing to a null-terminated multicharacter sequence, which represents a path name (either in portable or in native format)
      first, last - pair ofLegacyInputIterators that specify a multicharacter sequence that represents a path name
      Type requirements
      -
      InputIt must meet the requirements ofLegacyInputIterator.
      -
      The value type ofInputIt must be one of the encoded character types (char,wchar_t,char16_t andchar32_t).

      [edit]Return value

      *this

      [edit]Exceptions

      May throwstd::bad_alloc if memory allocation fails.

      [edit]Notes

      These functions effectively yield an approximation of the meaning of the argument pathp in an environment where*this is the starting directory.

      [edit]Example

      The output is produced on Windows.

      Run this code
      #include <filesystem>#include <iostream>namespace fs= std::filesystem; int main(){    fs::path p1="C:";    p1/="Users";// does not insert a separatorstd::cout<<"\"C:\" /\"Users\" == "<< p1<<'\n';    p1/="batman";// inserts fs::path::preferred_separator, '\' on Windowsstd::cout<<"\"C:\" /\"Users\" /\"batman\" == "<< p1<<'\n';}

      Possible output:

      "C:" / "Users" == "C:Users""C:" / "Users" / "batman" == "C:Users\\batman"

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3244C++17constraint thatSource cannot bepath was missingadded

      [edit]See also

      concatenates two paths without introducing a directory separator
      (public member function)[edit]
      (C++17)
      concatenates two paths with a directory separator
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/filesystem/path/append&oldid=158013"

      [8]ページ先頭

      ©2009-2025 Movatter.jp