ostream

How to write 'n' copies of a character to ostream like in python

浪尽此生 提交于 2019-12-03 19:52:19
问题 In python, the following instruction: print 'a'*5 would output aaaaa . How would one write something similar in C++ in conjunction with std::ostream s in order to avoid a for construct? 回答1: The obvious way would be with fill_n : std::fill_n(std::ostream_iterator<char>(std::cout), 5, 'a'); Another possibility would be be to just construct a string: std:cout << std::string(5, 'a'); 回答2: Use some tricky way: os << setw(n) << setfill(c) << ""; Where n is number of char c to write 回答3: You can do

What is the difference between flush() and sync() in regard to fstream buffers?

无人久伴 提交于 2019-12-03 15:51:55
问题 I was reading the cplusplus.com tutorial on I/O. At the end, it says fstream buffers are synchronized with the file on disc Explicitly, with manipulators: When certain manipulators are used on streams, an explicit synchronization takes place. These manipulators are: flush and endl. and Explicitly, with member function sync(): Calling stream's member function sync(), which takes no parameters, causes an immediate synchronization. This function returns an int value equal to -1 if the stream has

Why does using std::endl with ostringstream affect output speed?

。_饼干妹妹 提交于 2019-12-03 13:04:59
I'm timing the difference between various ways to print text to standard output. I'm testing cout , printf , and ostringstream using both \n and std::endl . I expected std::endl to make a difference with cout (and it did), but I didn't expect it to slow down output with ostringstream . I thought using std::endl would just write a \n to the stream and it would still only get flushed once. What's going on here? Here's all my code: // cout.cpp #include <iostream> using namespace std; int main() { for (int i = 0; i < 10000000; i++) { cout << "Hello World!\n"; } return 0; } // printf.cpp #include

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

匿名 (未验证) 提交于 2019-12-03 07:50:05
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: 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 defining the construct function as: void

What is the difference between flush() and sync() in regard to fstream buffers?

女生的网名这么多〃 提交于 2019-12-03 05:31:49
I was reading the cplusplus.com tutorial on I/O . At the end, it says fstream buffers are synchronized with the file on disc Explicitly, with manipulators: When certain manipulators are used on streams, an explicit synchronization takes place. These manipulators are: flush and endl. and Explicitly, with member function sync(): Calling stream's member function sync(), which takes no parameters, causes an immediate synchronization. This function returns an int value equal to -1 if the stream has no associated buffer or in case of failure. Otherwise (if the stream buffer was successfully

Coverity finding: Not restoring ostream format (STREAM_FORMAT_STATE)

匿名 (未验证) 提交于 2019-12-03 01:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: We are catching a Coverity finding CID 156014: Not restoring ostream format (STREAM_FORMAT_STATE) (text below and image at the end). 938 const std::streamsize oldp = cout.precision(6); 5. format_changed: setf changes the format state of std::cout for category floatfield. 939 const std::ios::fmtflags oldf = cout.setf(std::ios::fixed, std::ios::floatfield); 940 cout << " Maurer Randomness Test returned value " << mv << endl; 6. format_changed: precision changes the format state of std::cout for category precision. 941 cout.precision(oldp); 7.

error C2512: 'std::basic_ostream<_Elem,_Traits>' : no appropriate default constructor available with Visual Studio only

守給你的承諾、 提交于 2019-12-02 12:59:48
问题 I'm asking this question because I'm a bit helpless: this error occurs ONLY with Visual Studio, GCC compiles it without errors or even warnings. Since this is some portable code I'm looking for a solution that works with both compilers (and in best case with no platform-dependent ifdefs ). error C2512: 'std::basic_ostream<_Elem,_Traits>' : no appropriate default constructor available happens while constructing an object of type MyObject that is defined as class MyObject : public Socket,

ostream cout and char *

戏子无情 提交于 2019-12-02 09:25:43
I have an array of chars like this one: char arr[3]="hi"; cout << arr;// this will print out hi So is the operator<< has an overloaded version that takes an ostream object and char *. so cout<<arr; first arr will decays to a chat * . and then operator<<() will print out what the char pointer is pointing to until it find a null-character ? The same question for cin>>arr; how does it work with operator>> that takes an array as the second operand. Your ostream and istream do have operator<< and operator>> overloaded to take a char* , and arrays decay into pointers to the first element. So, yes it

operator<< overloading ostream

隐身守侯 提交于 2019-12-01 17:48:45
In order to use cout as such : std::cout << myObject, why do I have to pass an ostream object? I thought that was an implicit parameter. ostream &operator<<(ostream &out, const myClass &o) { out << o.fname << " " << o.lname; return out; } Thanks You aren't adding another member function to ostream , since that would require redefining the class. You can't add it to myClass , since the ostream goes first. The only thing you can do is add an overload to an independent function, which is what you're doing in the example. Only if it is a member function of the class that would otherwise be the

Why does std::ofstream add extra #13 (newline) characters?

[亡魂溺海] 提交于 2019-12-01 12:07:49
I'm working with a basic std::ofstream object, created as follows: output_stream = std::ofstream(output_file.c_str()); This creates a file, where some information is put in. Let me show an example of such a message: (Watch window excerpt) full_Message "Error while processing message:\r\n\tForecast Request:" All this is ok, but after having launched following commands, there is a problem: output_stream << full_Message; output_stream.flush(); In order to see what is wrong, let's look at the hexadecimal dump of the file: (this is a hexadecimal display of the file, as seen in Notepad++. For