c++字符串使用assign函数和直接使用 '='改变值的区别

标签 c++ c++11

这个问题在这里已经有了答案:





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/

相关文章:

c++ - 在 32 位字位模式中找到 "edges"

c++ - 不要使用 C++ 在 Linux 中为 IPv6 数据包分段标志

c# - 嵌入式文件系统?

c++ - 类型特征 : Check if class have specific function (maybe inherit)

c++ - 如何将 C++11 std::stoi 与 gcc 一起使用?

c++ - 在 C++11 中定义只移动对象是否有意义?

c++ - 在运行时获取命名空间中的函数列表?

c++ - 这里有内存泄漏吗?试图通过函数传递对象

c++ - 功能模板与自动关键字

c++ - 是否泄漏 std::thread 未定义行为?