ostream

Istream function to read with istream parameter

ⅰ亾dé卋堺 提交于 2020-07-07 06:58:10
问题 I'm trying to understand this code istream &read(istream &is, Sales_data &item) { double price = 0; is >> item.bookNo >> item.units_sold >> price; item.revenue = price * item.units_sold; return is; } ostream &print(ostream &os, const Sales_data &item) { os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price(); return os; } I don't understand what are these function , also I can't understand why we use istream for reading and ostream for printing instead of

C++ What is the role of std::ctype<char>::widen()?

蓝咒 提交于 2020-06-27 06:57:35
问题 According to the C++ standard (§30.7.5.2.4 of C++17 draft (N4659)), out << ch will not perform a widening operation on ch , if ch is a char and out is a std::ostream . Does this imply that std::ctype<char>::widen() (i.e., char -> char ) is guaranteed by the standard to be an identity function ( widen(ch) == ch ) for all characters in the basic source character set? If so, does this, in turn, imply that all locales are required by the standard to use the same non-wide (or multi-byte) encoding

Remove trailing comma in CSV file written for a vector using copy and ostream_iterator

爷,独闯天下 提交于 2020-06-08 20:01:10
问题 I have the following function, which writes a vector to a CSV file: #include <math.h> #include <vector> #include <string> #include <fstream> #include <iostream> #include <iterator> using namespace std; bool save_vector(vector<double>* pdata, size_t length, const string& file_path) { ofstream os(file_path.c_str(), ios::binary | ios::out); if (!os.is_open()) { cout << "Failure!" << endl; return false; } os.precision(11); copy(pdata->begin(), pdata->end(), ostream_iterator<double>(os, ",")); os

第四讲:运算符重载

大兔子大兔子 提交于 2020-03-22 00:26:17
针对:ostream & operator <<(ostream & os, const ClassType &object) 说明几点: 1.第一个形参为对ostream对象的引用,在该对象上将产生输出, ostream为非const,因为写入到流会改变流的状态 ;该形参是一个引用,因为 不能复制ostream对象 (在c++中定义的标准输入输出流类istream和ostream,其中拷贝构造函数和赋值操作符函数都被放置在了 private 部分,且只有声明,没有定义)。 2.第二个形参一般应是对要输出的类类型的引用,该形参是一个引用 以避免复制实参,减少一次拷贝 ;它设为const,因为输出一般不会改变该对象,设为const就可以用来输出const对象和非const对象。 3.返回类型是一个ostream引用,它的值通常是输出操作符所操作的ostream对象,首先 因为ostream对象不能复制,所以必须是引用 ;其次引用可以少一次拷贝,提高效率;最后,为了体现连续性,实现连续输出,达到用多个输出操作符操作一个ostream对象的效果,如果不是引用,程序返回的时候就会生成新的临时对象,也就是说,连续的两个<<操作符实际上是针对不同对象的,这就好比cout<<a<<b;与cout<<a;cout<<b;的区别。 来源: https://www.cnblogs.com/wwk510

c++ primer复习(三)

最后都变了- 提交于 2020-02-26 05:22:44
1 istream、ostream类型,cin、cout、cerr是istream或ostream类型的具体的对象,<<和>>是操纵符  getline函数的参数是istream和string类型的两个引用形参  面向对象的标准库,3个头文件:iostream,fstream、sstream  对应的类型:istream,ostream,iostream;ifstream,ofstream,fstream;istringstream,ostringstream,stringstream 2 标准库类型不能复制或赋值  流对象不能存储在vector或其他容器中;形参或返回值不能使流对象,必须传递或返回流对象的指针或引用 3 流状态  标准IO库类型定义的类型:  strm::iostat 标准IO库类型内定义的类型,用于定义流状态  数据成员:  strm::badbit strm::iostat类型的值,用于指出被破坏的流  strm::failbit strm::iostat类型的值,用于指出失败的IO操作  strm::eofbit strm::iostat类型的值,用于指出流已经到达文件结束符  成员函数:  s.eof():是否设置eofbit  s.fail():是否设置failbit  s.bad():是否设置badbit  s.good():流是否有效  s

Why do we get the build error “error C2065: 'ostringstream' : undeclared identifier” & How to fix this?

五迷三道 提交于 2020-02-04 09:35:23
问题 Hi I am compilinig a C++ solution in VS2008. ostringstream strout; I am getting the compilation error " error C2065: 'ostringstream' : undeclared identifier ". I feel I have included all the necessary header files. Can anyone kindly let me know how to fix this error (What all header files to include) ? Also I am getting a strange error like "error C2146: syntax error : missing ';' before identifier 'strout' " at the same line. Whereas I know that I havent missed ";" semi-colon @ the line whre

Why do we get the build error “error C2065: 'ostringstream' : undeclared identifier” & How to fix this?

安稳与你 提交于 2020-02-04 09:34:06
问题 Hi I am compilinig a C++ solution in VS2008. ostringstream strout; I am getting the compilation error " error C2065: 'ostringstream' : undeclared identifier ". I feel I have included all the necessary header files. Can anyone kindly let me know how to fix this error (What all header files to include) ? Also I am getting a strange error like "error C2146: syntax error : missing ';' before identifier 'strout' " at the same line. Whereas I know that I havent missed ";" semi-colon @ the line whre

Why do we get the build error “error C2065: 'ostringstream' : undeclared identifier” & How to fix this?

妖精的绣舞 提交于 2020-02-04 09:33:06
问题 Hi I am compilinig a C++ solution in VS2008. ostringstream strout; I am getting the compilation error " error C2065: 'ostringstream' : undeclared identifier ". I feel I have included all the necessary header files. Can anyone kindly let me know how to fix this error (What all header files to include) ? Also I am getting a strange error like "error C2146: syntax error : missing ';' before identifier 'strout' " at the same line. Whereas I know that I havent missed ";" semi-colon @ the line whre

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

只谈情不闲聊 提交于 2020-01-22 14:04:06
问题 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()

How to bypass a << calling as if “#ifndef DEBUG” macro in c++?

こ雲淡風輕ζ 提交于 2020-01-06 05:40:07
问题 I coded a little logging-lib for myself, and it accepts two forms calling. The one likes a normal function calling, the other likes a std::ostream << operator output. And then I defined a few of Macros respectively for every log-levels as following: #ifdef DEBUG #define LOG_DEBUG( strLogBody ) appendLog( leon_log::LogLevel_e::ellDebug, std::string( __func__ ) + "()," + ( strLogBody ) ) #define LOG_INFOR( strLogBody ) appendLog( leon_log::LogLevel_e::ellInfor, std::string( __func__ ) + "()," +