Formatting multiple char* strings to an std::string
I have two char* strings and a char* literal that I need to combine into a single std::string. Below is what I am doing. It works, but I don't like the way it looks (3 lines to accomplish it). I am wondering if there is a better way to do it...
std::string strSource = _szImportDirectory;
strSource += "\\";开发者_开发技巧
strSource += _szImportSourceFile;
Thanks for you help!
std::string strSource = std::string(_szImportDirectory) + "\\" + _szImportSourceFile;
Is one obvious way.
Another way is to use std::stringstream
:
std::stringstream s;
s << _szImportDirectory << '\\' + _szImportSourceFile;
std::string strSource = s.str()
That's the most flexible and maintainable way to do it but it still requires three lines.
Something like this?
std::string str = std::string(_szImportDirectory).append("\\").append(_szImportSourceFile);
PS: updated with correct code
精彩评论