0

我正在尝试通过 ostringstream 将 int 转换为字符串,但每次我将数据放入流中时,它都会保留在流中。我已经尝试使用两者.flush()<<endl但流永远不会清空。这个问题表明我并没有真正得到流(我没有,仍在努力)而且我正在做的是胡说八道和不必要的。

int main()
{
  long int max = 0;
  ostringstream strs;
  for(int i=10;i<100; i++){
    for(int j = i; j < 100; j++){ 
        long int product = i*j;
        strs.flush();
        strs <<product;

        string str = strs.str(); 
        cout<<str;
        int size = str.length();
    }
  }
  cout<<max;
  return 0;
}

目前我得到的输出

100/100
110/100110
120/100110120
130/100110120130
etc...

代替

100/100
110/110
120/120
130/130
etc... 
4

5 回答 5

3

std::ostringstream是字符串的编写器接口。使用<<追加到字符串,并且flush是多余的,因为字符串没有缓冲区。

要更改流写入的字符串(例如更改为空字符串),请使用其str()成员函数的设置版本:

strs << product;
string str = strs.str();
strs.str(""); // reset string written into
于 2013-06-27T14:07:13.887 回答
1

By far the easiest way to handle this is to create a new stringstream object every time you want it to be empty. In most cases (including yours) this is easily handled by simply letting the existing object go out of scope, and having a new one that's created when you re-enter the correct scope.

Personally, I'd do the job by moving all the code to convert from int to std::string into a function that has a local stringstream object, so a "clean" stringstream is created every time you call the function, and destroyed when you leave it.

std::string to_string(long int in) { 
    std::stringstream buffer; // New/empty every time this function is called
    buffer << in;
    return buffer.str();
}                             // here buffer goes out of scope and is destroyed.

int main()
{
  long int max = 0;
  for(int i=10;i<100; i++){
    for(int j = i; j < 100; j++) {
        long int product = static_cast<long>(i)*j;
        std::string str = to_string(product);

        // presumably:
        // if (product > max) max = product;

        std::cout << str;

        // you never seem to use this:
        //int size = str.length();
    }
  }
  cout<<max;
  return 0;
}

A couple of notes: if your compiler is reasonably recent, it may already have an std::to_string in its standard library, so you can just use that instead of writing your own.

Also note the cast to long before multiplying i and j. Given the values you're using right now, this isn't strictly necessary, but neither is the use of long for product. Assuming you might ever use values where product could be larger than will fit in an int, the cast becomes necessary. Without it, you're doing multiplication on two ints, which naturally produces an int result. Then you're taking that result (which has already overflowed if it's too large to fit in an int) and converting it to long. By converting one of the operands to long before the multiplication, you force the multiplication to be carried out on longs as well, preventing the overflow (at least assuming long is large enough to hold the result).

于 2013-06-27T14:28:13.447 回答
0

将 ostringstream 放入范围,这样每次输入时都会重新创建它。在您的情况下,它将是:

int main()
{
  long int max = 0;
  {
  ostringstream strs;
  for(int i=10;i<100; i++){
    for(int j = i; j < 100; j++){ 
        long int product = i*j;
        strs.flush();
        strs <<product;

        string str = strs.str(); 
        cout<<str;
        int size = str.length();
    }
  }
}

  cout<<max;
  return 0;
}
于 2013-06-27T14:08:47.080 回答
0

要重置std::ostringstream,您只需分配一个新缓冲区:

std::ostringstream oss;
// ...
oss.str(std::string());

但是,大多数时候这不是必需的,因为您可以在最窄的范围内定义/初始化您的字符串流以自动获取它。在您的情况下,您可能希望在外部 for 循环中定义它。

于 2013-06-27T14:07:22.213 回答
0

因此清除 ostringstream.buffer:

strs.str("");
于 2013-06-27T14:04:13.200 回答