iostream

printf和cout的区别

跟風遠走 提交于 2019-12-19 22:06:56
问题描述: printf("%s",string)出现乱码 与 cout << string 无乱码 C标准输入输出(stdio)的方法是借助输出函数printf和scanf printf输出格式:%[标志][输出最小宽度][.精度][长度] %c 输出单个字符 %d /i以十进制形式输出带符号整数 %e/E以指数形式输出单精度实数 %f 以小数形式输出单双精度实数 %o 以八进制形式输出无符号整数 printf("%s",string)----->> printf("%s",string.c_str())==cout<<string; C++中iostream的方法是cout/cin cout实际是一个iostream类的对象,每次调用的时候就会输出对应的字符串,调用的实际上就是成员运算符函数operator<< iostream::operator<< 对于各种C++基本数据类型的重载 const 类名 & operator<< (int value)const;//对整型变量进行重载 const 类名 & operator<< (char* str)const;//对字符串变量进行重载 来源: CSDN 作者: 毛先森 链接: https://blog.csdn.net/weixin_41586854/article/details/103618055

C++标准组成部分 流

自作多情 提交于 2019-12-19 11:53:13
我们一直使用cout将数据写到屏幕,使用cin从键盘读取数据,但对他们的工作原理却没有全面的了解。 其实C++没有定义如何将数据写入屏幕或文件,也没有定义如何将数据读入程序。然而,使用c++编写程序时,这些是不可缺少的部分,标准的C++库包含用来方便输入输出的(I/O)的iostream库。 将输入和输出同语言分开并使用库来处理输入输出的优点是,更容易使语言独立于平台。也就是说,可以在PC上编写C++程序,然后在Sun工作站上重新编译并运行它们;或者在Linux上重新编译使用Windows编译器创建的代码,然后运行它。 注:库是一组扩展名为.obj的文件,可将它们链接到程序以得到额外的功能。这是最基本的代码重用形式。 当前,除对平面文件输入外,流对C++就不那么重要了,但是流是C++标准的组成部分,还是要知道一些的。 数据流的封装 流的主要目标是将从磁盘读取文件或将输入写入控制台屏幕的问题封装起来。创建流后,程序就可以使用它,流将负责处理所有细节。 正如读者可能预期到的,C++从面向对象的角度来实现流和缓冲区。他使用一系列的类和对象来完成这项任务: 1、streambuf类管理缓冲区,其成员函数提供了填充、清空、刷新和处理缓冲区的其他功能。 2、ios类是输入和输出流类的基类,它有一个成员变量为streambuf对象。 3、istream和ostream类派生而来的

Why would I include iostream and ostream separately? [duplicate]

穿精又带淫゛_ 提交于 2019-12-19 06:46:01
问题 This question already has answers here : iostream vs ostream what is different? (5 answers) Closed 3 years ago . I've noticed that many people include iostream and ostream in C++ programs separately, like so: #include <iostream> #include <ostream> int main() { } Why would anyone do that? Since iostream inherits from ostream, it should include everything in it, right? Is there some obscure reason? What about simple (std::cout) code? 回答1: Although stringstream inherits from iostream , it is not

Strange output, not as expected

你离开我真会死。 提交于 2019-12-19 05:09:12
问题 sorry for asking you a stupid question, but I just can't figure out why I keep on getting this output. So here is my code: #include <cstdio> #include <iostream> using namespace std; unsigned n = 4242; int getRemainderOf(int m, int n, int& quotient); static int l = 0; int main() { int quotient; // the value of which should be changed after calling the func. for(int i=-1; i<=1; ++i) { for(int j=-1; j<=1; ++j) { if( i && j ) { cout << "("<< i*3 << "," << j*7 << ") " <<( getRemainderOf(i*3, 7*j,

C++ cout with prefix

半世苍凉 提交于 2019-12-19 04:55:34
问题 I want a ostream with a prefix at the beginning of every line redirected on cout; I try this: #include <iostream> #include <thread> class parallel_cout : public std::ostream { public: parallel_cout(std::ostream& o):out(o){} template <typename T> std::ostream& operator<< (const T& val) { out << "prefix " << val; return *this; } std::ostream& out; }; int main() { parallel_cout pc(std::cout); pc<<"a\nb"<<"c\n"; } but i have in output prefix a b without c. why this? 回答1: The way you modify the

In simple terms, what is the purpose of flush() in ostream

给你一囗甜甜゛ 提交于 2019-12-19 03:46:33
问题 By definition taken from: http://www.cplusplus.com/reference/iostream/ostream/flush/ , it is not clear why the function exists, and for what purpose you would use it for. Why not call flush(), every time your write to the stream? 回答1: In all likelihood, the word flush comes from exactly what you'd flush in real-life. A toilet... So let's try a toilet analogy: Flushing every time a new one drops into the bowl is very time-consuming and a complete waste of water. That's a big problem today

Why are iostreams not copyable?

烈酒焚心 提交于 2019-12-19 03:20:15
问题 It's possible to make a local copy of an iostream object, using rdbuf and copyfmt . This allows formatting changes to be locally scoped: std::ostream & operator << ( std::ostream & os, foo const & smth ) { cloned_ostream cs( os ); cs << std::hex << smth.num; // os is not switched to hexadecimal, which would be a confusing side-effect return os; } Why don't the stream classes provide copy constructors to do this? Have relevant C++ best practices changed since they were designed as non-copyable

Is it OK to use iostreams with int as character-type?

谁说我不能喝 提交于 2019-12-18 16:47:10
问题 When trying to come up with an answer to this question, I wrote this little test-program: #include <iostream> #include <fstream> #include <vector> #include <iterator> #include <algorithm> void writeFile() { int data[] = {0,1,2,3,4,5,6,7,8,9,1000}; std::basic_ofstream<int> file("test.data", std::ios::binary); std::copy(data, data+11, std::ostreambuf_iterator<int>(file)); } void readFile() { std::basic_ifstream<int> file("test.data", std::ios::binary); std::vector<int> data(std::istreambuf

Using boost in WDK build environment for applications?

别说谁变了你拦得住时间么 提交于 2019-12-18 15:53:43
问题 I am using the Windows Driver Kit (WinDDK 6001.18001) to build my userspace application rather than Visual Studio 2005. I am taking this approach because we also have to build driver components, so I'd prefer to have a single build environment to build everything. Microsoft itself uses this approach for several products. This was working fine until I started using Boost 1.38.0. I'm not using C++ in the kernel mode components, just the userspace applications. In C++ code, it's natural to use

How is std::iostream buffered?

谁说胖子不能爱 提交于 2019-12-18 12:49:12
问题 General use case I am trying to implement a basic shell. Description I need to read user input until some delimiters are pressed so a corresponding action can be performed. Those delimiter could be a single 'a', a single 'b' or a single 'c'. An input example would look like this (where > is the shell prompt): > 111-222-333-444a Ok, '111-222-333-444' entered Why do I want inline delimiter instead of 'new-line' delimiter? Because I would like to listen to keyboard event such as 'up-arrow' to