cout

【C++】表达式中各类数值型数据间的混合运算

a 夏天 提交于 2019-11-30 21:13:23
注意:以下内容摘自文献[1],修改了部分内容。 1.运算中各类型数据转换方向如下: 高 double ← float ↑ ↑ | long | ↑ | unsigned | ↑ 低 int ← char, short 其中,横向的箭头表示必定的转换,即使是两个char型的数据相加减,每个数都先转换为int,然后在运算;纵向箭头表示当运算对象为不同类型时转换的方向。例如int型与double数据进行运算时,先将int型转换为double型,然后在两个double型数据间进行运算,结果为double型。 P31[1] (从取值范围来看,刚好是从小到大) 2.下面一个例子: // 20191009.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <typeinfo> using namespace std; int main() { system("color 3f"); char a = 10; int b = 3; cout << "type(a):" << typeid(a).name() << endl; cout << "type(b):" << typeid(b).name() << endl; cout << "type(a+b):" << typeid(a + b).name

C++ Qt: Redirect cout from a thread to emit a signal

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 19:59:32
问题 In a single thread, I have this beautiful class that redirects all cout output to a QTextEdit #include <iostream> #include <streambuf> #include <string> #include <QScrollBar> #include "QTextEdit" #include "QDateTime" class ThreadLogStream : public std::basic_streambuf<char>, QObject { Q_OBJECT public: ThreadLogStream(std::ostream &stream) : m_stream(stream) { m_old_buf = stream.rdbuf(); stream.rdbuf(this); } ~ThreadLogStream() { // output anything that is left if (!m_string.empty()) { log

Programmatically Ignore Cout

我怕爱的太早我们不能终老 提交于 2019-11-30 16:55:17
问题 Does anybody know if there is a trick to toggle all the cout << functions to not print out visible output? I am trying to hack together some code written by me and some other people to put together a demo. I would rather not redirect the output to a file and would like a solution that had some measure of compatibility between Windows and Linux. In my scenario I have many many lines of code with with various #defines controlling when certain methods produce debug output. I want to call

C++中的static的作用

时光总嘲笑我的痴心妄想 提交于 2019-11-30 16:18:36
C++中的static的作用 C++中的关键字static,顾名思义表示静止,静态,下面是C++中static的一些常见应用 一,作用于函数内部的局部变量 局部作用域静态变量的特点:当一个函数返回后,下一次再调用时,该变量还会保持上一回的值,函数内部的静态变量只开辟一次空间,且不会因为多次调用产生副本,也不会因为函数返回而失效 例如: 如果我想实现fun()函数功能:在函数内部定义count计数器,打印出每次调用它的次数,你可能会这样写,如下 #include<iostream> using namespace std; void fun() { int count = 0;//不加关键字static count++; cout << "count=" << count << endl; } int main() { cout << "Calling the “fun()”for the first time! " << endl; fun(); cout << "Calling the “fun()”for the second time! " << endl; fun(); return 0; } 我们预计结果为: 第一次调用,打印出结果 1 第二次调用,打印出结果 2 我们试着运行后,结果并不是我们想要的,如下图: 两次运行结果都是1,这是为什么呢,原来是每次调用函数结束后

How to write a function wrapper for cout that allows for expressive syntax?

。_饼干妹妹 提交于 2019-11-30 16:07:37
I'd like to wrap std::cout for formatting, like so: mycout([what type?] x, [optional args]) { ... // do some formatting on x first std::cout << x; } and still be able to use expressive syntax like mycout("test" << i << endl << somevar, indent) instead of being forced to be more verbose like mycout(std::stringstream("test") << i ...) How can I implement this? What type to make x ? Edit: added consideration for optional arguments How about this: struct MyCout {}; extern MyCout myCout; template <typename T> MyCout& operator<< (MyCout &s, const T &x) { //format x as you please std::cout << x;

mycout automatic endl

元气小坏坏 提交于 2019-11-30 14:23:23
I'd like implement class MyCout , which can provide possibility of automatic endl, i.e. this code MyCout mycout; mycout<<1<<2<<3; outputs 123 //empty line here Is it possible to implement class with such functionality? UPDATE: Soulutions shouldn't be like that MyCout()<<1<<2<<3; i.e. they should be without creating temporary object This is simply a variant of Rob's answer , that doesn't use the heap. It's a big enough change that I didn't want to just change his answer though struct MyCout { MyCout(std::ostream& os = std::cout) : os(os) {} struct A { A(std::ostream& r) : os(r), live(true) {} A

Unbuffered output with cout

喜你入骨 提交于 2019-11-30 13:56:06
问题 How can you get unbuffered output from cout, so that it instantly writes to the console without the need to flush (similar to cerr)? I thought it could be done through rdbuf()->pubsetbuf, but this doesn't seem to work. The following code snippet below is supposed to immediately output to the console, and then wait a few seconds. But instead, it just waits, and only outputs when the program exits and the buffer is flushed. #include <iostream> int main() { std::cout.rdbuf()->pubsetbuf(0, 0);

How to determine the size of an array of strings in C++?

て烟熏妆下的殇ゞ 提交于 2019-11-30 13:15:42
问题 I'm trying to simply print out the values contained in an array. I have an array of strings called 'result'. I don't know exactly how big it is because it was automatically generated. From what I've read, you can determine the size of an array by doing this: sizeof(result)/sizeof(result[0]) Is this correct? Because for my program, sizeof(result) = 16 and sizeof(result[0]) = 16 so that code would tell me that my array is of size 1. However that doesn't appear correct, because if I manually

cin---cout----using namespace std

旧城冷巷雨未停 提交于 2019-11-30 12:59:08
2019-09-26 今天学了cin和cout的用法,之前就知道输入和输出,根本不知道它原本的意义,cin是istream的对象,cout是 ostream的对象。 1、 头文件 #include<iostream> using namespace std;//c++标准库所用的所有标识符《类、函数、对象等名称》都是在一个特殊的std符号定义的 //如果没有using.....,则在写语句的时候要写上:std::cout std::cin std::endl; 2、 >>和<<的用法 啊!这就是函数的 重载 呀!!!在c语言里面这是移位符,但在c++里面是输入流和输出流 3、 cin的一些用法 int i;cin>>i :表示从输入对象中读取一个整数 cin.get():cin不能识别空格、制表符、回车,可以用此命令获得空格 cin.peek():从字符串挑一个字符 判断是否和条件相似,然后 放回去 cin.ignore(n):忽略前n位数 cin.getline(arrray,10):获取数组里面的前10位 cin.gcout():计算个数 cin.read(array,10):读取前10位 一个实例 1 //输入一串整数和任意段的空格,整数必须位于同一行,空格 允许出现在任何位置,当按下键盘中的enter,结束输入。 2 //对输入的字符串中的整数求和。 3 #include

std::cout deal with uint8_t as a character

喜你入骨 提交于 2019-11-30 09:21:04
问题 If I run this code: std::cout << static_cast<uint8_t>(65); It will output: A Which is the ASCII equivalent of the number 65. This is because uint8_t is simply defined as: typedef unsigned char uint8_t; Is this behavior a standard? Should not be a better way to define uint8_t that guaranteed to be dealt with as a number not a character? I can not understand the logic that if I want to print the value of a uint8_t variable, it will be printed as a character. P.S. I am using MSVS 2013. 回答1: