9

I need a way to get this PHP behaviour in C++:

$foo = "PHP";
$bar = "this is a " . $foo . " example.";

Is there something close to that, or do I have to do lots of strcat?

2
  • strcat is C(++). If you are using C++, you can use std::string and go ahead and do str = "something" + str + "something"; Commented May 15, 2013 at 22:31
  • 2
    Beware with @Tolga comment above, as it will work if at least one of the arguments to the addition is a std::string, but will fail to concatenate string literals. Sadly (or luckily, depends on who you ask), string literals in C++ are not std::string, but arrays of constant characters. Commented May 15, 2013 at 23:38

6 Answers 6

22

Easy enough with std::string:

std::string foo = "C++";
auto bar = "this is a " + foo + " example.";

Just make sure one of the first two operands is a std::string, not both const char * or something.


As noted below, this result is being used in CreateProcess as a char * (LPSTR) argument. If the argument was const char *, c_str() would be perfectly acceptable to pass in. However, it is not, which means you should assume it modifies the string. MSDN says this:

The Unicode version of this function, CreateProcessW, can modify the contents of this string.

Since this is char *, it's evidently using CreateProcessA, so I'd say a const_cast<char *> should work, but it's better to be safe.

You have two main options, one for C++11 and later, and one for pre-C++11.

C++11

std::string's internal buffer is now guaranteed to be contiguous. It's also guaranteed to be null-terminated. That means you can pass a pointer to the first element:

CreateProcess(..., &str[0], ...);

Make sure the function only overwrites indices within [0, size()) in the internal array. Overwriting the guaranteed null-terminator is not good.

C++03

std::string is not guaranteed to be contiguous or null-terminated. I find it best to make a temporary std::vector, which guarantees the contiguous part, and pass a pointer to its buffer:

std::vector<char> strTemp(str.begin(), str.end());
strTemp.push_back('\0');
CreateProcess(..., &strTemp[0], ...);

Also note MSDN again:

The system adds a terminating null character to the command-line string to separate the file name from the arguments. This divides the original string into two strings for internal processing.

That seems to suggest that the null-terminator here isn't necessary, but there's no size parameter, so I'm not completely sure.

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

30 Comments

Does that atually work, don't you need std::string("this is a") to ensure it's a string?
@MatsPetersson, Yes, it sure does. operator+(const char *, operator+(const std::string &, const char *)). The inner returns a std::string.
@MatsPetersson, Dang it, my first was right. It goes left to right, which means the result of "this is a " + foo is the first argument to result + " example.". I'm just hopelessly confusing myself.
What if now I get cannot convert 'std::string' to 'LPSTR' when I use the concatenated string? I've tried converting with c_str() but then I get invalid conversion from 'const char*' to 'LPSTR'.
@esauvisky : That is a slightly different matter. What is the actual function you are passing it to - does it actually change the string? Otherwise, using a cast (static_cast<LPSTR>(somestring.c_str()) should work - but don't use that if the string is being modified by the API function).
|
7

Yes, you can use std::string:

std::string foo = "PHP";
std::string bar = std::string("This is a") + foo + std::string(" example.")

1 Comment

whoever finds this, make sure you also do #include <string> otherwise you'd get some errors
7

In C++, you can use std::string:

std::string foo = "C++"
std::string bar = std::string("this is a") + foo + " example.";

You need the std::string(...) to make the first string into a std::string, since otherwise it's a const char *, which doesn't have operator+ to join it with string.

There are probably at least 5 other possible ways to do this, like almost always in C++.

[Again being too slow in my typing]

Comments

4

If you are using C++ with the standard C++ library, you can use a std::stringstream to accomplish that. The code would look something like this:

#include <sstream>

std::string const foo("C++");
std::stringstream bar;

bar << "this is a " << foo << " example";

std::string const result(bar.str());

If for some reason you cannot use the C++ standard library you are unfortunately stuck with the likes of strcat.

Comments

3

C++ provides the string class.

string foo = "PHP";
string bar = string("this is a ") + foo + string(" example.");

3 Comments

a number can be used into that constructor of string?
@gumuruh What do you mean? There are string constructors that take integral values, but none of them will convert a "number" to a string. I get the feeling you're looking for something like std::to_string
ah, yeah... you're right... we need to convert a number std::to_string() if they're located (concatenated) in the middle of string variables. thanks @paddy.
3

You can use std::string for this. So try this:

#include <string>

int main() {
    std::string foo = "C++";
    std::string bar = "this is a " + foo + " example.";
    return 0;
}

Comments

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.