【问题标题】:C++: How to concatenate a variable between strings literals?C ++:如何在字符串文字之间连接变量?
【发布时间】:2013-05-15 22:29:57
【问题描述】:

我需要一种在 C++ 中获得这种 PHP 行为的方法:

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

有什么接近的,还是我必须做很多strcat

【问题讨论】:

  • strcat 是 C(++)。如果您使用的是 C++,您可以使用 std::string 并继续执行 str = "something" + str + "something";
  • 请注意上面的@Tolga 注释,因为如果添加的至少一个参数是std::string,它将起作用,但无法连接字符串文字。遗憾的是(或者幸运的是,取决于你问谁),C++ 中的字符串文字不是std::string,而是常量字符数组。

标签: c++ string concatenation


【解决方案1】:

std::string 很简单:

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

只需确保前两个操作数之一是 std::string,而不是 const char * 或其他。


如下所述,此结果在CreateProcess 中用作char * (LPSTR) 参数。如果参数是const char *,则传入c_str() 是完全可以接受的。但是,它不是,这意味着您应该假设它修改了字符串。 MSDN 是这样说的:

这个函数的Unicode版本,CreateProcessW,可以修改这个字符串的内容。

既然这是char *,它显然是在使用CreateProcessA,所以我会说const_cast<char *>应该工作,但最好是安全的。

您有两个主要选项,一个用于 C++11 及更高版本,一个用于 C++11 之前的版本。

C++11

std::string 的内部缓冲区现在保证是连续的。它也保证是空终止的。这意味着您可以将指针传递给第一个元素:

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

确保该函数仅覆盖内部数组中 [0, size()) 内的索引。覆盖有保证的空终止符不好。

C++03

std::string 不保证是连续的或空终止的。我发现最好做一个临时的std::vector,它保证了连续的部分,并将一个指针传递给它的缓冲区:

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

还要注意 MSDN:

系统会在命令行字符串中添加一个终止空字符,以将文件名与参数分开。这将原始字符串分成两个字符串进行内部处理。

这似乎表明这里的空终止符不是必需的,但没有大小参数,所以我不完全确定。

【讨论】:

  • 这真的有效吗,你不需要std::string("this is a") 来确保它是一个字符串吗?
  • @MatsPetersson,Yes, it sure does.operator+(const char *, operator+(const std::string &amp;, const char *))。内部返回一个std::string
  • @MatsPetersson,该死的,我的第一个是对的。它从左到右,这意味着"this is a " + foo 的结果是result + " example." 的第一个参数。我只是无可救药地让自己感到困惑。
  • 如果我现在使用连接字符串时得到cannot convert 'std::string' to 'LPSTR' 怎么办?我尝试使用 c_str() 进行转换,但后来得到 invalid conversion from 'const char*' to 'LPSTR'
  • @esauvisky :这是一个稍微不同的问题。您将其传递给的实际函数是什么 - 它实际上是否更改了字符串?否则,使用强制转换(static_cast&lt;LPSTR&gt;(somestring.c_str()) 应该可以工作 - 但如果 API 函数正在修改字符串,则不要使用它)。
【解决方案2】:

是的,你可以使用std::string

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

【讨论】:

  • 谁发现了这个,确保你也这样做#include &lt;string&gt;,否则你会得到一些错误
【解决方案3】:

在 C++ 中,你可以使用std::string:

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

您需要std::string(...) 将第一个字符串变成std::string,否则它就是const char *,它没有operator+ 来将它与字符串连接起来。

可能至少有 5 种其他可能的方法可以做到这一点,就像几乎总是在 C++ 中一样。

[又是我打字太慢了]

【讨论】:

    【解决方案4】:

    C++ 提供了字符串类。

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

    【讨论】:

    • 字符串的构造函数中可以使用数字吗?
    • @gumuruh 你什么意思?有些字符串构造函数采用整数值,但它们都不会将“数字”转换为字符串。我感觉你在寻找类似std::to_string 的东西
    • 啊,是的......你是对的......如果它们位于(连接)字符串变量的中间,我们需要转换一个数字 std::to_string() 。谢谢@paddy。
    【解决方案5】:

    您可以为此使用 std::string。所以试试这个:

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

    【讨论】:

      【解决方案6】:

      如果您将 C++ 与标准 C++ 库一起使用,则可以使用 std::stringstream 来完成此操作。代码看起来像这样:

      #include <sstream>
      
      std::string const foo("C++");
      std::stringstream bar;
      
      bar << "this is a " << foo << " example";
      
      std::string const result(bar.str());
      

      如果由于某种原因您不能使用 C++ 标准库,那么很遗憾您会被 strcat 之类的东西困住。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-15
        • 1970-01-01
        • 1970-01-01
        • 2020-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-10
        相关资源
        最近更新 更多