【发布时间】:2016-03-15 17:51:18
【问题描述】:
我很久以前在 Borland C++ 中编码,现在我试图理解“新”(对我而言)C+11(我知道,我们在 2015 年,有一个 c+14 ......但我正在开发一个 C++11 项目)
现在我有几种方法可以为字符串赋值。
#include <iostream>
#include <string>
int main ()
{
std::string test1;
std::string test2;
test1 = "Hello World";
test2.assign("Hello again");
std::cout << test1 << std::endl << test2;
return 0;
}
它们都有效。我从http://www.cplusplus.com/reference/string/string/assign/ 了解到,还有另一种使用assign 的方法。但是对于简单的字符串赋值,哪一个更好呢?我必须用 8 个 std:string 填充 100 多个结构,并且我正在寻找最快的机制(我不关心内存,除非有很大的不同)
【问题讨论】: