例如,我有两个字符串对象:string str_1,str_2.我想连接他们.我可以使用两种方法:方法1:
std::stringstream ss;
//std::string str_1("hello");
//std::string str_2("world");
ss << "hello"<< "world"?
const std::string dst_str = std::move(ss.str());
Run Code Online (Sandbox Code Playgroud)
方法2:
std::string str_1("hello");
std::string str_2("world");
const std::string dst_str = str_1 + str_2;
Run Code Online (Sandbox Code Playgroud)
因为字符串的缓冲区是只读的,所以当您更改字符串对象时,其缓冲区将销毁并创建一个新的缓冲区来存储新内容.方法1比方法2好吗?我的理解是否正确?
归档时间: |
|
查看次数: |
1422 次 |
最近记录: |