C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别

c++ the difference between string using the assign function and directly using '=' to change the value

本文关键字:字符串 之间 区别 函数 assign 使用 C++      更新时间:2023-10-16

例如,这个代码

std::string a("this is a string");
std::string b;
b = a;
std::string c;
c.assign(a);

B和C在本质上有什么区别吗

来自cppreference

2(basic_string& assign( const basic_string& str );

2( 将内容替换为str的副本。等效于*this = str;。特别是,可能会发生分配器传播。(自C++11起(

所以这也是一样的。