2

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!

2 Answers 2

8
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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. I think if I had a large number of char*'s to combine (which I often do) then the stringstream solution becomes more efficient. I'm going to go with that one for now. And now that I think about it, with the stringstream I can also incorpate int's and anything else that is sendable to a cout, I would assume. That's definitely what I wanted.
Seth, not really related to the problem at hand, but if I had a stringstream and wanted to get a char* from it, can I get it directly from the stringstream somehow? Or do I get a std::string and then c_str() it?
@John yeah, you need to call str() to "materialize" the string, then c_str() to get the pointer to it. I would save the string that it returns from str() though if you can use it again, because it's not an inexpensive operation.
@Seth: You need to save the string from str or the memory pointed to by c_str will be deallocated.
@Seth: when you do s.str() you get a newly allocated string. If you don't capture it then it deallocates at the end the statement. In char* cptr = s.str().c_str();, the temporary only lasts until ;, so the memory c_str() points to is no longer valid by the time you can use cptr. If you're passing the result to another function, like strcpy or some other function that expects a null-terminated char*, then you're OK as long as it doesn't store the pointer.
|
2

Something like this?

std::string str = std::string(_szImportDirectory).append("\\").append(_szImportSourceFile);

PS: updated with correct code

8 Comments

BTW, looks like Java StringBuffer style :)
Which is a good thing. Truth be told, I really miss all the Java conveniences when working with strings in C++. (Do downvoted comments affect my reputation?)
String s = strImportDir + "\\" + strImportFile; s.replace("some Regular Expression", "b"); if (s.match("\$\{hi\}") { /* etc */ }; /* Actually I know I can do this all with std::string, but I'm still learning it all */
Oh and I forgot my favorite String[] a = s.split(",");
John, for regexes you can use std::tr1 extension or boost::regex.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.