Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_string<CharT,Traits,Allocator>::assign

      From cppreference.com
      <cpp‎ |string‎ |basic string
       
       
       
      std::basic_string
       
      basic_string& assign(const basic_string& str);
      (1)(constexpr since C++20)
      basic_string& assign( basic_string&& str)noexcept(/* see below */);
      (2)(since C++11)
      (constexpr since C++20)
      basic_string& assign( size_type count, CharT ch);
      (3)(constexpr since C++20)
      basic_string& assign(const CharT* s, size_type count);
      (4)(constexpr since C++20)
      basic_string& assign(const CharT* s);
      (5)(constexpr since C++20)
      template<class SV>
      basic_string& assign(const SV& t);
      (6)(since C++17)
      (constexpr since C++20)
      template<class SV>

      basic_string& assign(const SV& t,

                            size_type pos, size_type count= npos);
      (7)(since C++17)
      (constexpr since C++20)
      (8)
      basic_string& assign(const basic_string& str,
                            size_type pos, size_type count);
      (until C++14)
      basic_string& assign(const basic_string& str,
                            size_type pos, size_type count= npos);
      (since C++14)
      (constexpr since C++20)
      template<class InputIt>
      basic_string& assign( InputIt first, InputIt last);
      (9)(constexpr since C++20)
      basic_string& assign(std::initializer_list<CharT> ilist);
      (10)(since C++11)
      (constexpr since C++20)

      Replaces the contents of the string.

      1) Equivalent toreturn*this= str;.
      2) Equivalent toreturn*this= std::move(str);.
      3) Replaces the contents withcount copies of characterch.
      Equivalent toclear(); resize(n, c);return*this;.
      4) Replaces the contents with copies of the characters in the range[ss+ count).
      If[ss+ count) is not avalid range, the behavior is undefined.
      5) Equivalent toreturn assign(s, Traits::length(s));.
      6,7) Replaces the contents with characters in a string viewsv constructed fromt.
      • If onlyt is provided, replaces the contents with all characters insv.
      • Ifpos is also provided:
        • Ifcount isnpos, replaces the contents with all characters insv starting frompos.
        • Otherwise, replaces the contents with thestd::min(count, sv.size()- pos) characters insv starting frompos.
      These overloads participate in overload resolution only if all following conditions are satisfied:
      6) Equivalent tostd::basic_string_view<CharT, Traits> sv= t;
      return assign(sv.data(), sv.size());
      .
      7) Equivalent tostd::basic_string_view<CharT, Traits> sv= t;
      return assign(sv.substr(pos, count));
      .
      8) Replaces the contents with characters instr.
      • Ifcount isnpos, replaces the contents with all characters instr starting frompos.
      • Otherwise, replaces the contents with thestd::min(count, str.size()- pos) characters instr starting frompos.
      Equivalent toreturn assign(std::basic_string_view<CharT, Traits>
                       (str).substr(pos, count));
      .
      (since C++20)
      9) Equivalent toreturn assign(basic_string(first, last, get_allocator()));.

      This overload participates in overload resolution only ifInputIt satisfies the requirements ofLegacyInputIterator.

      (since C++11)
      10) Equivalent toreturn assign(ilist.begin(), ilist.size());.

      Contents

      [edit]Parameters

      str - string to be used as source to initialize the characters with
      count - size of the resulting string
      ch - value to initialize characters of the string with
      s - pointer to a character string to use as source to initialize the string with
      t - object (convertible tostd::basic_string_view) to initialize the characters of the string with
      pos - index of the first character to take
      first, last - range to copy the characters from
      ilist -std::initializer_list to initialize the characters of the string with

      [edit]Return value

      *this

      [edit]Exceptions

      2)
      noexcept specification:  
      noexcept(std::allocator_traits<Allocator>::

                   propagate_on_container_move_assignment::value||

               std::allocator_traits<Allocator>::is_always_equal::value)

      If the operation would causesize() to exceedmax_size(), throwsstd::length_error.

      7) Ifpos> sv.size() istrue, throwsstd::out_of_range.
      8) Ifpos> str.size() istrue, throwsstd::out_of_range.

      If an exception is thrown for any reason, this function has no effect (strong exception safety guarantee).

      [edit]Example

      Run this code
      #include <iostream>#include <iterator>#include <string> int main(){std::string s;// assign(size_type count, CharT ch)    s.assign(4,'=');std::cout<< s<<'\n';// "====" std::stringconst c("Exemplary");// assign(const basic_string& str)    s.assign(c);std::cout<< c<<" == "<< s<<'\n';// "Exemplary == Exemplary" // assign(const basic_string& str, size_type pos, size_type count)    s.assign(c,0, c.length()-1);std::cout<< s<<'\n';// "Exemplar"; // assign(basic_string&& str)    s.assign(std::string("C++ by ")+"example");std::cout<< s<<'\n';// "C++ by example" // assign(const CharT* s, size_type count)    s.assign("C-style string",7);std::cout<< s<<'\n';// "C-style" // assign(const CharT* s)    s.assign("C-style\0string");std::cout<< s<<'\n';// "C-style" char mutable_c_str[]="C-style string";// assign(InputIt first, InputIt last)    s.assign(std::begin(mutable_c_str),std::end(mutable_c_str)-1);std::cout<< s<<'\n';// "C-style string" // assign(std::initializer_list<CharT> ilist)    s.assign({'C','-','s','t','y','l','e'});std::cout<< s<<'\n';// "C-style"}

      Output:

      ====Exemplary == ExemplaryExemplarC++ by exampleC-styleC-styleC-style stringC-style

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 847C++98there was no exception safety guaranteeadded strong exception safety guarantee
      LWG 2063C++11non-normative note stated that overload(2)
      can be implemented by swapping
      corrected to require move assignment
      LWG 2250C++98the behavior of overload(8) was
      undefined ifpos> str.size() istrue
      always throws an exception in this case
      LWG 2579C++98overload(1) and the copy assignment
      operator had different effects
      they have the same effect
      LWG 2946C++17overload(6) caused ambiguity in some casesavoided by making it a template

      [edit]See also

      assign a range of characters to a string
      (public member function)[edit]
      constructs abasic_string
      (public member function)[edit]
      assigns values to the string
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/basic_string/assign&oldid=178467"

      [8]ページ先頭

      ©2009-2025 Movatter.jp