C++ 为什么ostringstream没有清空?
我试图通过ostringstream将int转换为字符串,但每次我将数据放入流中时,它都会留在流中。我已尝试使用C++ 为什么ostringstream没有清空?,c++,flush,ostringstream,C++,Flush,Ostringstream,我试图通过ostringstream将int转换为字符串,但每次我将数据放入流中时,它都会留在流中。我已尝试使用.flush()和清除ostringstream.buffer,因此: strs.str(""); 清除ostringstream.buffer,从而: strs.str(""); std::ostringstream是字符串的编写器接口。使用std::ostringstream是字符串的编写器接口。使用重置std::ostringstream,您只需分配一个新的缓冲区: std:
.flush()
和清除ostringstream.buffer,因此:
strs.str("");
清除ostringstream.buffer,从而:
strs.str("");
std::ostringstream
是字符串的编写器接口。使用std::ostringstream是字符串的编写器接口。使用重置std::ostringstream
,您只需分配一个新的缓冲区:
std::ostringstream oss;
// ...
oss.str(std::string());
但是,大多数情况下,这是不必要的,因为您可以在尽可能窄的范围内定义/初始化stringstream,以自动获得该值。在您的情况下,您可能希望在外部for循环中定义它。要重置std::ostringstream
,只需分配一个新的缓冲区:
std::ostringstream oss;
// ...
oss.str(std::string());
但是,大多数情况下,这是不必要的,因为您可以在尽可能窄的范围内定义/初始化stringstream,以自动获得该值。在您的情况下,您可能希望在外部for循环中定义它。将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;
}
intmain()
{
长整数最大值=0;
{
ostringstream strs;
对于(int i=10;i将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;
}
intmain()
{
长整数最大值=0;
{
ostringstream strs;
对于(int i=10;i到目前为止,最简单的处理方法是在每次希望对象为空时创建一个新的stringstream
对象。在大多数情况下(包括您的),只需让现有对象超出范围,并在重新进入正确范围时创建一个新对象,就可以轻松处理此问题
就我个人而言,我会将所有代码从int
转换为std::string
转换为一个具有本地stringstream对象的函数,这样每次调用函数时都会创建一个“干净”的stringstream,离开函数时会将其销毁
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;
}
std::字符串到_字符串(长整型){
std::stringstream buffer;//每次调用此函数时都是新建/空的
buffer到目前为止,处理这个问题最简单的方法是在每次希望对象为空时创建一个新的stringstream
对象。在大多数情况下(包括您的),只需让现有对象超出范围,并在重新进入正确范围时创建一个新对象,就可以轻松处理这个问题
就我个人而言,我会将所有代码从int
转换为std::string
转换为一个具有本地stringstream对象的函数,这样每次调用函数时都会创建一个“干净”的stringstream,离开函数时会将其销毁
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;
}
std::字符串到_字符串(长整型){
std::stringstream buffer;//每次调用此函数时都是新建/空的
缓冲区为什么它是空的?文件的内容不会因为你读取而消失,是吗?从流中读取只是获取流中的数据,它不会删除流中的数据。你甚至读过ostringstream的文档,特别是它的成员函数,特别是flush吗?@Plasmah yup,它说flush:flush out放置流缓冲区
。我认为这会解决问题。我很困惑,因为我使用它的方式not@Daniel:刷新缓冲区意味着确保数据到达其最终目的地,在您的情况下,这是内部字符串。为什么它是空的?文件的内容不会因为读取而消失,是吗?Reading从流中只获取流中的数据,它不会删除流中的数据。你读过ostringstream的文档吗,特别是它的成员函数,特别是flush吗?@Plasmah yup,它说flush:flush output stream buffer
。我认为这可以解决问题。我很困惑,因为我使用它的方式它的not@Daniel:嗯,刷新缓冲区意味着确保数据到达其最终目的地,在您的情况下是内部字符串。这只是重置标志,不会更改字符串缓冲区的内容。这只是重置标志,不会更改字符串缓冲区的内容。-1:这不起作用,str()
返回基础字符串的副本。-1:那不行,str()
返回基础字符串的副本。