cout

Why “cout” works weird for “unsigned char”?

寵の児 提交于 2019-11-27 16:31:45
I have the following code: cvtColor (image, image, CV_BGRA2RGB); Vec3b bottomRGB; bottomRGB=image.at<Vec3b>(821,1232); When I display bottomRGB[0] , it displays a value greater than 255. What is the reason for this? As you have commented, the reason is that you use cout to print its content directly. Here I will try to explain to you why this will not work. cout << bottomRGB[0] << endl; Why "cout" works weird for "unsigned char" ? It will not work because here bottomRGB[0] is a unsigned char (with value 218 ), cout actually will print some garbage value (or nothing) as it is just a non

When does cout flush?

一世执手 提交于 2019-11-27 16:06:56
I know endl or calling flush() will flush it. I also know that when you call cin after cout , it flushes too. And also when the program exit. Are there other situations that cout flushes? I just wrote a simple loop, and I didn't flush it, but I can see it being printed to the screen. Why? Thanks! for (int i =0; i<399999; i++) { cout<<i<<"\n"; } Also the time for it to finish is same as with endl both about 7 seconds. for (int i =0; i<399999; i++) { cout<<i<<endl; } There is no strict rule by the standard - only that endl WILL flush, but the implementation may flush at any time it "likes". And

C++中的对象初始化

你说的曾经没有我的故事 提交于 2019-11-27 15:49:41
总所周知,C++对象在创建之时,会由构造函数进行一系列的初始化工作。以没有继承关系的单个类来看,除了构造函数本身的产生与指定,还涉及到初始化步骤,以及成员初始化方式等一些细节,本篇笔记主要对这些细节进行介绍,弄清C++对象在初始化过程中一些基本运行规则。 构造函数指定 通常,我们在设计一个类的时候,会为这个类编写对应的default constructor、copy constructor、copy assignment operator,还有一个deconstructor。即便我们仅仅编写一个空类,编译器在编译时仍旧会为其默认声明一个default constructor、copy constructor、copy assignment operator与deconstructor,如果在代码里面存在着它们的使用场景,那么这个时候编译器才会创建它们。 1 class MyCppClass 2 { 3 } 一旦我们为一个类编写了default constructor,那么编译器也就不会为其默认生成default constructor,对于其他几个函数也一样。对于编译器默认生成的constructor来说,它会以一定规则对每一个数据成员进行初始化。考虑到成员初始化的重要性,在编写自己的constructor时就需要严谨认真了,特别是在类的派生与继承情况下这点显得尤为重要

Using << operator to write to both a file and cout

笑着哭i 提交于 2019-11-27 15:41:57
I'd like to overload << operator to write the value it takes to a file and cout. I have tried to do it with following code, but couldn't succeed it. It just writes the value to text file. Any help would be appreciated. Thank you. void operator<<(std::ostream& os, const string& str) { std::cout << str; os << str; } int main() { ofstream fl; fl.open("test.txt"); fl << "!!!Hello World!!!" << endl; return 0; } Create a helper class and overload operators that takes care of streaming to two streams. Use the helper class instead of trying to override the standard library implementations of the

Float formatting in C++

夙愿已清 提交于 2019-11-27 15:01:33
How do you format a float in C++ to output to two decimal places rounded up? I'm having no luck with setw and setprecision as my compiler just tells me they are not defined . cout << "Total : " << setw(2) << total << endl; total outputs: Total : 12.3961 I'd like it to be: 12.40 or 12.39 if it's too much work to round up. You need to include <iomanip> and provide namespace scope to setw and setprecision #include <iomanip> std::setw(2) std::setprecision(5) try: cout.precision(5); cout << "Total : " << setw(4) << floor(total*100)/100 << endl; or cout << "Total : " << setw(4) << ceil(total*10)/10

What can explain std::cout not to display anything?

血红的双手。 提交于 2019-11-27 14:43:22
问题 For whatever reason, std::cout does not display anything with my application. The description of my development environment follows. I am working on a Qt application using Qt Creator. Since Qt Creator can't be launched from my station (XP64), i am currently developping it with Visual Studio 2008 and the Qt plugin (by importing the .pro project file). Everything seems fine and the application works. In some cases (depending on command line arguments), i don't want to launch the HIM, just to

Assigning cout to a variable name

霸气de小男生 提交于 2019-11-27 13:55:08
问题 In ANSI C++, how can I assign the cout stream to a variable name? What I want to do is, if the user has specified an output file name, I send output there, otherwise, send it to the screen. So something like: ofstream outFile; if (outFileRequested) outFile.open("foo.txt", ios::out); else outFile = cout; // Will not compile because outFile does not have an // assignment operator outFile << "whatever" << endl; I tried doing this as a Macro function as well: #define OUTPUT outFileRequested

c++ force std::cout flush (print to screen)

给你一囗甜甜゛ 提交于 2019-11-27 13:05:59
I have code such as the following: std::cout << "Beginning computations..."; // output 1 computations(); std::cout << " done!\n"; // output 2 The problem, however, is that often output #1 and output #2 appear (virtually) simultaneously. That is, often output #1 does not get printed to the screen until after computations() returns. Since the entire purpose of output #1 is to indicate that something is going on in the background (and thus to encourage patience from the user), this problem is not good. Is there any way to force the std::cout buffer to get printed before the computations() call?

How to make C++ cout not use scientific notation

家住魔仙堡 提交于 2019-11-27 12:59:31
double x = 1500; for(int k = 0; k<10 ; k++){ double t = 0; for(int i=0; i<12; i++){ t += x * 0.0675; x += x * 0.0675; } cout<<"Bas ana: "<<x<<"\tSon faiz: "<<t<<"\tSon ana: "<<x+t<<endl; } this the output Bas ana: 3284.78 Son faiz: 1784.78 Son ana: 5069.55 Bas ana: 7193.17 Son faiz: 3908.4 Son ana: 11101.6 Bas ana: 15752 Son faiz: 8558.8 Son ana: 24310.8 Bas ana: 34494.5 Son faiz: 18742.5 Son ana: 53237 Bas ana: 75537.8 Son faiz: 41043.3 Son ana: 116581 Bas ana: 165417 Son faiz: 89878.7 Son ana: 255295 Bas ana: 362238 Son faiz: 196821 Son ana: 559059 Bas ana: 793246 Son faiz: 431009 Son ana: 1

[PAT Basic] 1002 .写出这个数

笑着哭i 提交于 2019-11-27 10:49:46
1002 写出这个数 (20 分) 题目来源 读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字。 输入格式: 每个测试输入包含 1 个测试用例,即给出自然数 n 的值。这里保证 n 小于 1。 输出格式: 在一行内输出 n 的各位数字之和的每一位,拼音数字间有 1 空格,但一行中最后一个拼音数字后没有空格。 输入样例: 1234567890987654321123456789 输出样例: yi san wu 分析: 用string接收输入,将string中的每一位数字累加到sum里,用 to_string(sum) 将sum转化成字符串nums,然后逐一输出nums对应的拼音 1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 int main() 6 { 7 string str; 8 string output[10] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" }; 9 int sum = 0; 10 11 cin >> str; 12 int length = str.length(); 13 14 for (int i = 0; i < length; ++i) 15 { 16 sum =