【问题标题】:C++ how to add more strings into a methodC ++如何将更多字符串添加到方法中
【发布时间】:2012-09-21 00:51:15
【问题描述】:

自从我开始编程并决定学习 c++ 以来,我一直在使用 Java。 我用 Java 写的看起来像这样:

showMessage("Hello world" + randomNumber);

它显示文本 + 整数或浮点数或其他。但它不会在 c++ 中工作。 xCode 的错误消息:Invalid operands to binary expression ('const char *' and 'float')

干杯!

【问题讨论】:

    标签: c++ xcode char cocos2d-x


    【解决方案1】:

    你可以按照安东的说法sprintf,或者更多c++

    std::stringstream ss;
    ss << "Hello, world " << randomNumber;
    showmessage(ss.str());
    

    (sprintf 没有任何问题,特别是如果您使用snprintf 代替)。

    【讨论】:

      【解决方案2】:
          ostringstream os;
          os<<"HelloWorld"<<randomnumber;
          string s;
          s = os.str();
      

      string s 现在包含您想要作为字符串对象的字符串。

      【讨论】:

        【解决方案3】:

        您也可以使用boost::lexical_cast 将数字转换为字符串,这是大多数情况下最快的方法:

        showMessage("Hello world" + boost::lexical_cast<std::string>(randomNumber));
        

        showMessage 声明是

        void showMessage(cosnt std::string& message)
        

        【讨论】:

          【解决方案4】:

          考虑添加一个能够将多种类型转换为 std::string 的新函数:

          template<typename ty>
          string to_str(ty t)
          {
             stringstream ss; ss << t;
             return ss.str();
          }
          

          用法:

          "Hello World " + to_str(123)
          

          【讨论】:

          • @MichaelKrelin-hacker 它不会失败。连接char*string&lt;string&gt; 中已经超载
          • 我以为只有string在左边。也许我在旧的不完整实现中遇到了这个问题,不确定。
          • 即使通过。它不会失败,因为它将被分配给一个字符串string str = "hello World " + to_str(123);
          【解决方案5】:

          定义一个类S。然后写

          showMessage( S() << "Hello world" << randomNumber );
          

          我已经为 SO 编写了太多次 S 类,创建它是一个很好的练习,因此不提供源代码。

          请注意,您可以合理地将其命名为 StringWriter 或类似名称,然后在函数调用中使用 typedef 以获得更简洁的代码。

          【讨论】:

            【解决方案6】:

            我不确定 c 风格的答案是否合适,但我已经在 cocos2d-x 问题中回答了。

            Trying to set up a CCLabelTTF with an integer as part of it's string in Cocos2d-X C++

            【讨论】:

              【解决方案7】:

              使用 C++11:

              showMessage("Hello world" + std::to_string(randomNumber));
              

              【讨论】:

                【解决方案8】:

                您应该改为打印到 char* 中。

                你可以做类似的事情

                char* tempBuffer = new char[256];
                
                sprintf_s(tempBuffer, 256, "Hello world %d", randomNumber);
                
                showMessage(tempBuffer);
                

                【讨论】:

                • printf 功能是C 的一部分,在C++ 中不鼓励
                • 如您所见,他使用了安全的 sprintf
                • 就 C++ 而言,sprintf 没有什么“安全”的。它是整个printf 家族的一部分。
                • printffamily 并没有什么不安全的地方,不过,sprintf_s 不是 windows 功能吗?
                • 重复调用会导致内存泄漏
                【解决方案9】:

                在 C++ 中,连接字符串和原语的标准方法是使用 stringstream。它实现了与 Java 中的 StringBuilder 相同的功能(并且更多)(当然它的 API 不同)。但是,如果您对使用 cout 感到满意,那么您应该没问题。

                例如。

                #include <iostream>
                #include <sstream>
                #include <string>
                
                using namespace std;
                
                int main () {
                  stringstream ss;
                  ss << "Some string - " << 124; // build string    
                  string str = ss.str(); // extract string
                
                  cout << str << endl;
                  return 0;
                }
                

                字符串流快速参考http://www.cplusplus.com/reference/iostream/stringstream/stringstream/

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-01-13
                  • 2015-03-09
                  • 2023-03-17
                  • 2021-12-30
                  • 2022-12-04
                  • 2013-06-27
                  • 2011-11-27
                  相关资源
                  最近更新 更多