【问题标题】:How to add several string together such as "123"+"456"?如何将多个字符串添加在一起,例如“123”+“456”?
【发布时间】:2021-11-24 21:58:29
【问题描述】:

怎么实现这样的操作,Visual Studio老是说错了。
错误代码是C2110E2140
有人可以帮忙吗?

std::string a = "2323" + "22323" + "232332";

【问题讨论】:

  • 试试std::string("2323") + "22323"
  • std::string a = "2323" "22323" "232332";。仅由空格分隔的字符串文字被连接起来。
  • string a="2323" "22323" "232332"; 应该可以工作
  • 为了详细说明“问题”,+ 运算符仅针对std::string 定义,但是"2323"(没有任何前缀)不是std::string,它是@ 987654331@。因此,您需要将第一个参数显式声明为 std::string 以使 + (const char *) 可用。
  • @RefugnicEternium — 没有任何前缀的”2323” 不是const char*。它是一个由 5 个const char 组成的数组。

标签: c++ algorithm string-concatenation stdstring string-literals


【解决方案1】:

除了提供的其他答案外,您还可以使用std::stringstream 来构建std::string

#include <sstream>

int main( )
{
    std::stringstream ss{ };
    ss << "2323" << "22323" << "232332";

   // Maybe you also want to conditionally append something else.
   if ( some_condition )
   {
       // Note that I'm inserting an integer here.
       ss << 42;
   }

   // When you're ready you can call the str( ) member function
   // to attain a copy of the underlying string.
   std::string s{ ss.str( ) };
}

【讨论】:

    【解决方案2】:

    在此声明中

    std::string a="2323" + "22323" + "232332";
    

    您正在使用带有字符串文字的表达式和未为字符数组定义的运算符 + 以及相应的字符串文字被隐式转换到的指针。

    你可以这样写

    std::string a = "2323"s + "22323" + "232332";
    

    使用用户定义的字符串文字"2323"s 或者你可以写

    std::string a = std::string( "2323" ) + "22323" + "232332";
    

    另一种方式是声明对象alike

    std::string a;
    

    然后写

    a += "2323";
    a += "22323";
    a += "232332";
    

    这是一个演示程序。

    #include <iostream>
    #include <string>
    
    int main() 
    {
        {
            using namespace std::literals;
            
            std::string a = "2323"s + "22323" + "232332";
            std::cout << a << '\n';
        }
        
        {
            std::string a = std::string( "2323" ) + "22323" + "232332";
            std::cout << a << '\n';
        }
        
        {
            std::string a;
            
            a += "2323";
            a += "22323";
            a += "232332";
            
            std::cout << a << '\n';
        }
        
        return 0;
    }
    

    程序输出是

    232322323232332
    232322323232332
    232322323232332
    

    【讨论】:

      【解决方案3】:

      const char[X] 没有预定义的operator+(一个 char 的 const 数组,其中包含 X 的字符数;例如,const char[5] 是您的代码中的文字 "2323" 的类型) - 也不是对于const char *,它们可以自动转换为。这就是 C++ 编译器不允许你这样做的原因。 但是,有一个operator+ for std::string。由于该运算符依次返回一个字符串,因此您可以链式调用,即在其结果上再次调用 + 运算符。 所以你可以这样写:

      std::string a = std::string("2323") + "22323" + "232332";
      

      如果您已经在使用 C++14 或更高版本,您可以通过 s 后缀使用字符串类型的文字,那么另一种写法是

      using namespace std::string_literals;
      std::string a = "2323"s + "22323" + "232332";
      

      如果只是多个const char[X]要连接的文字,也可以一个接一个地写,像这样:

      std::string a("2323" "22323" "232332");
      // or:
      std::string a = "2323" "22323" "232332";
      

      编译器会自动为你连接它们。

      【讨论】:

      • 它们是 char 数组,而不是 char 指针。您将无法像这样连接指针。
      • 错过了细微差别,谢谢提示,现在应该是正确的吗?
      • 感谢您的修复。现在看起来很好。
      【解决方案4】:

      表达式"2323" 不是std::string,而是const char[5]

      从 C++14 开始,您可以拥有std::string 类型的文字:

      using namespace std::string_literals;
      
      std::string a = "2323"s + "22323"s + "232332"s;
      

      【讨论】:

      • "2323"s + "22323" + "232332" 就足够了。你不需要三个std::strings。
      猜你喜欢
      • 1970-01-01
      • 2021-01-06
      • 2023-04-03
      • 1970-01-01
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 2011-01-19
      • 2016-08-17
      相关资源
      最近更新 更多