在线文档教程
C++
字符串 | Strings

std::basic_string::assign

性病:基本[医]字符串::分配

basic_string& assign( size_type count, CharT ch (1)
basic_string& assign( const basic_string& str (2)
(3)
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)
basic_string& assign( basic_string&& str (4)(since C++11)
basic_string& assign( const CharT* s, size_type count (5)
basic_string& assign( const CharT* s (6)
template< class InputIt > basic_string& assign( InputIt first, InputIt last (7)
basic_string& assign( std::initializer_list<CharT> ilist (8)(since C++11)
basic_string& assign( std::basic_string_view<CharT, Traits> sv(9)(since C++17)
template < class T > basic_string& assign( const T& t, size_type pos, size_type count = npos(10)(since C++17)

替换字符串的内容。

1%29将内容替换为count字符副本ch...

2%29将内容替换为str...

3%29用子字符串替换内容。[pos, pos+count)成str如果请求的子字符串持续到字符串的末尾,或者count == npos,生成的子字符串是[pos, str.size()).如果pos > str.size(),,,std::out_of_range被扔了。

4%29将内容替换为str使用移动语义。相当于*this = std::move(str)...

5%29将内容替换为第一个count所指向的字符串字符s...s可以包含空字符。

6%29将内容替换为s字符串的长度由第一个空字符决定。

7%29将内容替换为范围内字符的副本。[first, last)此重载不参与重载解决方案。InputIt不满意InputIterator.%28自C++11%29

8%29将内容替换为初始化程序列表的内容。ilist...

9%29将内容替换为字符串视图的内容。sv,好像assign(sv.data(), sv.size())

10%29名皈依者t到字符串视图sv好像std::basic_string_view<CharT, Traits> sv = t;,然后将内容替换为子视图中的字符。[pos, pos+count)成sv如果所请求的子视图持续到sv,或者如果count == npos,生成的子视图是[pos, sv.size()).如果pos > sv.size(),,,std::out_of_range被扔了。此重载只参与在下列情况下的重载解决方案:std::is_convertible_v<const T&,std::basic_string_view<CharT, Traits>>是true和std::is_convertible_v<const T&, const CharT*>是false...

参数

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

类型要求

-输入必须符合输入器的要求。

返回值

*this...

复杂性

1%29线性count

2%29线性str

3%29线性count

4%29常数。如果alloc被赋予和alloc != other.get_allocator(),然后是线性的。

5%29线性count

6%29线性s

7%29直线之间的距离firstlast

8%29线性init

例外

如果出于任何原因引发异常,则此函数没有效果%28强异常保证%29。%28自C++11%29。

如果手术会导致size() > max_size(),抛std::length_error...

4) noexcept specification: noexcept(since C++11)(until C++17)
4) noexcept specification: noexcept(std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value || std::allocator_traits<Allocator>::is_always_equal::value)(since C++17)

二次

#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::string const c("Exemplary" // assign(basic_string const& str) s.assign(c std::cout << c << "==" << s <<'\n'; // "Exemplary == Exemplary" // assign(basic_string const& 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(charT const* s, size_type count) s.assign("C-style string", 7 std::cout << s << '\n'; // "C-style" // assign(charT const* 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" }

二次

产出:

二次

==== Exemplary==Exemplary Exemplar C++ by example C-style C-style C-style string C-style

二次

缺陷报告

以下行为更改缺陷报告追溯应用于先前发布的C++标准。

DRApplied toBehavior as publishedCorrect behavior
LWG 2063C++11non-normative note stated that swap is a valid implementation of move-assigncorrected to require move assignment

另见

(constructor)constructs a basic_string (public member function)
operator=assigns values to the string (public member function)

© cppreference.com

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

http://en.cppreference.com/w/cpp/string/basic[医]字符串/赋值