Namespaces
Variants

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

From cppreference.net
std::basic_string
basic_string & assign ( const basic_string & str ) ;
(1) (自 C++20 起为 constexpr)
basic_string & assign ( basic_string && str ) noexcept ( /* 见下文 */ ) ;
(2) (自 C++11 起)
(自 C++20 起为 constexpr)
basic_string & assign ( size_type count, CharT ch ) ;
(3) (自 C++20 起为 constexpr)
basic_string & assign ( const CharT * s, size_type count ) ;
(4) (自 C++20 起为 constexpr)
basic_string & assign ( const CharT * s ) ;
(5) (自 C++20 起为 constexpr)
template < class SV >
basic_string & assign ( const SV & t ) ;
(6) (自 C++17 起)
(自 C++20 起为 constexpr)
template < class SV >

basic_string & assign ( const SV & t,

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

替换字符串的内容。

1) 等价于 return * this = str ;
2) 等价于 return * this = std :: move ( str ) ;
3) 将内容替换为字符 ch count 个副本。
等效于 clear ( ) ; resize ( n, c ) ; return * this ;
4) 将内容替换为范围 [ s , s + count ) 中字符的副本。
如果 [ s , s + count ) 不是有效的 范围 ,则行为未定义。
5) 等价于 return assign ( s, Traits :: length ( s ) ) ;
6,7) 将内容替换为由 t 构造的字符串视图 sv 中的字符。
  • 若仅提供 t ,则将内容替换为 sv 中的所有字符。
  • 若同时提供 pos
    • count npos ,则将内容替换为 sv 中从 pos 开始的所有字符。
    • 否则,将内容替换为 std:: min ( count, sv. size ( ) - pos ) 个从 pos 开始的 sv 中的字符。
这些重载仅在满足以下所有条件时参与重载决议:
6) 等价于 std:: basic_string_view < CharT, Traits > sv = t ;
return assign ( sv. data ( ) , sv. size ( ) ) ;
7) 等价于 std:: basic_string_view < CharT, Traits > sv = t ;
return assign ( sv. substr ( pos, count ) ) ;
8) 将内容替换为 str 中的字符。
  • count npos ,则将内容替换为从 pos 起始的 str 全部字符。
  • 否则,将内容替换为从 pos 起始的 std:: min ( count, str. size ( ) - pos ) str 字符。
等价于 return assign ( std:: basic_string_view < CharT, Traits >
( str ) . substr ( pos, count ) ) ;
(C++20 起)
9) 等价于 return assign ( basic_string ( first, last, get_allocator ( ) ) ) ;

此重载仅在 InputIt 满足 LegacyInputIterator 要求时参与重载决议。

(C++11 起)
10) 等价于 return assign ( ilist. begin ( ) , ilist. size ( ) ) ;

目录

参数

str - 用作初始化字符来源的字符串
count - 结果字符串的大小
ch - 用于初始化字符串字符的值
s - 用作字符串初始化来源的字符字符串指针
t - 用于初始化字符串字符的对象(可转换为 std::basic_string_view
pos - 起始字符的索引位置
first, last - 复制字符的源范围
ilist - 用于初始化字符串字符的 std::initializer_list

返回值

* this

异常

2)
noexcept 规范:
noexcept ( std:: allocator_traits < Allocator > ::

propagate_on_container_move_assignment :: value ||

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

如果该操作将导致 size() 超过 max_size() ,则抛出 std::length_error

7) pos > sv. size ( ) true ,则抛出 std::out_of_range
8) pos > str. size ( ) true ,则抛出 std::out_of_range

若因任何原因抛出异常,此函数不产生任何效果( 强异常安全保证 )。

示例

#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(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"
}

输出:

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

缺陷报告

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

缺陷报告 应用于 发布时的行为 正确行为
LWG 847 C++98 未提供异常安全保证 增加强异常安全保证
LWG 2063 C++11 非规范性说明指出重载 ( 2 )
可通过交换实现
修正为要求移动赋值
LWG 2250 C++98 pos > str. size ( ) true
重载 ( 8 ) 行为未定义
此情况下始终抛出异常
LWG 2579 C++98 重载 ( 1 ) 与拷贝赋值运算符
具有不同效果
两者效果相同
LWG 2946 C++17 重载 ( 6 ) 在某些情况下引发歧义 通过模板化避免歧义

另请参阅

将字符范围赋值给字符串
(公开成员函数)
构造 basic_string
(公开成员函数)
为字符串赋值
(公开成员函数)