C++ primer 笔记一 开始

C++ Primer 笔记一:开始


本书开篇的友情提示:

“学习一门新的程序设计语言的最好方法就是练习编写程序”

一个简单的C++程序

int main() {
	return 0;
}

每个C++程序都必须包含一个名为main()的函数,它是操作系统执行程序的调用入口。
一个函数定义包含四部分:
    1.返回类型(return type):main()的返回类型必须是int
    2.函数名(function name):用来进行函数调用,这里的函数名是main
    3.形参列表(parameter list):用()包围,指出调用函数时可以使用什么样的实参,允许为空
    4.函数体(function body):用{}包围的语句块,定义了函数所执行的动作。这里语句块里只有一条语句return 0;main的返回值用来指示执行状态,0表示成功。

标准输入输出
C++包含了一个标准库来提供IO机制及其它很多其他设施,其中两个基础类型istreamostring分别表示输入流和输出流。
一个流就是一个字符序列,随着时间的推移字符是顺序生成或者消耗的。

一个使用IO库的例子

//pracio.cpp
#inlcude <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 <iostream>告诉编译器使用iostream库,<>里指出库所在的头文件。一般包含来自标准库的头文件使用<>,不属于标准库的头文件用双引号“ ”包围,如“myheader.h"。
cin:标准输入,istream类型对象;
cout:标准输出,ostream类型对象。默认情况下,读cin会刷新cout;程序非正常终止也会刷新cout
endl:操纵符,结束当前行,将与设备关联的buffer中的内容刷到设备中;
std:::指出cincoutendl是定义在名为std的命名空间中的,作用运算符::指出定义在命名空间中的名字;
<<:输出运算符,将右侧的运算对象的值写到左侧运算对象中(左侧运算对象必须是一个ostream对象,并返回其左侧运算对象,因此<<可以连接

std::cout << "Enter two numbers:" << std::endl;

等价于

std::cout << "Enter two numbers:";
std::cout << std::endl;

>>:输入运算符,从左侧运算对象所指定的输入流中读取数据,存入右侧运算对象中,同样返回其左侧运算对象因此,因此>>也可以连接

std::cin >> v1 >> v2;

等价于

std::cin >> v1;
std::cin >> v2;

编译运行结果

$ g++ pracio.cpp -o pracio.out
$ ./pracio.out
Enter two numbers:
3 7
The sum of 3 and 7 is 10

注释
C++有两种注释:单行注释和界定符注释
单行注释以”//"开始,以换行符结束
界定符注释,即多行注释,继承自C语言,以“/*"开始,以”*/"结束。此注释方式不可嵌套

#include <iostream>

int main() {
    // 这是单行注释
    
    /* 
     * 这是多行注释
     * 多行注释不可嵌套
     * /
     
    // /* 
    //  *单行注释中任何内容都会被忽略
    //  * 包括嵌套的注释对也会被忽略
    //  */
    return 0;
}

控制流

  1. while语句 迭代语句,提供重复执行直至一个特定条件为假的机制
    while (condition)
        statement;
Created with Raphaël 2.2.0 ... IS_TRUE? statement ... yes no

alt
求1到10这10个数的和

#include <iostream>

int main() {
    int sum = 0, val = 1;
    //只要val小于等于10成立,就会执行while循环里的语句
    while(val <= 10) {
        sum += val;	// 复合运算符(+=),等价于“sum = sum + val”
        ++val;		// 前缀自增运算符(++),等价于“val = val + 1”
    }
    std::cout << "Sum of 1 to 10 inclusive is " 
    		  << sum << std::endl;
    return 0;
}

编译运行结果:

$ g++ sum.cpp -o sum.out
$ ./sum.out
Sum of 1 to 10 inclusive is 55
  1. for语句 迭代语句,提供重复执行能力
    for(init-statement; condition; expression)
         statement;
Created with Raphaël 2.2.0 ... init_statement IS_TRUE? statement expression ... yes no

在这里插入图片描述提示用户输入两个整数,打印出这两个整数所指定的范围内的所有整数:

#include <iostream>

int main() {
	std::cout << "Enter two numbers: " << std::endl;
	int start = 0, end = 0;
	std::cin >> start >> end;
	for (int i = start; i <= end; ++i) {
		std::cout << i << std::endl;
	}
	return 0;
}

编译运行结果:

$ g++ for.cpp -o for.out
$ ./for.out
Enter two numbers: 
4 10
4
5
6
7
8
9
10
  1. if语句 根据一个特定条件的值进行条件执行的语句。如果条件为真,执行if语句体;否则,执行else语句体(如果存在的话)
    if(condition)
        statement;
    或者
    if(condition)
        statement;
    else
        statement;
    或者
    if(condition)
        statement;
    else if (condition)
        statement;
Created with Raphaël 2.2.0 ... IS_TRUE? statement; ... IS_TRUE? statement; yes no yes no

在这里插入图片描述修改上一个程序,是其能处理用户输入的第一个数比第二个书小的情况:

#include <iostream>

int main() {
	std::cout << "Enter two numbers: " << std::endl;
	int start = 0, end = 0, tmp = 0;
	std::cin >> start >> end;
	
    // 如果第一个数比第二个数大,交换这两个数的值
	if (start > end) {
		tmp = start;
		start = end;
		end = tmp;
	}

	for (int i = start; i <= end; ++i) {
		std::cout << i << std::endl;
	}
	return 0;
}

编译运行:

$ g++ if.cpp -o if.out
$ ./if.out 
Enter two numbers: 
10 4
4
5
6
7
8
9
10

在这里插入图片描述
1. 读取数量不定的输入数据

// example.cpp
#include <iostream>

int main() {
    int sum = 0, value = 0;
    
    // 使用一个istream对象作为条件时,其效果是检测流的状态
    // 当遇到文件结束符,或处于无效输入时,while条件为假
    while (std::cin >> value) {
	sum += value;
    }
    std::cout << "Sum is : " << sum << std::endl;
    return 0; 
}

编译运行:

$ ./example 
11 49 65 15 36 14 E
Sum is : 190

2. 统计输入中每个值连续出现的次数

// example.cpp
#include <iostream>

int main() {

    // currval:保存正在统计的数;val:保存将读入的新值
    int currval = 0, val = 0;
    if (std::cin >> currval) {
    	// 保存当前处理值出现的次数
	    int cnt = 1;
	    while (std::cin >> val) {
	        if (val == currval) 	// 新读取的值与当前值相同,计数+1
				++cnt;
	    	else {					// 否则打印当前值次数,记住新值并重新开始计数
				std::cout << currval << " occurs "
						  << cnt << " times " << std::endl;
				currval = val;
				cnt = 1;
	  	 	 }
		}
		std::cout << currval << " occurs "
		  	      << cnt << " times " << std::endl;
	}
    return 0;
}

编译运行:

$ g++ example.cpp -o example
$ ./example 
42 42 42 56 88 88 88 19 19 19 21 21 E
42 occurs 3 times 
56 occurs 1 times 
88 occurs 3 times 
19 occurs 3 times 
21 occurs 2 times 

==== END ====

猜你喜欢

转载自blog.csdn.net/wenrui7868/article/details/86215050