【问题标题】:Increment number in string C++ [duplicate]字符串C++中的递增数字[重复]
【发布时间】:2015-08-28 22:59:22
【问题描述】:

我正在尝试根据与文件名关联的数字来增加文件名。如果我将变量 postFix 设为 int 类型,它会给出错误(可以理解),因为我无法将 int 连接到字符串类型。

在这种情况下如何增加字符串变量?

for (int i = 0; i <= 10; i++) 
{
  std::string postFix = "1"; // int postFix = 1 gives an error
  std::cout << (std::string("Image" + postFix + std::string(".png")));
}

【问题讨论】:

  • 请澄清“递增”字符串变量。字符串递增后的值是多少?如果我有字符串“hello”,它增加后会发生什么?
  • 另一个建议,您可以通过在文件名中添加时间戳来使文件名唯一。
  • @ThomasMatthews 我想将 postFix 值从 1 增加到 2 到 3 到.. 20 作为循环结束。

标签: c++ string int increment


【解决方案1】:
int value = atoi(postFix.c_str());
value++;
postFix = std::to_string(value);

【讨论】:

【解决方案2】:

使用std::to_string

for (int i = 0; i <= 10; i++) 
{
    int postFix = 1;
    std::cout << (std::string("Image" + std::to_string(postFix) + std::string(".png")));
}

【讨论】:

    【解决方案3】:

    您可以使用std::to_string 从您的int 值创建std::string。

    for (int i = 0; i <= 10; i++) 
    {
        std::cout << "Image" + std::to_string(i+1) + ".png";
    }
    

    虽然在这种情况下你还不如只使用流operator&lt;&lt;

    for (int i = 0; i <= 10; i++) 
    {
        std::cout << "Image" << i+1 << ".png";
    }
    

    【讨论】:

      【解决方案4】:

      你没有;您使用一个整数变量,并对其进行字符串表示来构建文件名:std::to_string(n)

      【讨论】:

        【解决方案5】:

        根据您的意愿增加int 'Number',然后将其转换为字符串,最后附加文件名(或准确地添加到您想要的位置)。

        需要的标题:

        #include <iomanip>
        #include <locale>
        #include <sstream>
        #include <string> // this should be already included in <sstream>
        

        要将“数字”转换为“字符串”,请执行以下操作:

        int Number = 123;       // number to be converted to a string
        
        string Result;          // string which will contain the result
        
        ostringstream convert;   // stream used for the conversion
        
        convert << Number;      // insert the textual representation of 'Number' in the characters in the stream
        
        Result = convert.str(); // set 'Result' to the contents of the stream
        
                  // 'Result' now is equal to "123" 
        

        这个操作可以缩短为一行:

        int Number = 123;
        string String = static_cast<ostringstream*>( &(ostringstream() << Number) )->str();
        

        参考来自here。

        【讨论】:

        • 呃 - 您是否发布了错误问题的答案?
        • @kfsone,我正在编辑中
        【解决方案6】:

        简单写

        std::cout << std::string("Image" ) + std::to_string( i + 1 ) + ".png";
        

        甚至喜欢

        std::cout << "Image" + std::to_string( i + 1 ) + ".png";
        

        或者你可以先定义一个字符串

        std:;string = "图像" + std::to_string( i + 1 ) + ".png";

        然后输出

        std::cout

        【讨论】:

          【解决方案7】:

          看来你把它弄得比必要的复杂了。

          for (int i = 1; i <= 10; i++) {
               std::cout << "Image" << i << ".png";
             }
          

          或

          for (int i = 1; i  <= 10; i++) {
              std::string filename = "Image" + std::to_string(i) + ".png";
              // do something with filename
          }
          

          to_string(int) 出现在 C++11 中。

          【讨论】:

            猜你喜欢
            • 2017-11-03
            • 2017-02-22
            • 1970-01-01
            • 2013-11-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多