iostream

ios and ios_base class for stream formatting

最后都变了- 提交于 2019-12-10 04:16:59
问题 I found there is two ways to setf()/unsetf() for the iostream, that is (1) ios and (2) ios_base. #include <iostream> using namespace std; int main() { cout.width(5); cout << 123 << endl; cout.setf(ios::adjustfield); // (1) using ios:: cout << 123 << endl; cout.width(5); cout << 456 << endl; cout.setf(ios_base::adjustfield); // (2) using ios_base:: cout << 456 << endl; return 0; } What's the difference of them when I would like to change the format of the ostream; Which do you use normally in

Lazy evaluation with ostream C++ operators

泄露秘密 提交于 2019-12-10 03:09:19
问题 I am looking for a portable way to implement lazy evaluation in C++ for logging class. Let's say that I have a simple logging function like void syslog(int priority, const char *format, ...); then in syslog() function we can do: if (priority < current_priority) return; so we never actually call the formatting function (sprintf). On the other hand, if we use logging stream like log << LOG_NOTICE << "test " << 123; all the formating is always executed, which may take a lot of time. Is there any

how do I override a std::filebuf?

会有一股神秘感。 提交于 2019-12-09 23:08:00
问题 I have a Visual Studio 2008 C++ 03 application using STLPort 5.2.1 where I would like to use a custom std::filebuf implementation. For example: class MyFileBuf : public std::filebuf { protected: virtual int_type sync() { // breakpoint here never fires return std::filebuf::sync(); }; virtual std::streamsize xsputn( const char_type* p, std::streamsize n ) { // breakpoint here never fires return std::filebuf::xsputn( p, n ); }; virtual int_type overflow( int_type c = traits_type::eof() ) { //

How to write iostream-like interface to logging library?

僤鯓⒐⒋嵵緔 提交于 2019-12-09 23:00:06
问题 I would like to write a convinient interface to my very simple logging library. Take two following pieces of code. The first one is what I do now, the second one is my idea for an intuitive interface: std::ostringstream stream; stream<<"Some text "<<and_variables<<" formated using standard string stream" logger.log(stream.str()); //then passed to the logger And logger.convinient_log()<<"Same text "<<with_variables<<" but passed directly"; My thought-design process behind that idea is to

How to implement custom sticky manipulator that automatically adds separators?

亡梦爱人 提交于 2019-12-09 22:57:41
问题 The print function in Python automatically separates its arguments with a customisable separator. Is there any way to emulate this behavior in C++ by using stream manipulators? That is, the following C++ code: std::cout << custom::sep(", ") << 1 << "two" << 3 << std::endl; Should work similar to the following Python code: print(1, "two", 3, sep=", ") The desired output would be: 1, two, 3 How would I go about implementing custom::sep ? It seems a bit more tricky than your standard custom

Is there a good idiom to deal with alternative output streams?

会有一股神秘感。 提交于 2019-12-09 20:24:50
问题 I want to write a simple program that depending on the options passed it the executable will print the output to the screen or to a file. The program is simple. #include<iostream> int main(int argc, char* argv[]){ ... process options... std::ostream& out = ... // maybe std::cout, maybe a *new* std::ofstream; out << "content\n"; } Is there a good idiom to make out refer alternatively to std::cout or a file stream at runtime? I tried with pointers, but it is horrible. I couldn't avoid using

01_C++对C的扩展

淺唱寂寞╮ 提交于 2019-12-09 19:58:25
一:简单的C++程序 1.求圆的周长和面积   数据描述:     半径,周长,面积均用实型数表示   数据处理:     输入半径 r;     计算周长 = 2*π*r ;     计算面积 = π* r2 ;   输出半径,周长,面积; 方法 1 :用结构化方法编程,求圆的周长和面积 // count the girth and area of circle #include<iostream> using namespace std; void main () {   double r, girth, area ;   const double PI = 3.1415 ;   cout << "Please input radius:\n" ; //操作符重载   cin >> r ; //输入   girth = 2 * PI * r ;   area = PI * r * r ;   cout << "radius = " << r << endl ;    cout << "girth = " << girth << endl ;    cout << "area = " << area << endl ; } 方法 2 :用面向对象方法编程,求圆的周长和面积 #include<iostream> using namespace std; class Circle {

How should I correctly assign cout to a static ostream reference variable?

会有一股神秘感。 提交于 2019-12-09 18:27:39
问题 I'm defining a class like this: class StaticRuntimeContext { public: enum Verbosity { kHIGH, kMEDIUM, kLOW, kSILENT }; static void Construct(); static std::ostream& stdout1() {return stdout1_;} static std::ostream& stdout2() {return stdout2_;} static std::ostream& stdout3() {return stdout3_;} static std::ostream& stderr() {return stderr_;} protected: private: static std::ostream& stdout1_; static std::ostream& stdout2_; static std::ostream& stdout3_; static std::ostream& stderr_; }; I'm

how can I read exactly 128 bytes from an fstream into a string object? [duplicate]

对着背影说爱祢 提交于 2019-12-09 10:23:23
问题 This question already has answers here : Reading directly from an std::istream into an std::string (6 answers) Closed 3 years ago . How do I read exactly 128 bytes from an fstream into a string object? I wrote some code to read the first 128 bytes of a file and print it and then the last 128 bytes of the file and print that. The last part works, since you can easily iterate to EOF, but how do I get exactly 128 bytes from the front? The code below doesn't work since you can't add 128 to an

<< Operator Rewrite to cout int and double values

坚强是说给别人听的谎言 提交于 2019-12-09 03:48:51
问题 I need to rewrite the << operator so that it can cout values for hour (int) and temperature (double). I think I've included all necessary sections. Thanks in advance. struct Reading { int hour; double temperature; Reading(int h, double t): hour(h), temperature(t) { } bool operator<(const Reading &r) const; }; ======== ostream& operator<<(ostream& ost, const Reading &r) { // unsure what to enter here return ost; } ======== vector<Reading> get_temps() { // stub version cout << "Please enter