如何将char字符串转换为std字符串
问题描述:
我有以下代码,我无法弄清楚如何将char字符串转换为std字符串。有人能帮助我吗?如何将char字符串转换为std字符串
char Path[STRING_MAX];
test->getValue(Config::CODE,Path);
size_t found;
cout << "Splitting: " << Path << endl;
found=Path.find_last_of("/");
cout << " folder: " << Path.substr(0,found) << endl;
cout << " file: " << Path.substr(found+1) << endl;
答
使用此:
std::string sPath(Path);
或:
std::string sPath = Path;
或:
std::string sPath;
sPath.assign(Path);
答
如果C字符串是空终止,那么Erik演示的方法将工作正常。
然而,如果C-串不是空终止,则一个必须执行下列操作(假定人知道的有效数据的长度):
std::string sPath(Path, PathLength);
或:
// given std::string sPath
sPath.assign(Path, PathLength);
请缩进代码4个空格让它正确显示。 – Swiss 2011-04-05 18:23:13