C++ primer 第一章-预备,开始!

Hi!这里是山幺幺的c++ primer系列。写这个系列的初衷是,虽然在学校学习了c++,但总觉得对这门语言了解不深,因此我想来啃啃著名的c++ primer,并在这里同步记录我的学习笔记。由于我啃的是英文版,所以笔记是中英文夹杂的那种。另外由于已有一定的编程基础,所以这个系列不会含有太基础的知识,想入门的朋友最好自己啃书嘻嘻~

关于I/O


4 IO objects

  • cin:标准输入流
  • cout:标准输出流
  • cerr:error message
  • clog:日志

示例

std::cin >> v1 >> v2; //等价于
(std::cin >> v1) >> v2;
std::cout << "Hello World" << std::endl; //等价于
(std::cout << "Hello World") << std::endl;

endl的作用

  • ending the current line
  • flushing the buffer: ensures that all the output the program has generated so far is actually written to the output stream, rather than sitting in memory waiting to be written.
    PS1:debug打印信息时记得要flush buffer,否则若程序崩溃buffer中的遗留信息会造成误判
    PS2:By default, reading cin flushes cout; cout is also flushed when the program ends normally.

读未知数量的输入

while (std::cin >> value) sum += value;

关于注释


// 第一种
// Comment here
// 第二种
/* Comment
 * here
 */

关于编译报错


  • Syntax errors:语法错误
  • Type errors:数据类型错误
  • Declaration errors:使用未定义的名字

关于header


Headers from the standard library are enclosed in angle brackets (< >). Those that are not part of the library are enclosed in double quotes (" ").

关于重定向I/O到文件


$ addItems <infile >outfile
# addItems是可执行程序,infile是输入文件名,outfile是输出文件名
# 这条命令的含义:用infile中的内容作为addItems的输入,并将结果输出到outfile中
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。