C++ Primer 学习笔记 1.1和1.2

最近在leetcode学习算法,发现自己代码水平太差劲了,所以复习一下C++。虽然早就听说C++难学,但是毕竟当初学编程最先学语言的就是C++,所以相对来说C++是最为熟悉的语言,尽管对于大佬来说,熟悉,精通C++等等是对于人类来说不可能实现的。选择了《C++ Primer》这本书作为参考书,打算跟着这本书从头到尾系统地再学一遍C++,当然算法题是不会搁下的,边复习边练算法题。

前言

前言部分提到现代C++的三个部分:

  • Low-level language, inherited from C
  • More advanced language features used for defining our own types
  • Standard library

第一章

目标: write, compile, and execute simple programs
write a program to solve a simple problem for a bookstore.
每次交易:

  1. ISBN
  2. 数量
  3. 每本价格

我们的程序必须:
• Define variables
• Do input and output
• Use a data structure to hold the data
• Test whether two records have the same ISBN
• Contain a loop that will process every record in the transaction file

1.1 写一个简单的C++程序

C++程序包含一个或多个函数(function),其中一个必须名为main。操作系统通过调用main执行C++程序。
函数定义四个要素:

  1. 返回类型
  2. 函数名
  3. 括号括起来的参数表
  4. 函数体
int main()
{
    return 0;
}

main函数的返回类型是int,代表整型数。int是内置类型(built-in type),意思是C++语言本身定义的类型。

main函数的参数表是空的,用空括号表示()。
函数名是main。

函数体是一块语句,以花括号开始和结束。这块代码的唯一语句是 return,这是终止一个函数的语句。当return语句包含值,值的类型必须和函数的返回值一致。

大多数系统中,main的返回值是一个状态指示标(status indicator)。返回值是0说明成功。非0返回值有特殊意义,具体含义是由系统定义的。通常来说,非零返回值代表有错误发生。

关键概念:类型(Types)

1.1.1 编译和运行程序

写出程序后,我们需要编译它。如何编译一个程序取决于你的操作系统和编译器。

许多基于PC的编译器是在集成开发环境(IDE)运行的,IDE捆绑了编译器和分析工具。

程序源文件命名规则

不论是用命令行还是IDE,大多数编译器需要程序源代码存储在一个或多个文件里。程序文件通常被称作源文件(source file)。多数系统中,源文件名以后缀结尾,例如.cc, .cxx, .cpp, .cp, .c等。

从命令行运行编译器

如果我们用命令行,我们通常会在控制台窗口编译程序。假设我们的mian程序在一个名为prog1.exe的文件里,我们可以用以下命令编译它:

$ CC prog1.cc

CC是编译器名, $ 系统注释。编译器可以产生一个可运行文件。在Windows系统,可运行文件名为prog1.exe,UNIX编译器倾向于将可执行文件放在名为a.out的文件中。

在Windows跑可运行文件,我们提供可执行文件的名称,并可以省略.exe文件扩展名:

$ prog1

一些系统上必须指明文件位置,即使文件已经在当前目录或文件夹。在这种情况下,我们要写:

$ .\prog1

“.”加反斜线表示文件在当前目录。
在UNIX上跑可运行文件,我们要用全文件名,加上文件扩展:

$ a.out

如果需要指明文件位置,我们用“.”加正斜线表明在当前目录。

$ ./a.out

Main返回的值被用系统依赖的方式评估。在UNIX和Windows系统上,你都必须发送一个合适的echo命令。
On UNIX systems, we obtain the status by writing

$ echo $?

To see the status on a Windows system, we write

$ echo %ERRORLEVEL%

运行GNU或Microsoft编译器:
GNU:g++
Code:

$ g++ -o prog1 prog1.cc

Microsof Visual Studio 2010: cl
Code:

C:\Users\me\Programs> cl /EHsc prog1.cpp

这里提一下,在用命令行运行程序的时候,如果直接按照书中的步骤是会出错的,提示指令不存在。查了一下网上基本上有两种解决方案,第一种是设置相关的环境变量,第二种是用vs自带的命令行工具。这里我暂且用第二种方法,先学习C++的语言特性。
在命令行编译C++程序

1.2 A First Look at Input/Output

C++没有定义输入输出语句(IO)。但是,C++包含了扩展的标准库,标准库提供了IO。

iostream库

istream–input stream
ostream–output stream
A stream is a sequence of characters read from or written to an IO device.—Characters are generated or consumed sequentially over time.

标准输入和输出

4个IO对象:

  • 标准输入cin,istream的object
  • 标准输出cout,ostream的object
  • 标准错误cerr,ostream的object
  • 一般信息clog,ostream的object

Ordinarily, the system associates each of these objects with the window in which the program is executed. So, when we read from cin, data are read from the window in which the program is executing, and when we write to cout, cerr, or clog, the output is written to the same window.
这些对象与程序运行的窗口对应。

A Program That Uses the IO Library

Code:

#include <iostream> 
int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
std::cout << "The sum of " << v1 << " and " << v2
<< " is " << v1 + v2 << std::endl;
return 0;
}

#include必须在所有函数外。
尖括号内的名字是header
例如#include 中,iostream是header。要使用库就必须include它的相关的header。

Writing to a Stream

std::cout << “Enter two numbers:” << std::endl;运行了表达式。在C++中表达式得出一个结果,通常由一个或多个操作数和一个操作符组成。这句中,<<output operator, 打印消息在标准输出。

<<有两个操作数:左边必须是一个ostream对象;右边是要打印的值。<<操作符把给定值写在给定ostream上。它的结果就是它左边的操作数。也就是我们写了给定值的ostream。

这句语句用了两次<<操作符。因为操作符返回它的左边操作数,第一个操作符的结果就变成了第二个操作符的左边操作数。所以,我们可以把输出结果串联。因此,表达式相当于:
(std::cout << “Enter two numbers:”) << std::endl;

每个操作符的左边操作数都是std::cout。也就是说,我们可以得出同样的输出用下面两个语句:

std::cout << “Enter two number:;
std::cout << std::endl;

The first output operator prints a message to the user. That message is a string literal, which is a sequence of characters enclosed in double quotation marks. The text between the quotation marks is printed to the standard output.

Manipulator
The second operator prints endl, which is a special value called a manipulator. Writing endl has the effect of ending the current line and flushing the buffer associated with that device. 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.

在例程中删掉最后的endl,改为

std::cout << “Enter two numbers:;

本以为没有endl的话可能会打印不出来,但是并不是。message照样可以打印出来,只是没有换行。加上endl之后会自动加上换行。简单查了一下,是这样说的:endl相当于\n加清理缓存。也就是std::cout << endl;相当于std::cout << ‘\n’ << std::flush;

警告: Programmers often add print statements during debugging. Such statements should always flush the stream. Otherwise, if the program crashes, output may be left in the buffer, leading to incorrect inferences about where the program crashed.
意思是用于调试程序打印的内容应该立即清理缓存否则当程序崩溃时,输出会留在缓冲区,导致干扰。

网上查了一下,看到说Or consider using ::std::cerr instead of ::std::cout since it’s unbuffered and flushed with each and every output operation. 意思是std::cerr可以在输出后自动清理缓存。

Using Names from the Standard Library

std::前缀表示cout和endl是在std命名空间(namespace)定义的。命名空间可以防止我们自己定义的名字和一个库里的名字冲突使用。标准库的所有名字都定义在std命名空间。

副作用就是当我们想使用这个库中的名字,我们必须明确说明我们想用的名字是来自std命名空间的。用scope operator(::)说明我们想用的cout是定义在std命名空间的。

Reading from a Stream

首先定义两个变量用来存放输入:
int v1 = 0, v2 = 0;
int类型,内置类型代表整型数。
初始化为0。
std::cin >> v1 >> v2;
读入输入。输入操作符(>>)的行为与输出操作符类似。 It takes an istream as its left-hand operand and an object as its right-hand operand. It reads data from the given istream and stores what was read in the given object. Like the output operator, the input operator returns its left-hand operand as its result. Hence, this expression is equivalent to
(std::cin >> v1) >> v2;
Because the operator returns its left-hand operand, we can combine a sequence of input requests into a single statement. Our input operation reads two values from std::cin, storing the first in v1 and the second in v2. In other words, our input operation executes as
std::cin >> v1;
std::cin >> v2;
这里指出>>操作符的表达式返回的也是左边操作数,也就是istream类型的cin。读出cin的值存放在右边操作数。由于返回值还是cin,因此可以串联。

Completing the Program

What remains is to print our result:
Code:

std::cout << "The sum of " << v1 << " and " << v2
<< " is " << v1 + v2 << std::endl;

这句中输出并不只有一种类型值。有些是字符串类型(string literals),有些是int类型。

练习题解答

练习题
1.3

#include <iostream>
int main(){
    std::cout << "Hello, World" << std::endl;
    return 0;
}

1.4

#include <iostream>
int main(){
    std::cout << "Enter two numbers:" << std::endl;
    int v1 = 0, v2 = 0;
    std::cin >> v1 >> v2;
    std::cout << "The times of " << v1 << " and " << v2
    << " is " << v1 * v2 << std::endl;
    return 0;
}

1.5

#include <iostream>
int main(){
    std::cout << "Enter two numbers:";
    std::cout << std::endl;
    int v1 = 0, v2 = 0;
    std::cin >> v1;
    std::cin >> v2;
    std::cout << "The times of ";
    std::cout << v1;
    std::cout << " and ";
    std::cout << v2;
    std::cout<< " is ";
    std::cout << v1 * v2;
    std::cout << std::endl;
    return 0;
}

1.6
The program is illegal. The first line is already finished because there is a
semicolon in the end. So in the second line, operator “<<” doea not have a left
operand. The third line is the same.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值