开发者

Formatting multiple char* strings to an std::string

开发者 https://www.devze.com 2023-03-29 00:34 出处:网络
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 wond

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

0

精彩评论

暂无评论...
验证码 换一张
取 消