文章目录
前言
本文是c++学习笔记。
提示:以下是本篇文章正文内容,下面案例可供参考
一、c++初识
1.1 第一个c++程序
1.1.1 创建项目、文件
跳过,简单
1.1.2 编写代码
框架:
写代码,基本都需要写这些
#include<iostream>
using namespace std;
int main()
{
system("pause");
return 0;
}
打印hello world
#include<iostream>
using namespace std;
int main()
{
cout << "hello world" << endl;
system("pause");
return 0;
}
1.2 注释
#include<iostream>
using namespace std;
int main()
{
//1、单行注释
/*2、多行注释符号*/
/*
main是一个程序的入口;
每个程序都必须有这个函数
每个项目有且只有一个main
*/
cout<<"hello"<<endl;
system("pause");
return 0;
}
1.3 变量
变量存在的意义:方便我们管理内存空间;
变量的创建:数据类型 变量名=变量初始值;
#include<iostream>
using namespace std;
int main()
{
//变量创建
int a = 10;
cout << "a=" << a << endl;
system("pause");
return 0;
}
1.4 常量
作用:用于记录程序中不可以更改的数据。
C++定义常量的两种方式:
1、#define 宏常量;
2、const修饰的变量。
#include<iostream>
//#define定义的宏常量
#define DAY 7
//定义后不可修改
using namespace std;
int main()
{
//const修饰的变量也是常量
const int month = 30;
//定义后不可修改
cout << "一周总共有:" << DAY <<"天" << endl;
cout << "一个月总共有" << month<<"天" << endl;
system("pause");
return 0;
}
1.5 关键字
在定义变量或常量时不要用关键字;
#include<iostream>
using namespace std;
int main()
{
//int int = 10;会报错
system("pause");
return 0;
}
1.6 标识符命名规则
起名时最好见名知意
- 标识符不可以是关键字
- 只能由字母、数字、下划线组成
- 第一个字符不能是数字
- 标识符区分大小写
#include<iostream>
using namespace std;
int main()
{
int a1=1;
int a_1=2;
int _a1=3;
int _A1=4;//区分大小写
//int 1_a;无效,会报错
cout << "a1=" << a1 << endl;
cout << "a_1=" << a_1 << endl;
cout << "_a1=" << _a1 << endl;
cout << "_A1=" << _A1 << endl;
system("pause");
return 0;
}
二、数据类型
数据类型存在的意义:给变量分配合适的内存空间。
2.1 整型
整型即整数;
整型 | 占用空间 | 数值范围 |
---|---|---|
short (短整型) | 2字节 | -2 ^15 ~ 2^15-1 |
int (整型) | 4字节 | -2 ^31 ~ 2^31-1 |
long (长整型) | windows为4字节 | -2 ^31 ~ 2^31-1 |
long long (长长整型) | 8字节 | -2 ^63 ~ 2^63-1 |
#include<iostream>
using namespace std;
int main()
{
//整型
//1、短整型short -32768~32767
short num1 = 10;
//2、整型int
int num2 = 10;
//3、长整型long
long num3 = 10;
//4、长长整型long long
long long num4 = 10;
cout << "num1 = " << num1 << endl;
cout << "num2 = " << num2 << endl;
cout << "num3 = " << num3 << endl;
cout << "num4 = " << num4 << endl;
system("pause");
return 0;
}
2.2 sizeof关键字
利用sizeof关键字可以统计数据类型所占内存大小
#include<iostream>
using namespace std;
int main()
{
//整型:short \ int \ long \ long long
short num1 = 10;
cout << "short占用的内存空间为多大:" << sizeof(short) << endl;
cout << "num1占用的内存空间为多大:" << sizeof(num1) << endl;
int num2 = 10;
cout << "int占用的内存空间为多大:" << sizeof(int) << endl;
cout << "num2占用的内存空间为多大:" << sizeof(num2) << endl;
long num3 = 10;
cout << "long占用的内存空间为多大:" << sizeof(long) << endl;
cout << "num3占用的内存空间为多大:" << sizeof(num3) << endl;
long long num4 = 10;
cout << "long long占用的内存空间为多大:" << sizeof(long long) << endl;
cout << "num4占用的内存空间为多大:" << sizeof(num4) << endl;
//short < int <= long < long long
system("pause");
}
2.3 实型
float | 4字节 | 7位有效数字 |
---|---|---|
double | 8字节 | 15-16位有效数字 |
#include<iostream>
using namespace std;
int main()
{
//单精度
float num1 = 3.14f;//一般会多写一个f,因为默认双精度
//双精度
double num2 = 3.1415926;
cout << "num1=" << num1<<endl;
cout << "num2=" << num2 << endl;
//默认情况下,输出一个小数会显示六位有效数字
cout << "sizeof(float)=" << sizeof(float) << endl;
cout << "sizeof(double)=" << sizeof(double) << endl;
//科学计数法
float num3 = 3e-2;//3*10^2
cout << "num3=" << num3 << endl;
system("pause");
return 0;
}
2.4 字符型
用于表示单个字符。
字符型变量不是将字符本身放进内存中存储,而是将其ascii码放进存储单元中。
每个字符都对应一个编号,按照二进制存储进内存。
#include<iostream>
using namespace std;
int main()
{
//字符变量只占一个字节
char r = 'q';
cout << "r=" << r << endl;
//字符型变量所占内存大小
cout << "sizeof(char)=" << sizeof(char) << endl;
//字符型变量常见错误
char ch2 = 'b';//注意得用单引号
//单引号内只能有一个字符
cout << (int)ch2 << endl;//强制类型转换
//a-97
//A-65
system("pause");
return 0;
}
2.5 转义字符
#include<iostream>
using namespace std;
int main()
{
//转义字符
//用于表示一些不能显示出来的ASCII字符
cout << "hello\nhi" << endl;//换行符
cout << "\\" << endl;//反斜杆
cout << "a\ta" << endl; //水平制表符 \t
cout << "aa\ta" << endl;//作用:可以整齐地输出数据
system("pause");
return 0;
}
八、结构体
8.1 概念
用户自定义的数据类型
8.2 结构体的定义和使用
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>//使用c++的字符串,需要包含头文件
using namespace std;
//1、创建学生的数据类型:学生的属性:姓名、年龄、分数
//数据类型的集合
//自定义的数据类型
struct student
{
//成员列表
string name;
int age;
int score;
}s3;//顺便创建结构体变量
//2、通过学生的类型创建具体的学生
// 2.1 struct syudent s1
// 2.2 struct student s1={....}
// 2.3 在定义是顺便创建结构体变量
int main()
{
//2.1
//结构体创建变量时struct可以省略、定义时则不可以
student s1;
s1.name = "kxr";
s1.age = 21;
s1.score = 100;
cout << "姓名:" << s1.name << endl;
cout << "年龄:" << s1.age << endl;
cout << "分数:" << s1.score << endl;
//2.2
struct student s2 = { "kxr",21,100 };
cout << "姓名:" << s2.name << endl;
cout << "年龄:" << s2.age << endl;
cout << "分数:" << s2.score << endl;
//2.3 创建结构体时顺便创建变量
s3.name = "kxr";
s3.age = 21;
s3.score = 100;
cout << "姓名:" << s3.name << endl;
cout << "年龄:" << s3.age << endl;
cout << "分数:" << s3.score << endl;
system("pause");
return 0;
}
p64
8.3 结构体数组
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
using namespace std;
//结构体数组
// 1、定义结构体
struct student
{
string name;
int age;
int score;
};
int main()
{
// 2、创建结构体数组
struct student stuarray[3] =
{
{"张三",18,100},
{"李四",28,99},
{"王五",38,99}
};
// 3、给结构体数组中的元素赋值
stuarray[2].name = "赵六";
stuarray[2].age = 90;
stuarray[2].score = 60;
//4、遍历结构体数组
for (int i = 0; i < 3; i++)
{
cout << "姓名:" << stuarray[i].name <<' '
<< "年龄:" << stuarray[i].age <<' '
<< "分数:" << stuarray[i].score << endl;
}
system("pause");
return 0;
}
p65
8.4 结构体指针
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
using namespace std;
//结构体指针 ->
// 1、定义结构体
struct student
{
string name;
int age;
int score;
};
int main()
{
//2、创建结构体变量 struct 可以省略
struct student s1 = {"111",11,11};
//3、通过指针指向结构体变量 可以加上struct
student* p = &s1;
p->age = 90;
p->name = "kxr";
p->score = 100;
//4、打印
cout << "姓名:" << s1.name << endl;
cout << "年龄:" << s1.age << endl;
cout << "分数:" << s1.score << endl;
system("pause");
return 0;
}
p66
8.2 结构体的定义和使用
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。