【问题标题】:c++ stringstream not working properly when changing integer to string将整数更改为字符串时,c ++ stringstream无法正常工作
【发布时间】:2014-10-31 22:24:32
【问题描述】:

我正在尝试创建一个为我创建文件夹并使用数字组合命名它们的函数。这是我为函数编写的代码:

void folderMaker(int xCount, int yCount)
{
  stringstream ssObject;
  string xString;
  string yString;

  for (int x = 0; x<xCount; x++)
  {
    for (int y = 0; y<yCount; y++)
    {
      ssObject << x;
      xString = ssObject.str();


      ssObject << y;
      yString = ssObject.str();

      string nameAndLocation = "C:\\User\\DestinationFolder\\" + xString + "and" + yString;
      CreateDirectory (nameAndLocation.c_str(), NULL);

    }
  }
}

我想让两个“for”循环为 x 和 y 变量赋予特定的值。从那里我想使用名称中包含的这两个变量创建的新文件夹的名称。出于这个原因,我使用了我创建的 ssObject。结果是我确实创建了我想要的文件夹,但是名称不是我想要的名称。例如,如果 xCount = 3 和 yCount = 1,我会得到以下文件夹名称:

第一个文件夹的名称:x0andy00

第二个文件夹的名称:x001andy0010

第三个文件夹名称:x00102andy001020

为什么我得到这些名称(而不是 xCount = 3 和 yCount = 1 的情况下的 x0andy0、x1andy0 和 x2andy0)?以及如何获得我想要的正确结果?

【问题讨论】:

  • 设置两次 xString 而从不设置 yString 正常吗?
  • ssObject 永远不会清除 ..您的代码也不使用ystring
  • @DOOM 我对重用 xString 的态度不好,这是我在 stackoverflow 中重新输入代码时的错误。如何清除 ssObject DOOM?
  • 但是为什么要创建 3 个中间变量来格式化 fodler 名称,而您只能使用 stringstream 进行整个格式化?
  • @Christophe 我尝试使用字符串流来执行此操作,但是如果不先将 x 和 y 整数变量更改为字符串,则代码不允许我在 ssObject 中创建字符串。这就是为什么我必须声明 xString 和 yString。我这样做是否正确?

标签: c++ string directory stringstream


【解决方案1】:

这应该可以解决问题!

void folderMaker(int xCount, int yCount)
{
    std::stringstream ssObject;
    std::string xString;
    std::string yString;

    for (int x = 0; x<xCount; x++)
    {
        for (int y = 0; y<yCount; y++)
        {
            ssObject << "x" << x;
            xString = ssObject.str();
            ssObject.str("");

            ssObject << "y" << y;
            yString = ssObject.str();
            ssObject.str("");

            std::string nameAndLocation =   "C:\\User\\DestinationFolder\\" 
                                          + xString
                                          + "and" + yString;
            CreateDirectory (nameAndLocation.c_str(), NULL);
            std::cout << nameAndLocation << std::endl;
        }
    }
}

【讨论】:

  • 是的!我从 Piotr S. 获得了与 ssObject.str("") 相同的解决方案,我希望我能以某种方式将您的两个答案都作为最佳答案
【解决方案2】:

std::ostringstream::str()

(1) string str() const;

(2) void str (const string& s); 

第一种形式 (1) 返回一个字符串对象,其中包含流的当前内容的副本。

第二种形式 (2) 将 s 设置为流的内容,丢弃任何以前的内容

要清理缓冲区,也可以使用第二个重载:

xString = ssObject.str();
ssObject.str("");
//           ^^

【讨论】:

  • 完成了这项工作,ssObject.str("") 正是我需要的 xD!我希望有一天能像 Piotr S 一样出色。
【解决方案3】:

为什么不在 ssObject 中从头开始构建名称?

  for (int x = 0; x<xCount; x++)
    for (int y = 0; y<yCount; y++) {
       stringstream ssObject;
       ssObject << "C:\\User\\DestinationFolder\\" << x << "and" << y;
       CreateDirectory (ssObject.str().c_str(), NULL);
    }

【讨论】:

  • 我试过这样做,错误似乎是我无法将 x 和 y 变量插入到最后第三行代码中,因为它们是整数而不是字符串。
  • @CodeBlocks:那是不可能的。您可以将整数传递给流。肯定有其他问题。
  • 缺少#include?你用 + 代替
  • @ChristianHackl 你是对的,我用“+”号代替了“
  • @Christophe 谢谢 christophe,你的方法确实是最好的
【解决方案4】:

因为您不断将数据推送到同一流

最简单的事情是为每次转换使用一个新的流,最好是在一个函数中:

std::string ToString(int number)
{
    std::ostringstream os; // new stream created
    os << number;
    return os.str();
}

或者,如果您有 C++11 编译器,只需使用 std::to_string

【讨论】:

  • 我明白了。是否有任何代码可以清除字符串流对象?我无法真正更改代码,因为其他一些代码依赖于它,并且如果有一行代码可以清理 stringstream 对象以便可以重用,那对我来说是理想的
  • @CodeBlocks:见 Piotr S. 的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-04
  • 2017-01-22
  • 1970-01-01
  • 1970-01-01
  • 2014-05-31
  • 1970-01-01
  • 2017-02-18
相关资源
最近更新 更多