cout

cout << order of call to functions it prints?

感情迁移 提交于 2019-11-25 23:42:23
问题 the following code: myQueue.enqueue(\'a\'); myQueue.enqueue(\'b\'); cout << myQueue.dequeue() << myQueue.dequeue(); prints \"ba\" to the console while: myQueue.enqueue(\'a\'); myQueue.enqueue(\'b\'); cout << myQueue.dequeue(); cout << myQueue.dequeue(); prints \"ab\" why is this? It seems as though cout is calling the outermost (closest to the ;) function first and working its way in, is that the way it behaves? 回答1: There's no sequence point with the << operator so the compiler is free to

How to print out the contents of a vector?

蹲街弑〆低调 提交于 2019-11-25 23:13:34
问题 I want to print out the contents of a vector in C++, here is what I have: #include <iostream> #include <fstream> #include <string> #include <cmath> #include <vector> #include <sstream> #include <cstdio> using namespace std; int main() { ifstream file(\"maze.txt\"); if (file) { vector<char> vec(istreambuf_iterator<char>(file), (istreambuf_iterator<char>())); vector<char> path; int x = 17; char entrance = vec.at(16); char firstsquare = vec.at(x); if (entrance == \'S\') { path.push_back(entrance

How do I print a double value with full precision using cout?

早过忘川 提交于 2019-11-25 22:16:54
问题 So I\'ve gotten the answer to my last question (I don\'t know why I didn\'t think of that). I was printing a double using cout that got rounded when I wasn\'t expecting it. How can I make cout print a double using full precision? 回答1: You can set the precision directly on std::cout and use the std::fixed format specifier. double d = 3.14159265358979; cout.precision(17); cout << "Pi: " << fixed << d << endl; You can #include <limits> to get the maximum precision of a float or double. #include

C++ 基本的输入输出

允我心安 提交于 2019-11-25 19:31:02
一、C++ 基本的输入输出 C++ 标准库提供了一组丰富的输入/输出功能,我们将在后续的章节进行介绍。本章将讨论 C++ 编程中最基本和最常见的 I/O 操作。 C++ 的 I/O 发生在流中,流是字节序列。如果字节流是从设备(如键盘、磁盘驱动器、网络连接等)流向内存,这叫做输入操作。如果字节流是从内存流向设备(如显示屏、打印机、磁盘驱动器、网络连接等),这叫做输出操作。 1.1 I/O 库头文件 头文件 函数和描述 该文件定义了 cin、cout、cerr 和 clog 对象,分别对应于标准输入流、标准输出流、非缓冲标准错误流和缓冲标准错误流。 该文件通过所谓的参数化的流操纵器(比如 setw 和 setprecision),来声明对执行标准化 I/O 有用的服务。 该文件为用户控制的文件处理声明服务。我们将在文件和流的相关章节讨论它的细节。 1.2 标准输出流(cout) 预定义的对象 cout 是 iostream 类的一个实例。cout 对象"连接"到标准输出设备,通常是显示屏。cout 是与流插入运算符 << 结合使用的,如下所示: # include <iostream> using namespace std ; int main ( ) { char str [ ] = "Hello C++" ; cout << "Value of str is : " <<

C++ 数据抽象

℡╲_俬逩灬. 提交于 2019-11-25 19:27:37
一、C++ 数据抽象 数据抽象是指,只向外界提供关键信息,并隐藏其后台的实现细节,即只表现必要的信息而不呈现细节。 数据抽象是一种依赖于接口和实现分离的编程(设计)技术。 让我们举一个现实生活中的真实例子,比如一台电视机,您可以打开和关闭、切换频道、调整音量、添加外部组件(如喇叭、录像机、DVD 播放器),但是您不知道它的内部实现细节,也就是说,您并不知道它是如何通过缆线接收信号,如何转换信号,并最终显示在屏幕上。 因此,我们可以说电视把它的内部实现和外部接口分离开了,您无需知道它的内部实现原理,直接通过它的外部接口(比如电源按钮、遥控器、声量控制器)就可以操控电视。 现在,让我们言归正传,就 C++ 编程而言,C++ 类为数据抽象提供了可能。它们向外界提供了大量用于操作对象数据的公共方法,也就是说,外界实际上并不清楚类的内部实现。 例如,您的程序可以调用 sort() 函数,而不需要知道函数中排序数据所用到的算法。实际上,函数排序的底层实现会因库的版本不同而有所差异,只要接口不变,函数调用就可以照常工作。 在 C++ 中,我们使用类来定义我们自己的抽象数据类型(ADT)。您可以使用类 iostream 的 cout 对象来输出数据到标准输出,如下所示: # include <iostream> using namespace std ; int main ( ) { cout <

How to print Unicode character in C++?

主宰稳场 提交于 2019-11-25 19:26:49
I am trying to print a Russian "ф" ( U+0444 CYRILLIC SMALL LETTER EF) character, which is given a code of decimal 1092 . Using C++, how can I print out this character? I would have thought something along the lines of the following would work, yet... int main (){ wchar_t f = '1060'; cout << f << endl; } To represent the character you can use Universal Character Names (UCNs). The character 'ф' has the Unicode value U+0444 and so in C++ you could write it '\u0444' or '\U00000444'. Also if the source code encoding supports this character then you can just write it literally in your source code. /

C++ 命名空间

蓝咒 提交于 2019-11-25 19:26:43
一、C++ 命名空间 假设这样一种情况,当一个班上有两个名叫 Zara 的学生时,为了明确区分它们,我们在使用名字之外,不得不使用一些额外的信息,比如他们的家庭住址,或者他们父母的名字等等。 同样的情况也出现在 C++ 应用程序中。例如,您可能会写一个名为 xyz() 的函数,在另一个可用的库中也存在一个相同的函数 xyz()。这样,编译器就无法判断您所使用的是哪一个 xyz() 函数。 因此,引入了命名空间这个概念,专门用于解决上面的问题,它可作为附加信息来区分不同库中相同名称的函数、类、变量等。使用了命名空间即定义了上下文。本质上,命名空间就是定义了一个范围。 1.1 定义命名空间 命名空间的定义使用关键字 namespace,后跟命名空间的名称,如下所示: namespace namespace_name { // 代码声明 } 为了调用带有命名空间的函数或变量,需要在前面加上命名空间的名称,如下所示: name : : code ; // code 可以是变量或函数 让我们来看看命名空间如何为变量或函数等实体定义范围: # include <iostream> using namespace std ; // 第一个命名空间 namespace first_space { void func ( ) { cout << "Inside first_space" <<