C++基础笔记
C++基础笔记与习题
Mozart086
学习爱好者
展开
-
C++打印学生成绩直方图
Pratice problem: As we introduced in the lecture, try to implement a project to finish the task:” Calculate and print out the histogram of the test scores.”.Requirements:1. The number of grades of the scores is 11. A detailed division of the grades can原创 2021-08-17 11:20:59 · 547 阅读 · 2 评论 -
C++类和对象(1)
题目一定义一个学生类,学生类属性有学生的姓名与学号,学生的类行为有赋值函数与打印信息函数代码实现:#include<iostream>#include<string>using namespace std;class Student {public: string name; int ID; void assign(string name_1, int ID_1) { name = name_1; ID = ID_1; } void pri原创 2021-08-17 10:35:09 · 106 阅读 · 0 评论 -
C++函数重载
1. 重载的定义:相同函数名,不同参数函数可以在同一个作用域下共存。2. 重载的方法:(1)同一个作用于下。(2)函数的名称相同。(3)函数的参数个数不同,数据类型不同,参数顺序不同。(4)函数的返回值类型不同不可以作为函数重载的前提。测试代码示例:#include<iostream>using namespace std;void func() { cout << "func" << endl;}void func(int a) { cou原创 2021-08-17 09:56:07 · 89 阅读 · 0 评论 -
C++引用(2)
1. 引用的本质: 是一个指针常量int& ref 等价于 int* const p2. 引用的注意事项:(1)必须初始化(2)一旦被赋值,不可以再更改指向代码实现:#include<iostream>using namespace std;int main() { //引用的本质 //引用的不呢之就是指针常量 //指针常量int* const p int a = 10; cout << a << endl; int&原创 2021-08-16 18:25:25 · 153 阅读 · 0 评论 -
C++引用(1)
1. 引用的基本语法:数据类型 &新变量名=原变量名。通过新变量名可以对原始值进行修改,因为两个名称指向的地址都相同。2. 引用的注意事项:(1)引用必须初始化。(2)引用初始化之后,不可以再改变。(一旦被确定,不可以再修改)3. 引用做函数参数:简化指针,用引用可以实现形参修饰实参。//主函数中函数的调用,传参直接就是原始参数的名称swap(a, b);//交换函数void swap(int& a, int& b){}//注意这里的形参列表...原创 2021-08-16 10:45:26 · 1057 阅读 · 0 评论 -
C++程序的内存模型(2)
栈区1. 栈区存放的数据:函数形参,局部变量2. 栈区的特点:由编译器分配,程序员无权参与。因此不要返回栈区数据的地址。代码验证:#include<iostream>#include<string>using namespace std;//由于返回值是地址,因此返回值类型是指针int* fun1() {//形参也会放在栈区 //函数内局部变量 int a = 10; //返回局部变量的地址 //局部变量存放在栈区,fun1函数执行完成后,自动清除原创 2021-08-15 16:48:54 · 84 阅读 · 0 评论 -
C++程序的内存模型(1)
代码区1. 代码区的生成时间:程序形成exe可执行文件之前。2. 代码区存放的内容:CPU需要执行的指令(二进制)3. 代码区的特点:(1)共享特性:即使多次执行程序,执行的内容有且仅有一个。(2)只读特性:程序在运行时,无论如何不会对我的执行命令进行修改。全局区1. 全局区的生成时间:程序形成exe可执行文件之前。2. 全局区的特点:全局区存放的数据的生命周期只由编译器决定。全局区用于存放数据,如下:(1)全局变量(2)静态变量(3)常量:1. 字符串常量。2.原创 2021-08-15 16:21:56 · 103 阅读 · 0 评论 -
C++冒泡排序结构体数组
#include<iostream>#include<string>using namespace std;struct Hero { string name; int age; string sex;};void printHero(Hero* p,int length) { for (int i = 0; i < length; i++) { cout << p[i].name << " " << p[i].a.原创 2021-08-15 15:33:53 · 189 阅读 · 1 评论 -
C++结构体嵌套循环赋值打印
1. 学生结构体:姓名,分数2. 老师结构体:姓名,学生数组(长度为5)3. 功能:创建老师结构体数组,每个老师单元有一个学生数组,每个学生数组包括姓名和分数,函数1实现对老师和其学生信息的赋值,函数2实现对所有信息的打印#include<iostream>#include<string>#include<ctime>using namespace std;struct Student { string name; int score;};原创 2021-08-15 12:01:21 · 1611 阅读 · 1 评论 -
C++const在结构体中的应用
#include<iostream>#include<string>using namespace std;struct Student { string name; int age; int score;};//地址传递-不复制新的副本,但容易修改原本的数据//在指针前加上const,不可以通过解引用的方式对原始数据进行修改void printstu(const Student* p) { //p->age = 10; cout << .原创 2021-08-15 11:17:24 · 1074 阅读 · 0 评论 -
C++指针与结构体
#include<iostream>#include<string>using namespace std;//定义结构体struct Student { string name; int age; int score;};int main() { //结构体指针 //创建结构体变量 Student s1 = { "kelly",20,100 }; //通过指针c语言访问结构体 //创建和结构体的类型相同的指针 Student* p = &.原创 2021-08-14 16:53:00 · 273 阅读 · 0 评论 -
C++利用指针和函数进行冒泡排序
#include<iostream>using namespace std;//声明冒泡排序函数void bubble(int* p, int length);void printarray(int* p, int length);int main() { //指针,数组,函数 int arr[10] = { 4,3,6,9,1,2,10,8,7,5 }; //计算数组的长度 int length = sizeof(arr) / sizeof(arr[0]); //调.原创 2021-08-12 16:44:48 · 1538 阅读 · 0 评论 -
C++指针_5
1. 值传递:基础的函数不涉及指针时,多为值传递。main函数中调用函数中传入的参数为实参,自己定义的函数中传入的参数为形参,在不考虑返回值的情况下,形参数值的改变不会影响到实参数值的改变。如下:#include<iostream>using namespace std;//声明值传递函数void swap(int a, int b);int main() { //值传递:改变函数的形参不会影响到main函数中的实参数值 int a = 3; int b = 4;原创 2021-08-12 16:22:51 · 68 阅读 · 0 评论 -
C++指针_4
1. 常量指针:形式:const 数据类型* 指针名称;const int* p = &a;'*'代表指针,由于const在*号之前,因此中英文都称之为常量指针。const后面爱着的是int*,因此const关键词修饰的是指针。因此*解引用的操作是不被允许的。即:指针代表的地址可以发生更改,但是不可以通过解引用的方式对指针代表的地址内的数据进行更改。2. 指针常量:形式:数据类型* const 指针名称;int* const p = &a;‘*’代表指针,co原创 2021-08-12 15:44:57 · 67 阅读 · 0 评论 -
C++指针_3
1. 空指针的定义:指向0的指针为空指针,或者说,指针变量内存放的数据为0(NULL)的指针为空指针。2. 空指针的用途:在岗建立一个指针变量时,并不确定指针的指向位置(不知道变量内存放谁的地址),可以先对指针变量进行初始化,如下:int* p;p = NULL;3. 空指针的注意事项:虽然空指针内存放的数据为NULL,但是程序员无法访问这一段内存,因为这是被编译器占用的。当解引用指针时,无法访问这段空间。#include<iostream>using namespace原创 2021-08-12 09:50:48 · 87 阅读 · 0 评论 -
C++指针_2
1. 指针的赋值://方法1:int a = 10;int* p;p = &a;//方法2:int* p = &a;2. 指针的大小:#include<iostream>using namespace std;int main() { //指针占有的内存空间 int a = 10; //创建指针变量 int* p; double* q; //另一种指针赋值方法 int* p1 = &a; //不论什么类型的指针,占用的原创 2021-08-12 09:40:09 · 65 阅读 · 0 评论 -
C++指针_1
1. 指针的概念:指针是一个变量,指针的数值大多代表另一个数据的内存地址。因为有这种特性,指针常常被用来访问一段特定的内存,同时,我们也可以通过指针更改指针指向的内存所包含的数据。2. 指针的定义方式://指针数据类型 * 指针变量名称;int * p;这里需要注意:指针要与指针所指向的数据的数据类型相一致。3. 指针的定义与应用实例:#include<iostream>using namespace std;int main() { //定义整型数原创 2021-08-11 23:46:56 · 66 阅读 · 0 评论 -
C++冒泡排序
Homework problem: Enter ten numbers randomly and sort by bubble sort. Regarding the bubble sort, you can refer to the following introduction. Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are原创 2021-08-11 11:14:09 · 153 阅读 · 0 评论 -
C++数组元素逆序排列
#include<iostream>using namespace std;int main() { //元素逆序排列,正好实现关于中心对称反转变换 int arr[5] = { 1,3,2,5,4 }; //其实元素下标位置 int start = 0; //末尾元素下标位置(数组长度)- 1 int end = (sizeof(arr) / sizeof(arr[0]) - 1); //这里注意最好记录一下数组真正的长度,因为后面对end进行加减操作后,end的数.原创 2021-08-10 23:43:41 · 4830 阅读 · 0 评论 -
C++寻找数组中的最大值
#include<iostream>using namespace std;int main() { //寻找数组中的最大值 int arr[5] = { 300,350,200,250,400 }; //先假定一个最大值,一般都认为是0 int max = 0; //访问数组中的每一个元素,如果这个元素比认定的最大值max还要大,那么更新最大值 for (int i = 0; i < 5; i++) { if (arr[i]>max) { .原创 2021-08-10 23:11:32 · 24293 阅读 · 4 评论 -
C++嵌套循环打印字母表
Homework problem: 4.2. Use nested loops to print letters in the following format:ABCDEFGHIJKLMNOPQRSTU#include<iostream>using namespace std;int main() { //初始A对应的ASCII码数值 int num = 65; //行数 for (int i = 0; i < 6; i++) { //原创 2021-08-10 18:05:20 · 921 阅读 · 2 评论 -
C++寻找100以内可被17整除的最大自然数
Homework problem: 4.1. Find the largest natural number within 100 that is divisible by 17.#include<iostream>using namespace std;//本代码可以实现找到可以整除17的最大自然数(即输出85)int main() { //100以内的自然数为0到99,当正序写入时,会打印出所有的值,最后一个值才是85,不利用数组很难拿出来 //因此逆序寻找,找到第一个被原创 2021-08-10 11:48:52 · 2435 阅读 · 0 评论