c++基础笔记
C++笔记
我用的软件是Visual Studio Code,这是我目前已知最好用的软件
软件下载地址
一、c++基础
1.第一个程序
首先学习了用C++编写第一个程序HelloWorld
#include<iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
system("pause");//按任意键继续
return 0;
}
2.注释
在vscode中经常用到的快捷键:
Visual Studio Code注释快捷键注释快捷键:ctrl + k, ctrl + c
解除注释快捷键:ctrl + k, ctrl + u
#include<iostream>
using namespace std;
/*
多行注释:对这一段进行解释
main函数是程序的入口
main函数有且只能有一个
*/
int main()
{
cout << "Hello World!" << endl;//单行注释:对这一行进行解释,输出Hello World!
system("pause");
return 0;
}
3.变量
(像极了爱情,不停改变的值)
变量存在的意义:方便管理内存空间
怎么创建变量:数据类型 变量名 = 变量初始值
#include <iostream>
using namespace std;
int main()
{
int a = 10;
cout << a << endl;
system("pause");
return 0;
}
4.常量
(程序中不可改变的数据,忠贞不渝)
两种定义常量的方式:
1.#define:宏定义常量
#define 常量名 常量值
2.const修饰的变量
const 数据类型 常量名 = 常量值
#include <iostream>
using namespace std;
#define day 7 //第一种方法
int main()
{
//day = 15;报错因为常量不可以修改
const int a = 10; //第二种方法
//a = 15;报错const修饰的变量也是常量
cout << day << " " << a << endl;
system("pause");
return 0;
}
5.关键字
这个不打重要,在后面的学习中慢慢就会都认识,不用背
6.标识符
标识符命名规则:(见名知意)
- 标识符不能是关键字
- 标识符只能由字母、数字、下划线组成
- 第一个字符必须是字母或下划线
- C++标识符区分大小写
7.数据的输入
从键盘上获取数据
语法:cin >> 变量;
数据的输出
语法:cout >> 变量;
二、数据类型
1.整形
数据类型存在意义:给变量分配合适的内存空间
short(短整型) int(整形) long(长整型) long long(长长整形)
#include <iostream>
using namespace std;
int main()
{
//1.短整型
short num1 = 10;
//2.整形
int num2 = 11;
//3.长整形
long num3 = 12;
//4.长长整型
long long num4 = 13;
cout << num1 << ' ' << num2 << ' ' << num3 << ' ' << num4 << endl;
system("pause");
return 0;
}
2.sizeof关键字
统计数据类型占用内存大小
语法:sizeof(数据类型/变量)
#include <iostream>
using namespace std;
int main()
{
//1.短整型
short num1 = 10;
cout << sizeof(short) << endl;
//2.整形
int num2 = 11;
cout << sizeof(int) << endl;
//3.长整形
long num3 = 12;
cout << sizeof(long) << endl;
//4.长长整型
long long num4 = 13;
cout << sizeof(long long) << endl;
system("pause");
return 0;
}
总结:short < int <= long <= long long
3.实型(浮点型)
默认情况下输出小数都是六位小数
- float(单精度)
- double(双精度)
#include <iostream>
using namespace std;
int main()
{
//1.单精度
float a = 3.14f;
//float a = 3.14;c++中默认小数都是double类型的,在赋值过程中进行了强制类型转换
cout << a << endl;
//2.双精度
double b = 3.14;
cout << b << endl;
//科学计数法
float c = 3e2;// 3 * 10 ^ 2;
float d = 3e-2;// 3 * 0.1 ^ 2;
cout << c << ' '<< d << endl;
system("pause");
return 0;
}
4.字符型
用于表示单个字符
语法:char ch = ‘a’;
#include <iostream>
using namespace std;
int main()
{
//1.字符型变量创建方式
char ch = 'a';
cout << ch << endl;
//2.字符型变量所占内存大小
cout << sizeof(char) << endl;
//3.字符型变量常见错误
//char ch1 = "a";创建字符型变量时要用单引号
//char ch2 = 'ssjdhks';创建字符型变量时单引号内只能有一个字符
//4.字符型变量对应的ASCII编码
cout << (int)ch << endl;
system("pause");
return 0;
}
5.转义字符
表示一些不能显示出来的ASCII字符
#include <iostream>
using namespace std;
int main()
{
//转义字符
//1.换行符\n
cout << "hello world\n";//等价于cout << "hello world" << endl;
//2.反斜杠
cout << "\\" << endl;
//3.水平制表符 \t一共占八个字符 作用是能更整齐的输出数据
cout << "aaa\thelloworld" << endl;
cout << "aa\thelloworld" << endl;
cout << "aaaaa\thelloworld" << endl;
system("pause");
return 0;
}
6.字符串型
作用:表示一串字符
两种风格:
- c风格字符串
char变量名[]=“字符串值”; - c++风格字符串
string 变量名 = “字符串值”;
#include <iostream>
using namespace std;
int main()
{
//字符串
//C语言风格
// char str1[] = "hello world";
// cout << str1 << endl;
//c++风格
string str2 = "hello world";
//正常来说要加#include <string>,
//但是因为#include<iostream>中隐含了这个头文件就不用加了
//最好平时做的时候就加上
cout << str2 << endl;
system("pause");
return 0;
}
7.布尔类型 bool
代表真或假的值,占用一个字节大小
true – 真(本质是1)
false – 假(本质是0)
三、程序流程结构
最基本数据结构:顺序、选择、循环
if语句、三目运算符、switch语句这些都和C语言没有什么区别,都很简单所以只学习案例
if语句案例
三个小猪称体重 有三只小猪a、b、c,请分别输入三只小猪的体重,并且判断哪只小猪最重?
#include <iostream>
using namespace std;
int main()
{
double a,b,c;
cin >> a >> b >> c;
if(a>b&&a>c)
{
cout << "a最重" << endl;
}
if(b>a&&b>c)
{
cout << "b最重" << endl;
}
if(c>a&&a>b)
{
cout << "b最重" << endl;
}
system("pause");
return 0;
}
while循环案例一
猜数字
系统随机生成以恶搞1到100之间的数字,玩家进行猜测,如果猜错,提示玩家数字过大或过小,如果猜对则恭喜玩家胜利,并且退出游戏。
生成随机数的方法:
rand()%100 生成 0~99 的随机数
rand()%100+1 生成 1~100 的随机数
#include <iostream>
#include <stdlib.h>//rand()和srand()函数的头文件
#include <ctime>//time()的头文件
using namespace std;
int main()
{
//添加随机数种子,作用是利用当前系统时间生成随机数,防止每次都一样
srand((unsigned int)time(NULL));
//1.系统生成随机数
int num = rand() % 100;
//2.玩家进行猜测
int val;
while (1)
{
cin >> val;
//3.判断玩家的猜测
if (val > num)
{
cout << "猜大了" << endl;
}
else if (val < num)
{
cout << "猜小了" << endl;
}
else
{
cout << "猜对了" << endl;
break;
}
}
system("pause");
return 0;
}
循环结构案例二
水仙花数
水仙花数是指一个3位数。它的每个位上的数字的3次幂之和等于它本身。 eg:1 ^ 3 + 5 ^ 3 + 3 ^ 3 = 153
请利用do……while语句,求出所有3位数中的水仙花数
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int i = 100;
do
{
int g, s, b;
g = i % 10;
s = i / 10%10;
b = i /100;
if(pow(g,3)+pow(s,3)+pow(b,3)==i)
{
cout << i << ' ';
}
i++;
}while(i<1000);
system("pause");
return 0;
}
循环结构案例三
敲桌子
从1开始数到数字100,如果数字个位含有7,或者数字十位含有7,或者该数字是7的倍数,我们打印敲桌子,其余数字直接输出。
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 100; i++)
{
int g, s;
g = i % 10;
s = i / 10 % 10;
if (g == 7 || s == 7 || i % 7 == 0)
{
cout << "敲桌子" << endl;
}
else
{
cout << i << endl;
}
}
system("pause");
return 0;
}
循环结构案例三
利用嵌套循环,实现九九乘法表。
#include <iostream>
using namespace std;
int main()
{
cout << "打印乘法口诀表:" << endl;
for (int i = 1; i < 10; i++)
{
for (int j = 1; j <= i; j++)
{
cout << j << "*" << i << "=" << i * j << ' ';
}
cout << "\n";
}
system("pause");
return 0;
}
跳转语句
1.break语句
作用:用于跳出选择结构或者循环结构。
break使用的时机:
- 使用在switch语句中,作用是终止case并跳出switch
- 出现在循环语句中,作用是跳出当前的循环语句
- 出现在嵌套循环中,跳出最近的内层循环语句
2.continue语句
作用:在循环语句中,跳出本次循环中余下的尚未执行的语句,继续执行下一次循环
3.goto语句(功能太强大,一般不打使用,会导致逻辑混乱)
作用:可以无条件跳转语句
语法: goto 标记;
解释:如果标记的名称存在,执行到goto语句时,会跳转到标记的位置
#include <iostream>
using namespace std;
int main()
{
//goto语句
cout << "1.xxxx" << endl;
cout << "2.xxxx" << endl;
cout << "3.xxxx" << endl;
cout << "4.xxxx" << endl;
cout << "5.xxxx" << endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
//goto语句
cout << "1.xxxx" << endl;
cout << "2.xxxx" << endl;
goto flag;
cout << "3.xxxx" << endl;
cout << "4.xxxx" << endl;
flag:
cout << "5.xxxx" << endl;
system("pause");
return 0;
}
四、数组
1.概述
数组就是一个集合,里面存放了相同类型的数据元素
特点1:数组中的每个元素都是相同的数据类型
特点2:数组是由连续的内存位置组成的
2.一维数组
定义初始化
三种定义方式:
- 数据类型 数组名[数组长度];
- 数据类型 数组名[数组长度] = {值1,值2…};
- 数据类型 数组[ ] ={值1,值2…};
#include <iostream>
using namespace std;
int main()
{
/*
1. 数据类型 数组名[数组长度];
2. 数据类型 数组名[数组长度] = {值1,值2…};
3. 数据类型 数组[ ] ={值1,值2…};
*/
//1.数据类型 数组名[数组长度];
int arr[5];
//给数组元素赋值
//数组下标从0开始索引
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
//访问数组元素
for (int i = 0; i < 5; i++)
{
cout << arr[i] << ' ';
}
cout << "\n";
//2. 数据类型 数组名[数组长度] = {值1,值2…};
int arr2[5] = {10, 20, 30, 40, 50};
//int arr2[5] ={10,20,30};初始化的时候,如果没有全部写完,会用0补全剩余数据
//访问数组元素
for (int i = 0; i < 5; i++)
{
cout << arr[i] << ' ';
}
cout << "\n";
//3. 数据类型 数组[ ] ={值1,值2…};
int arr3[] = {10, 20, 30, 40, 50, 60};
for (int i = 0; i < 6; i++)
{
cout << arr3[i] << ' ';
}
cout << "\n";
system("pause");
return 0;
}
总结1:数组名的命名规范与变量名命名规范一致,不要和变量重名
总结2:数组中下标是从0开始索引
一维数组数组名
用途:
- 可以统计整个数组在内存中的长度 sizeof(arr)
- 可以获取数组在内存中的首地址 cout << arr << endl;
#include <iostream>
using namespace std;
int main()
{
//1. 可以统计整个数组在内存中的长度 sizeof(arr)
//2. 可以获取数组在内存中的首地址 cout << arr << endl;
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
cout << "整个数组内存空间为:" << sizeof(arr) << endl;
cout << "每个元素占用的内存空间为:" << sizeof(arr[0]) << endl;
cout << "数组中元素的个数为:" << sizeof(arr)/sizeof(arr[0]) <<endl;
cout << "数组首地址为:"<< arr << endl;
cout << "数组第一个元素的地址为:" << &arr[0] << endl;
cout << "数组第二个元素的地址为:" << &arr[1] << endl;
system("pause");
return 0;
}
arr = 100;数组名是常量,不可以进行赋值操作
一维数组案例1:
五只小猪称体重
在一个数组记录了五只小猪的体重,如:int arr[5] = {300,350,200,400,250};找出并打印第几只小猪最重和小猪体重。
#include <iostream>
using namespace std;
int main()
{
int arr[5] = {300, 350, 200, 400, 250};
int max = 0;
int flag;
int n = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < n; i++)
{
if (arr[i] > max)
{
flag = i;
max = arr[i];
}
}
cout << "第" << flag + 1 << "只小猪最重,体重为:" << max << endl;
system("pause");
return 0;
}
一维数组案例2:
数组元素逆置
请声明一个5个元素的数组,并且将元素逆置,(如原数组为:1,3,2,5,4;逆置后输出结果为:4,5,2,3,1);
#include <iostream>
using namespace std;
int main()
{
int arr[5] = {1, 3, 2, 5, 4};
int start = 0;
int end = sizeof(arr) / sizeof(arr[0])-1;
int t;
for (int i = 0; i < 5; i++)
{
cout << arr[i] << " ";
}
cout << "\n";
while (start < end)
{
if (arr[start] < arr[end])
{
t = arr[start];
arr[start] = arr[end];
arr[end] = t;
start++;
end--;
}
}
for (int i = 0; i < 5; i++)
{
cout << arr[i] << " ";
}
system("pause");
return 0;
}
二维数组
定义:二维数组就是在一维数组上加了一个维度。
二维数组定义方式
- 数据类型 数组名[行数][列数];
- 数据类型 数组名[行数][列数]={{数据1,数据2},{数据3,数据4}};
- 数据类型 数组名[行数][列数]={数据1,数据2,数据3,数据4};
- 数据类型 数组名[ ][列数]={数据1,数据2,数据3,数据4};
以上四种定义方式,利用第二种更加直观,提高代码的可读性。
#include <iostream>
using namespace std;
int main()
{
//二维数组定义方式:
//1.数据类型 数组名[行数][列数];
int arr[2][3];
// arr[0][0] = 1;
// arr[0][1] = 2;
// arr[0][2] = 3;
// arr[1][0] = 4;
// arr[1][1] = 5;
// arr[1][2] = 6;
//外层循环打印行数,内层循环打印列数
// for(int i=0;i<2;i++)
// {
// for(int j=0;j<3;j++)
// {
// cout << arr[i][j] << " ";
// }
// cout << "\n";
// }
//2.数据类型 数组名[行数][列数]={{数据1,数据2},{数据3,数据4}};
int arr2[2][3]={{1,2,3},{4,5,6}};
// for(int i=0;i<2;i++)
// {
// for(int j=0;j<3;j++)
// {
// cout << arr2[i][j] << " ";
// }
// cout << "\n";
// }
//3.数据类型 数组名[行数][列数]={数据1,数据2,数据3,数据4};
int arr3[2][3]={1,2,3,4,5,6};
// for(int i=0;i<2;i++)
// {
// for(int j=0;j<3;j++)
// {
// cout << arr2[i][j] << " ";
// }
// cout << "\n";
// }
//4.数据类型 数组名[ ][列数]={数据1,数据2,数据3,数据4};
int arr4[][3]={1,2,3,4,5,6};
// for(int i=0;i<2;i++)
// {
// for(int j=0;j<3;j++)
// {
// cout << arr2[i][j] << " ";
// }
// cout << "\n";
// }
return 0;
}
总结:在定义二维数组时,如果初始化了数据,可以省略列数,行数不可省
二维数组数组名
查看二维数组所占内存空间
获取二维数组首地址
#include <iostream>
using namespace std;
int main()
{
//二维数组数组名
int arr[2][3] =
{
{1, 2, 3},
{4, 5, 6}};
//1.查看二维数组所占内存空间
cout << "二维数组占用内存空间为: " << sizeof(arr) << endl;
cout << "二维数组第一行占用内存空间为: " << sizeof(arr[0]) << endl;
cout << "二维数组第一个元素占用内存空间为: " << sizeof(arr[0][0]) << endl;
cout << "二维数组行数: " << sizeof(arr) / sizeof(arr[0]) << endl;
cout << "二维数组列数: " << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;
//2.获取二维数组首地址
cout << "二维数组首地址为: " << arr << endl;
cout << "二维数组第一行首地址: "<< arr[0] << endl;
cout << "二维数组第二行首地址: "<< arr[1] << endl;
cout << "二维数组第一个元素的地址: "<< &arr[0][0] << endl;
cout << "二维数组第二个元素的地址: "<< &arr[0][1] << endl;
system("pause");
return 0;
}
二维数组应用案例
考试成绩统计
有三名同学(张三,李四,王五),在一次考试中成绩分别如下表,请分别输出三名同学的总成绩。
#include <iostream>
using namespace std;
int main()
{
int scores[3][3] = {
{100, 100, 100},
{90, 50, 100},
{60, 70, 80}};
string names[3] = {"张三", "李四", "王五"};
int sum = 0;
for (int i = 0; i < 3; i++)
{
sum = 0;
for (int j = 0; j < 3; j++)
{
sum += scores[i][j];
}
cout << names[i] << "的总成绩为: " << sum << endl;
}
cout << "\n";
system("pause");
return 0;
}