这个问题在这里已经有了答案:
std::string::assign vs std::string::operator=
(2 个回答)
2年前关闭。
例如,这段代码
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) Replaces the contents with a copy of str. Equivalent to
*this = str
;. In particular, allocator propagation may take place. (since C++11)
所以这也是一样的。
关于c++字符串使用assign函数和直接使用 '='改变值的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60375577/