cout

How to print to console when using Qt

感情迁移 提交于 2019-11-27 02:41:31
I'm using Qt4 and C++ for making some programs in computer graphics. I need to be able to print some variables in my console at run-time, not debugging, but cout doesn't seem to work even if I add the libraries. Is there a way to do this? Goz If it is good enough to print to stderr , you can use the following streams originally intended for debugging: #include<QDebug> //qInfo is qt5.5+ only. qInfo() << "C++ Style Info Message"; qInfo( "C Style Info Message" ); qDebug() << "C++ Style Debug Message"; qDebug( "C Style Debug Message" ); qWarning() << "C++ Style Warning Message"; qWarning( "C Style

order of execution in operator<<

五迷三道 提交于 2019-11-27 02:15:10
I have difficulties in understanding the sequence of calls in the code below. I was expecting to see the output below A1B2 While I can see that the output I get is BA12 I thought that the call std::cout<< b->fooA() << b->fooB() << std::endl was equivalent to call std::cout.operator<<( b->fooA() ).operator<< ( b->fooB() ) but I can see that this is not the case. Can you help me understanding better how this does it work and the relationship with the global operator<< ? Is this last ever called in this sequence? Regards AFAG #include <iostream> struct cbase{ int fooA(){ std::cout<<"A"; return 1;

How to rollback lines from cout?

て烟熏妆下的殇ゞ 提交于 2019-11-27 01:40:49
问题 I'm coding a task monitoring, which updates tasks' progress using cout. I'd like to display one task progress per line, therefore I have to rollback several lines of the console. I insist on "several" because \b does the job for one line, but does not erase \n between lines. I tried std::cout.seekp(std::cout.tellp() - str.length()); but tellp() returns -1 (failure). 回答1: You can do cout << '\r'; to jump to the beginning of the current line, but moving upwards is system-specific. For Unix, see

C++ printing boolean, what is displayed?

拥有回忆 提交于 2019-11-27 01:33:35
I print a bool to an output stream like this: #include <iostream> int main() { std::cout << false << std::endl; } Does the standard require a specific result on the stream (e.g. 0 for false )? The standard streams have a boolalpha flag that determines what gets displayed -- when it's false, they'll display as 0 and 1 . When it's true, they'll display as false and true . There's also an std::boolalpha manipulator to set the flag, so this: #include <iostream> #include <iomanip> int main() { std::cout<<false<<"\n"; std::cout << std::boolalpha; std::cout<<false<<"\n"; return 0; } ...produces

How do I correctly organize output into columns?

孤者浪人 提交于 2019-11-27 00:55:25
问题 The first thing that comes to my mind is to do a bunch of \t's, but that would cause words to be misaligned if any word is longer than any other word by a few characters. For example, I would like to have something like: Name Last Name Middle initial Bob Jones M Joe ReallyLongLastName T Instead, by including only "\t"'s in my cout statement I can only manage to get Name Last Name Middle initial Bob Jones M Joe ReallyLongLastName T or Name Last Name Middle initial Bob Jones M Joe

输放输出格式控制

情到浓时终转凉″ 提交于 2019-11-27 00:12:11
#include <iostream> using namespace std; int main() {int a=21; cout.setf(ios::showbase); cout<<"dec:"<<a<<endl; cout.unsetf(ios::dec); cout.setf(ios::hex); cout<<"hex:"<<a<<endl; cout.unsetf(ios::hex); cout.setf(ios::oct); cout<<"oct:"<<a<<endl; char *pt="China"; cout.width(10); cout<<pt<<endl; cout.width(10); cout.fill('*'); cout<<pt<<endl; double pi=22.0/7.0; cout.setf(ios::scientific); cout<<"pi="; cout.width(14); cout<<pi<<endl; cout.unsetf(ios::scientific); cout.setf(ios::fixed); cout.width(12); cout.setf(ios::showpos); cout.setf(ios::internal); cout.precision(6); cout<<pi<<endl; return 0

C++的cin/cout高阶格式化操作

早过忘川 提交于 2019-11-27 00:11:12
这篇文章主要讲解如何在C++中使用cin/cout进行高级的格式化输出操作,包括数字的各种计数法(精度)输出,左或右对齐,大小写等等。通过本文,您可以完全脱离scanf/printf,仅使用cin/cout来完成一切需要的格式化输入输出功能(从非性能的角度而言)。更进一步而言,您还可以在<sstream>、<fstream>上使用这些格式化操作,从而代替sscanf/sprintf和fscanf/fprintf函数。为方便描述,下文仅以cin/cout为例进行介绍。 一、综述 cin/cout是STL库提供的一个iostream实例,拥有ios_base基类的全部函数和成员数据。进行格式化操作可以直接利用setf/unsetf函数和flags函数。cin/cout维护一个当前的格式状态,setf/unsetf函数是在当前的格式状态上追加或删除指定的格式,而flags则是将当前格式状态全部替换为指定的格式。cin/cout为这两个函数提供了如下参数(可选格式): ios::dec 以10进制表示整数 ios::hex 以16进制表示整数 ios::oct 以8进制表示整数 ios::showbase 为整数添加一个表示其进制的前缀 ios::internal 在符号位和数值的中间插入需要数量的填充字符以使串两端对齐 ios::left 在串的末尾插入填充字符以使串居左对齐 ios:

容器STL

☆樱花仙子☆ 提交于 2019-11-27 00:04:11
一、迭代器iterator 迭代器是容器的一种遍历方式,每种容器都定义了自己的迭代器类型 声明一个迭代器:    容器名称<数据类型>::iterator 迭代器名称 vector<int>::iterator it; map<int,int>::iterator it; set<int>::iterator it; ....... 使用:   for(it=vec.begin();it!=vec.end();it++)     cout<<*it; 二、vector 1、常用操作 empty():判断向量是否为空,为空返回真,否则为假 begin():返回向量(数组)的首元素地址 end(): 返回向量(数组)的末元素的下一个元素的地址 clear():清空向量 front():返回得到向量的第一个元素的数据 back():返回得到向量的最后一个元素的数据 size():返回得到向量中元素的个数 push_back(数据):将数据插入到向量的尾部 pop_back():删除向量尾部的数据 ..... 2、遍历方式 vector向量支持两种方式遍历,因为可以认为vector是一种动态数组,所以可以使用数组下标的方式,也可以使用迭代器   3、二维动态数组 #include <iostream> #include <vector> #include <list> #include

How to make cout behave as in binary mode?

让人想犯罪 __ 提交于 2019-11-26 23:14:15
Every time I do 'cout << endl' or even 'cout << "\n"' and then launch my program under Windows to output to a file ("a.exe < test.in > result.out") I get "\r\n" line endings in "result.out". Is there on earth a way to stop it doing so and just output "\n" on every 'cout << "\n"'? Thanks in advance. This works using Visual Studio 2013: #include <io.h> #include <fcntl.h> #include <iostream> int main( int argc, char * argv[] ) { _setmode( _fileno( stdout ), _O_BINARY ); std::cout << std::endl; } It will output only [0A], and not [0D][0A]. I don't believe you can force std::cout to output binary

cout map with boost::any

半城伤御伤魂 提交于 2019-11-26 21:57:32
问题 I have a "dictionary" std::map<std::string, boost::any> (or std::any , if you want) that can possibly be nested. Now, I would like to display the map. Since boost::any obviously doesn't play nicely with << , things are getting a little nasty. So far, I'm checking the type, cast it, and pipe the cast to cout : for (const auto &p: map) { std::cout << std::string(indent + 2, ' ') << p.first << ": "; if (p.second.type() == typeid(int)) { std::cout << boost::any_cast<int>(p.second); } else if (p