【问题标题】:What is the difference between these two C++ statements to concatenate a string?这两个连接字符串的 C++ 语句有什么区别?
【发布时间】:2020-09-19 17:18:40
【问题描述】:

我是 C++ 新手,我想知道这两行之间有什么区别:

cout << "John " << "Doe" << endl;
cout << "John " + "Doe" << endl;

第一个有效,第二个无效。有什么想法吗?

【问题讨论】:

  • 第一个打印John Doe ...第二个,没有。您实际上是将两个数组添加在一起,这不会编译。 Try it and see
  • 您应该将cout &lt;&lt; "John "s + "Doe"s &lt;&lt; endl; 添加到您的问题中。这就是字符串文字的连接方式。 wandbox.org/permlink/WaW4Kx8LHXh5KdX6
  • 都不连接字符串。第一个打印两个不同的字符串,它们之间没有任何输出。第二个不编译。
  • 这并没有解决问题,但您不需要std::endl 所做的额外内容。 '\n' 结束一行。

标签: c++ string concatenation c-strings string-literals


【解决方案1】:

第一个将“John”插入标准输出,然后插入“Doe”。最终结果是您可以按顺序看到两个字符串。

第二次尝试将加法运算符与数组一起用作操作数。这是格式错误的,因为 C++ 中不存在这样的运算符。

【讨论】:

    【解决方案2】:

    第二个不起作用的原因是您不能使用+ 运算符连接两个字符串文字。另一方面,第一种方法利用chaining of insertion operators 打印多个表达式

    【讨论】:

      【解决方案3】:

      第一行

      cout << "John " << "Doe" << endl;
      

      不连接字符串。它打印“John”,然后打印“Doe”。但从来不存在字符串“John Doe”。

      第二行

      cout << "John " + "Doe" << endl;
      

      语法错误。 char 数组没有 operator+ 重载。

      要连接字符串文字,您可以使用

      #include <iostream>
      #include <string>
      using std::literals::string_literals::operator""s;
      
      int main() {
          std::cout << "John "s + "Doe"s << std::endl;
          // or
          auto name = "John "s + "Doe"s;
          std::cout << name << std::endl;
      }
      

      C++14 起。 operator""soperator+ 创建一个std::string

      【讨论】:

        【解决方案4】:

        不能像在许多其他语言中一样将像 "foo" 这样的字符串文字添加在一起(即 operator+)以进行连接。这在像 C++ 这样的低级语言中是合理的,因为(在一般情况下)这意味着必须进行分配。

        但是,如果您对此感到满意,那么您可以使用具有这些功能的 std::string 对象。以下作品:

        cout << string("John ") + string("Doe") << endl;
        

        【讨论】:

        • "John " "Doe" 连接两个字符串文字。相当于"John Doe"
        • @PeteBecker 我写了“ added”,意思是执行加法(operator+)。写"John " "Doe" 不是补充。我将答案编辑为(甚至)更明确。
        【解决方案5】:

        第一种方法是在 C++ 中连接两个字符串的正确方法。而第二种方法,默认情况下是在 Python 中连接两个字符串的方法。

        您不能使用第二个,因为它表示添加两个字符串而不是串联(如 c++ 编译器所见),因此它不受支持。

        【讨论】:

          【解决方案6】:

          第二个表达式被计算为

          cout << ("John " + "Doe") << endl;
          

          由于语言语法(通常被认为是运算符优先级)。

          但是"John "const char[6] 类型,而"Doe"const char[4] 类型。由于+,两者都衰减const char* 指针。并且无法添加指针,因此编译器会发出诊断。


          在第一种情况下,std::ostreamconst char* 的重载 &lt;&lt; 使用了两次,结果是您所期望的。

          【讨论】:

            【解决方案7】:

            对于初学者来说,没有字符串的串联。

            在第一个语句中

            cout << "John " << "Doe" << endl;
            

            依次输出两个字符串。

            在第二个语句中

            cout << "John " + "Doe" << endl;
            

            尝试使用const char * 类型的两个指针执行二进制操作+,使用的具有数组类型的字符串文字被隐式转换到该指针。但是运算符+ 没有为指针定义。

            您可以使用运算符- 来表示类似的指针

            std::cout << "John " - "Doe" << std::endl;
            

            此语句在语法上正确,但在语义上具有未定义的行为,因为指针不指向同一数组的元素。

            内置数组类型没有二元运算符 +。

            此运算符为用户定义的类模板std::basic_string 重载。

            你可以这样写

            #include <iostream>
            #include <string>
            
            int main() 
            {
                std::cout << std::string( "John " ) + "Doe" << std::endl;
            
                return 0;
            }
            

            或者您可以使用用户定义的字符串文字,例如

            #include <iostream>
            #include <string>
            
            int main() 
            {
                using namespace std::string_literals;
                std::cout << "John "s + "Doe" << std::endl;
            
                return 0;
            }
            

            或者在 C++ 17 标准中定义了新的类模板 std::string_view,您也可以使用它来连接字符串。

            【讨论】:

              【解决方案8】:

              要连接两个文字字符串,只需一个接一个地写入,除空格外没有其他中间字符:

              const char *lit = "John " "Doe";
              std::cout << lit << '\n';
              

              std::cout << "John "
              
                  "Doe";
              

              等等

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2016-04-16
                • 2022-01-02
                • 1970-01-01
                • 1970-01-01
                • 2023-03-15
                • 2012-10-10
                相关资源
                最近更新 更多