C++:如何在字符串文字之间连接变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16576075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++: How to concatenate a variable between strings literals?
提问by emi
I need a way to get this PHP behaviour in C++:
我需要一种在 C++ 中获得这种 PHP 行为的方法:
$foo = "PHP";
$bar = "this is a " . $foo . " example.";
Is there something close to that, or do I have to do lots of strcat
?
有什么接近的,还是我必须做很多strcat
?
回答by chris
Easy enough with std::string
:
很简单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.
只需确保前两个操作数之一是 a std::string
,而不是两者const char *
或某物。
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:
如下面所指出的,该结果被用于CreateProcess
作为char *
(LPSTR
)参数。如果参数是const char *
,c_str()
则完全可以接受传入。但是,事实并非如此,这意味着您应该假设它修改了字符串。MSDN 是这样说的:
The Unicode version of this function, CreateProcessW, can modify the contents of this string.
此函数的 Unicode 版本 CreateProcessW 可以修改此字符串的内容。
Since this is char *
, it's evidently using CreateProcessA
, so I'd say a const_cast<char *>
shouldwork, but it's better to be safe.
由于这是char *
,它显然正在使用CreateProcessA
,所以我会说 aconst_cast<char *>
应该可以工作,但最好是安全的。
You have two main options, one for C++11 and later, and one for pre-C++11.
您有两种主要选择,一种用于 C++11 及更高版本,一种用于 C++11 之前的版本。
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:
std::string
的内部缓冲区现在保证是连续的。它也保证以空值终止。这意味着您可以传递指向第一个元素的指针:
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.
确保该函数仅覆盖内部数组中 [0, size()) 内的索引。覆盖保证的空终止符并不好。
C++03
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::string
不保证是连续的或以 null 结尾的。我发现最好制作一个临时的std::vector
,以保证连续部分,并将指针传递给它的缓冲区:
std::vector<char> strTemp(str.begin(), str.end());
strTemp.push_back('std::string foo = "PHP";
std::string bar = std::string("This is a") + foo + std::string(" example.")
');
CreateProcess(..., &strTemp[0], ...);
Also note MSDN again:
还要再次注意 MSDN:
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.
这似乎表明这里的空终止符不是必需的,但没有大小参数,所以我不完全确定。
回答by taocp
Yes, you can use std::string
:
是的,您可以使用std::string
:
std::string foo = "C++"
std::string bar = std::string("this is a") + foo + " example.";
回答by Mats Petersson
In C++, you can use std::string
:
在 C++ 中,您可以使用std::string
:
#include <sstream>
std::string const foo("C++");
std::stringstream bar;
bar << "this is a " << foo << " example";
std::string const result(bar.str());
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.
您需要将std::string(...)
第一个字符串变成 a std::string
,否则它是 a const char *
,不必operator+
将其与字符串连接。
There are probably at least 5 other possible ways to do this, like almost always in C++.
可能至少有 5 种其他可能的方法来做到这一点,就像在 C++ 中几乎总是一样。
[Again being too slow in my typing]
[再次打字太慢]
回答by Timo Geusch
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:
如果您将 C++ 与标准 C++ 库一起使用,则可以使用 astd::stringstream
来实现。代码看起来像这样:
#include <string>
int main() {
std::string foo = "C++";
std::string bar = "this is a " + foo + " example.";
return 0;
}
If for some reason you cannot use the C++ standard library you are unfortunately stuck with the likes of strcat.
如果由于某种原因你不能使用 C++ 标准库,你很不幸地被 strcat 之类的东西困住了。
回答by ecdeveloper
You can use std::string for this. So try this:
您可以为此使用 std::string 。所以试试这个:
string foo = "PHP";
string bar = string("this is a ") + foo + string(" example.");
回答by paddy
C++ provides the string class.
C++ 提供了字符串类。
##代码##