保存旧流格式并恢复它 [英] Saving old stream format and restoring it
问题描述
如何保存流的格式选项?在下面的程序
中,请注意,当我为自定义类型定义运算符<<()时,它会永久地更改流的输出格式:
#include< iostream>
#include< iomanip>
struct Data {
double d;
};
std :: ostream&
operator<<(std :: ostream& ; o,const数据& d)
{
// std :: ostream old;
//old.copyfmt(o);
o<< 期间: << std :: fixed<< std :: setprecision(3)<< dd;
//o.copyfmt(old);
返回o;
}
int main()
{
数据d;
dd = 3.14159;
double dd;
dd = 3.14159;
std :: cout<< 之前: << dd<< ''\ n'';
std :: cout<< d<< ''\ n'';
std :: cout<< "之后: << dd<< ''\ n'';
返回0;
}
输出:
之前:3.14159
期间:3.142
之后:3.142
当我取消注释运算符<<()中的行时,我得到一个没有合适的
默认构造函数错误,当我尝试时
std :: ostream old(o);
我得到一个没有复制构造函数可用或复制构造函数被声明
''explicit''"错误。
那么,如何保存流的旧格式化选项,并在返回之前恢复它们的价值?
-
Marcus Kwok
How do you save the formatting options for a stream? In the program
below, notice that when I define my operator<<() for the custom type, it
permanently changes the output format of the stream:
#include <iostream>
#include <iomanip>
struct Data {
double d;
};
std::ostream&
operator<<(std::ostream& o, const Data& d)
{
//std::ostream old;
//old.copyfmt(o);
o << "during: " << std::fixed << std::setprecision(3) << d.d;
//o.copyfmt(old);
return o;
}
int main()
{
Data d;
d.d = 3.14159;
double dd;
dd = 3.14159;
std::cout << "before: " << dd << ''\n'';
std::cout << d << ''\n'';
std::cout << " after: " << dd << ''\n'';
return 0;
}
output:
before: 3.14159
during: 3.142
after: 3.142
When I uncomment the lines in operator<<(), I get a "no appropriate
default constructor available" error, and when I tried
std::ostream old(o);
I get a "no copy constructor available or copy constructor is declared
''explicit''" error.
So, how do I save the old formatting options for the stream, and restore
them before returning?
--
Marcus Kwok
推荐答案
Marcus Kwok写道:
Marcus Kwok wrote:
如何你保存流的格式选项?在下面的程序中,请注意当我为自定义类型定义运算符<<()时,它会永久更改流的输出格式:
#include < iostream>
#include< iomanip>
struct Data {
double d;
};
std :: ostream& ;
operator<<(std :: ostream& o,const Data& d)
// std :: ostream old;
//old.copyfmt(o );<<< 期间: << std :: fixed<< std :: setprecision(3)<< dd;
//o.copyfmt(old);
返回o;
}
int main()
{
数据d ;
dd = 3.14159;
双dd;
dd = 3.14159;
std :: cout<< 之前: << dd<< ''\ n'';
std :: cout<< d<< ''\ n'';
std :: cout<< "之后: << dd<< ''\ n'';
返回0;
}
输出:
之前:3.14159
期间:3.142
之后:3.142
当我取消注释运算符<<()中的行时,我得到一个没有合适的
默认构造函数。错误,当我尝试使用
std :: ostream old(o);
我得到一个没有复制构造函数可用或复制构造函数被声明
'显式''"错误。
那么,如何保存流的旧格式化选项,并在返回之前恢复它们?
How do you save the formatting options for a stream? In the program
below, notice that when I define my operator<<() for the custom type, it
permanently changes the output format of the stream:
#include <iostream>
#include <iomanip>
struct Data {
double d;
};
std::ostream&
operator<<(std::ostream& o, const Data& d)
{
//std::ostream old;
//old.copyfmt(o);
o << "during: " << std::fixed << std::setprecision(3) << d.d;
//o.copyfmt(old);
return o;
}
int main()
{
Data d;
d.d = 3.14159;
double dd;
dd = 3.14159;
std::cout << "before: " << dd << ''\n'';
std::cout << d << ''\n'';
std::cout << " after: " << dd << ''\n'';
return 0;
}
output:
before: 3.14159
during: 3.142
after: 3.142
When I uncomment the lines in operator<<(), I get a "no appropriate
default constructor available" error, and when I tried
std::ostream old(o);
I get a "no copy constructor available or copy constructor is declared
''explicit''" error.
So, how do I save the old formatting options for the stream, and restore
them before returning?
只是不要改变它们:
#include< iostream>
#include< iomanip>
#include < sstream>
struct Data {
double d;
};
std :: ostream&
operator<<(std :: ostream& o,const Data& d)
{
std :: stringstream str;
str<< 期间: << std :: fixed<< std :: setprecision(3)<< d.d;
o<< str.str();
返回o;
}
int main()
{
数据d;
dd = 3.14159;
双dd;
dd = 3.14159 ;
std :: cout<< 之前: << dd<< ''\ n'';
std :: cout<< d<< ''\ n'';
std :: cout<< "之后: << dd<< ''\ n'';
返回0;
}
最佳
Kai-Uwe Bux
Just don''t change them:
#include <iostream>
#include <iomanip>
#include <sstream>
struct Data {
double d;
};
std::ostream&
operator<<(std::ostream& o, const Data& d)
{
std::stringstream str;
str << "during: " << std::fixed << std::setprecision(3) << d.d;
o << str.str();
return o;
}
int main()
{
Data d;
d.d = 3.14159;
double dd;
dd = 3.14159;
std::cout << "before: " << dd << ''\n'';
std::cout << d << ''\n'';
std::cout << " after: " << dd << ''\n'';
return 0;
}
Best
Kai-Uwe Bux
Marcus Kwok写道:
Marcus Kwok wrote:
std :: ostream&
operator<<(std: :ostream& o,const数据& d)
// std :: ostream old;
std::ostream&
operator<<(std::ostream& o, const Data& d)
{
//std::ostream old;
使用''std :: ostream old(0);''而是:''std :: ostream'的构造函数'
需要一个流缓冲区,但这可以为null。
-
< mailto:di *********** @ yahoo.com> < http://www.dietmar-kuehl.de/>
< http://www.eai-systems.com> - 高效的人工智能
Use ''std::ostream old(0);'' instead: the constructor of ''std::ostream''
requires a stream buffer but this can be null.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Kai-Uwe Bux写道:
Kai-Uwe Bux wrote:
只是不要改变它们:
std :: ostream&
operator<<(std :: ostream& o,const Data& d)
{st / :: stringstream str;
str<< 期间: << std :: fixed<< std :: setprecision(3)<< d.d;
o<< str.str();
返回o;
}
Just don''t change them: std::ostream&
operator<<(std::ostream& o, const Data& d)
{
std::stringstream str;
str << "during: " << std::fixed << std::setprecision(3) << d.d;
o << str.str();
return o;
}
这不起作用:例如,当使用这样的代码调用时
它以意想不到的方式表现:
std :: cout<< std :: showpos<< d;
-
< mailto:di *********** @ yahoo.com> < http://www.dietmar-kuehl.de/>
< http://www.eai-systems.com> - 高效的人工智能
This doesn''t work: for example, when called using code like this
it behaves in unexpected ways:
std::cout << std::showpos << d;
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
这篇关于保存旧流格式并恢复它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!