iostream

redirect std::cout to a custom writer

試著忘記壹切 提交于 2019-12-28 11:57:00
问题 I want to use this snippet from Mr-Edd's iostreams article to print std::clog somewhere. #include <iostream> #include <iomanip> #include <string> #include <sstream> int main() { std::ostringstream oss; // Make clog use the buffer from oss std::streambuf *former_buff = std::clog.rdbuf(oss.rdbuf()); std::clog << "This will appear in oss!" << std::flush; std::cout << oss.str() << '\\n'; // Give clog back its previous buffer std::clog.rdbuf(former_buff); return 0; } so, in a mainloop, I will do

Android ndk-build iostream: No such file or directory

丶灬走出姿态 提交于 2019-12-28 05:56:49
问题 I'm having problem with compiling cpp file using ndk-build tool (windows 7 with cygwin) Error appears when I try to compile cpp file with #include: jni/native.cpp:5:20: error: iostream: No such file or directory Here is my cpp file: #include <jni.h> #include <string.h> #include <stdio.h> #include <android/log.h> #include <iostream> #define DEBUG_TAG "NDK_SampleActivity" #define LOG_TAG "hellojni" #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) #define LOGE(...) _

How to output to the console in C++/Windows

岁酱吖の 提交于 2019-12-28 04:08:08
问题 When using iostream in C++ on Linux, it displays the program output in the terminal, but in Windows, it just saves the output to a stdout.txt file. How can I, in Windows, make the output appear in the console? 回答1: Since you mentioned stdout.txt I google'd it to see what exactly would create a stdout.txt; normally, even with a Windows app, console output goes to the allocated console, or nowhere if one is not allocated. So, assuming you are using SDL (which is the only thing that brought up

What exactly is streambuf? How do I use it?

拥有回忆 提交于 2019-12-28 02:23:08
问题 I'm trying to learn a bit more about how I/O streams work in C++, and I'm really confused at when to use what. What exactly is a streambuf ? When do I use a streambuf , as compared to a string , an istream , or a vector ? (I already know the last three, but not how streambuf compares to them, if it does at all.) 回答1: Stream buffers represent input or output devices and provide a low level interface for unformatted I/O to that device. Streams, on the other hand, provide a higher level wrapper

How can I set the decimal separator to be a comma?

 ̄綄美尐妖づ 提交于 2019-12-28 02:07:09
问题 I would like to read and write pi as 3,141592 instead of 3.141592 , as using the comma is common in many European countries. How can I accomplish this with iostream s? In other words cout << 3.141592; should print 3,141592 to standard output. 回答1: You should use basic_ios::imbue to set the preferred locale. Take a look here: http://www.cplusplus.com/reference/ios/ios_base/imbue/ Locales allow you to use the preferred way by the user, so if a computer in Italy uses comma to separate decimal

How can I set the decimal separator to be a comma?

久未见 提交于 2019-12-28 02:07:01
问题 I would like to read and write pi as 3,141592 instead of 3.141592 , as using the comma is common in many European countries. How can I accomplish this with iostream s? In other words cout << 3.141592; should print 3,141592 to standard output. 回答1: You should use basic_ios::imbue to set the preferred locale. Take a look here: http://www.cplusplus.com/reference/ios/ios_base/imbue/ Locales allow you to use the preferred way by the user, so if a computer in Italy uses comma to separate decimal

Overload handling of std::endl?

微笑、不失礼 提交于 2019-12-27 12:17:25
问题 I want to define a class MyStream so that: MyStream myStream; myStream << 1 << 2 << 3 << std::endl << 5 << 6 << std::endl << 7 << 8 << std::endl; gives output [blah]123 [blah]56 [blah]78 Basically, I want a "[blah]" inserted at the front, then inserted after every non terminating std::endl ? The difficulty here is NOT the logic management, but detecting and overloading the handling of std::endl . Is there an elegant way to do this? Thanks! EDIT: I don't need advice on logic management. I need

How do I deal with the max macro in windows.h colliding with max in std?

北战南征 提交于 2019-12-27 12:05:47
问题 So I was trying to get valid integer input from cin, and used an answer to this question. It recommended: #include <Windows.h> // includes WinDef.h which defines min() max() #include <iostream> using std::cin; using std::cout; void Foo() { int delay = 0; do { if(cin.fail()) { cin.clear(); cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } cout << "Enter number of seconds between submissions: "; } while(!(cin >> delay) || delay == 0); } Which gives me an error on Windows, saying

HELLO WORLD

时间秒杀一切 提交于 2019-12-27 09:15:46
例:在屏幕上输出“Hello World!”。 #include<iostream> //使用cin,cout,须调用iostream库,否则编译出错 #include<cstdlib> //使用system()调用cstdlib库 using namespace std; int main() { cout<<"Hello World!"<<endl; //输出“Hello World!” system("pause"); //暂停,使用system("pause")调用cstdlib库 return 0; //结束整个程序 } 说明: 1、以“//”开头为注释行,“//”后的内容用以对语句进行说明,输入程序时可以不 输入;C++语言的另一种注释方法是以“/*”表示注释的开始,以“*/”表示注释的结 束,在“/*”和“*/”之间的为注释内容。 2、#include<iostream> 告诉编译器的预处理器将输入输出流的标准头文件(iostream)包括在本程序中。这个 头文件包括了C++中定义的基本标准输入输出程序库的声明。 3、#include< >和#include" "的区别 < >引用的是编译器的类库路径里面的头文件 " "引用的是你程序目录的相对路径中的头文件 4、using namespace std 使用std(标准)名字空间的意思,所谓的名字空间是标准C+

C++ Primer Plus解读(第2章(共18章))

一个人想着一个人 提交于 2019-12-26 10:16:14
一、创建C++程序与C++程序的一般格式 1.C++的基本结构 int main() { statements return 0 ; } 其中: ①函数头对函数与程序其他部分之间的接口进行了总结,函数体指出函数应该做什么的计算机指令; ②C++中,每一条完整的指令都称之为语句,所有的语句都以分号结束(分号也叫终止符,是语句结束的标记,不可省略)。 ③返回语句:结束该函数。 ④调用函数与被调用函数:返回类型描述被调用函数返回给调用函数信息的属性;形参列表描述调用函数传递给被调用函数的信息。 二、#include 编译指令 预处理器:处理以#开头的编译指令,典型预处理器操作:在源代码被编译之前,替换或添加文本(例如:#include 的作用是在最终编译之前,使用iostream文件内容替换该编译指令)。 C++头文件没有扩展名(这是一个动态发展的过程)。是指在include的时候不加后缀。由此产生一种名称空间的说法。 名称空间旨在解决大型程序以及多个商家的程序组合更容易。可通过名称空间调用不同的同名函数。 最初我们都使用iostream.h与cout的组合,后来增加了名称空间的特性后,大家都不愿改之前的代码,即使用iostream并添加std,便于使用搞出了一条using namespace std的指令(潜在的问题是如果std中与其他部分有重名函数或者变量