起步:hello c++
#include <iostream>
using namespace std;
int main(){
cout <<"hello c++"<< endl;
// system("pause");
return 0;
}
常量(不可修改):#define ,const
#define 宏常量: #define 常量名 常量值
通常在文件上方定义,表示一个常量
const修饰的变量 : const 数据类型 变量名=变量值
通常在变量定义前加关键字const,修饰该变量为常量,不可修改
#define Days 7
const int days=7;
关键字
标识符(常量,变量)
数据类型: 给变量分配合适的内存空间
整型:short,int,long,long long
sizeof: 统计数据类型所占内存大小
int a=10
sizeof(a);
sizeof(int)
实型(浮点型):单精度float,双精度double;科学计数法e
编译器默认把小数视为double,所以需要在小数后加个f.
float a = 3.14f;
默认情况下 输出小数会显示出 6位 有效数字
科学计数法e
float a=3e2; // 3* 10^2
float a=3e-2; // 3* 0.1^2
字符型 char
ASCII编码 (int)char
a - 97;
A - 65
转义字符 /
字符串 string
1、C风格字符串:char 变量名[] = "字符串值"
char str1= "hello world"
2、C++ 风格字符串:string 变量名 = "字符串值"
#include<string> // 用C++风格字符串时,需包含这个头文件
string str2 ="s"
布尔类型 bool : true false
数据输入 cin >>
// int / float / char /string bool a
cin >> a;
cout << a;
运算符
算数运算符
赋值运算符
比较运算符
逻辑运算符
三目运算符:返回的是变量,可以继续赋值
A= a>b ? a : b; //A取 a b 之间较大值
(a>b ? a : b)=100; // 将 a,b 的较大变量重新赋值为100
程序流程结构
switch case default break
int a;
cin >> a;
switch (a)
{
case 10:
cout<< "good"<<endl;
break;
case 9 :
cout<<"normal"<<endl;
break;
default:
cout<<"bad"<<endl;
}
while
while(条件语句){
循环语句;
}
#include<iostream>
#include<ctime>
using namespace std;
int main(){
// 添加随机数种子,利用当前系统时间生成随机数,防止每次随机数一致
srand((unsigned int )time(NULL));
// 生成随机数
int num=rand()%100+1; //1-100随机数
int value=0;
// 玩家猜测
cout<<"输入猜测数据"<<endl;
cin>>value;
// 判断玩家猜测
while(true){
if(value>num){
cout<<"过大"<<endl;
}
else if(value<num){
cout<<"过小"<<endl;
}
else{
cout<<"猜测正确"<<endl;
cout<<"real data: "<<num<<endl;
break;
}
cout<<"重新猜测"<<endl;
cin>>value;
}
// system("pause");
return 0;
}
do while
do{
循环语句;
}while(循环条件);
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int data=100;
do{
if (data==pow((data/100),3)+pow((data%100/10),3)+pow((data%10),3))
cout<<data<<endl;
data++;
}while(data<1000);
// system("pause");
return 0;
}
for
#include<iostream>
using namespace std;
int main(){
for(int i=1;i<10;i++){
for(int j=1;j<=i;j++){
cout<<j<<'*'<<i<<'='<<i*j<<" ";
}
cout<<endl;
}
// system("pause");
return 0;
}
跳转语句:break-switch;循环;嵌套
跳转语句:continue,筛选条件
goto
#include <iostream>
using namespace std;
int main ()
{
// 局部变量声明
int a = 10;
// do 循环执行
LOOP:do
{
if( a == 15)
{
// 跳过迭代
a = a + 1;
goto LOOP;
}
cout << "a 的值:" << a << endl;
a = a + 1;
}while( a < 20 );
return 0;
}
goto 语句一个很好的作用是退出深嵌套例程。例如,请看下面的代码片段:
for(...) {
for(...) {
while(...) {
if(...) goto stop;
.
.
.
}
}
}
stop:
cout << "Error in program.\n";
消除 goto 会导致一些额外的测试被执行。一个简单的 break 语句在这里不会起到作用,因为它只会使程序退出最内层循环
数组
数据类型 数组名[数组长度];
数据类型 数组名[数组长度]={element1,.....,elements};
数据类型 数组名[]={element1,.....,elements};
一维数组名用途:
- 可以统计整个数组在内存中的长度;sizeof(数组名);
元素个数:sizeof(arr)/sizeof(arr[0]) - 可以获取数组在内存中的首地址;数组名;
元素逆置
#include<iostream>
using namespace std;
int main(){
int arr[]={0,1,2,3,4,5,6,7};
int start =0;
int end=sizeof(arr)/sizeof(arr[0])-1;
int temp=0;
for (int i=0;i<=end;i++)
cout<<arr[i]<<' ';
cout<<endl;
for(start=0;start<end;start++,end--){
temp=arr[start];
arr[start]=arr[end];
arr[end]=temp;
}
end=sizeof(arr)/sizeof(arr[0])-1;
for (int i=0;i<=end;i++)
cout<<arr[i]<<' ';
// system("pause");
return 0;
}
冒泡排序
#include<iostream>
using namespace std;
int main(){
int array[]={9,5,6,8,2,3,4};
int len=sizeof(array)/sizeof(array[0]);
int temp;
for (int j=0;j<len;j++){
for(int i=0;i<7;i++)
cout<<array[i]<<' ';
cout<<endl;
for (int i=0;i<len-1;i++){
if(array[i]>array[i+1]){//升序
temp=array[i];
array[i]=array[i+1];
array[i+1]=temp;
}
}
len--;
}
len=sizeof(array)/sizeof(array[0]);
for(int i=0;i<len;i++)
cout<<array[i]<<' ';
//system("pause");
return 0;
}
二维数组
函数
函数声明
函数的声明与函数的定义形式上十分相似,但是二者有着本质上的不同。
声明是不开辟内存的,仅仅告诉编译器,要声明的部分存在,要预留一点空间。定义则需要开辟内存。
声明可以写多次,定义只能有一次;
函数分文件编写创建
四步骤:
- 1、创建后缀名为.h的头文件;
- 2、创建后缀名为.cpp的源文件;
- 3、在头文件中写函数的声明;
- 4、在源文件中写函数的定义;
指针
内存空间分配
在32位操作系统下,指针是占4个字节空间大小,不管是什么数据类型;
在64位操作系统下,指针是占8个字节空间大小,不管什么数据类型;
空指针(初始化指针变量),野指针
int *p =NULL; //空指针 ,是不可以进行访问
int *p = (int *)0x1100 // 野指针 指向非法内存空间
const 修饰指针
指针,数组,函数
#include<iostream>
using namespace std;
void bubbleSort(int *arr,int len);
int main(){
int arr[]={4,3,6,9,1,2,10,8,11,7,5};
bubbleSort(arr,sizeof(arr)/sizeof(arr[0]));
for(int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
cout<<arr[i]<<' ';
return 0;
}
void bubbleSort(int *arr,int len){
int temp=0;
int *p=arr;
for(int i=0;i<len-1;i++){
for(int j=0;j<len-1;j++){
if(*p > *(p+1)){
temp=*p;
*p=*(p+1);
*(p+1)=temp;
}
p++;
}
p=arr;
}
}
结构体
C++中创建变量是 struct 关键字可以省略,结构体定义时不可以省略
struct 结构体名{结构体成员列表}
// 创建数据类型:
struct Student{
// 成员列表
string name;
int age;
float score;
};
创建方式:
- struct 结构体名 变量名
struct Student s1;
s1.name="liming";
s1.age=15;
s1.score=98.5f;
- struct 结构体名 变量名={成员1值,成员2 值,…}
struct Student s2={"hello",16,89.0f};
- struct 定义结构体时顺便创建变量
struct Student{
// 成员列表
string name;
int age;
float score;
}s3={"lkj",14,56.2f};
结构体数组(一维)
struct 结构体名 数组名[元素个数] = {{},{},...,{}}
#include <iostream>
#include <string>
using namespace std;
struct Student{
string name;
int age;
float score;
};
int main(){
struct Student students[3]={
{"liming",14,99.f},
{"zhangsan",15,100.f},
{"lisi",16,98.f}
};
for(int i=0;i<3;i++){
cout<<students[i].name
<<students[i].age
<<students[i].score <<endl;
}
//system("pause");
return 0;
}
结构体指针
利用操作符 -> 可以通过结构体指针访问结构体属性
#include <iostream>
#include <string>
using namespace std;
struct Student{
string name;
int age;
float score;
};
int main(){
struct Student students[]={
{"liming",14,99.f},
{"zhangsan",15,100.f},
{"lisi",16,98.f}
};
struct Student s1={"张三",25,100.f};
struct Student *p=NULL,*q=NULL;
p=&s1;
cout<<p->name<<p->score<<p->age<<endl;
q=students;
q++;
cout<<q->age<<q->name<<q->score<<endl;
return 0;
}
结构体嵌套结构体
#include<iostream>
#include<string>
using namespace std;
struct Student {
string name;
int age;
float score;
};
struct Teacher {
int id;
string name;
struct Student student;
};
int main(){
struct Teacher teacher;
teacher.id=1000;
teacher.name="fdkjahdfj";
teacher.student={"liming",14,98.f};
//system("pause");
return 0;
}
结构体做函数参数
#include<iostream>
#include<string>
using namespace std;
struct Student {
string name;
int age;
float score;
};
void print_stu(struct Student student);
void print_stu2(struct Student *stu);
int main(){
struct Student student={"liming",14,98.f};
print_stu(student);
print_stu2(&student);
print_stu(student);
//system("pause");
return 0;
}
void print_stu(struct Student student){
cout<<student.age<<student.name<<student.score<<endl;
}
void print_stu2(struct Student *stu){
stu->name="zhangsan";
cout<<stu->age<<stu->name<<stu->score<<endl;
}
结构体中const的使用(防止误操作)
#include<iostream>
#include<string>
using namespace std;
struct Student{
string name;
int age;
float score;
};
void print_stu2(struct Student *stu);
int main(){
struct Student s1={
"zhangsan",15,97.f
};
print_stu2(&s1);
//system("pause");
return 0;
}
// 地址传递: 将函数中的形参改成指针,可以减少内存空间,而且不会复制新的副本出来
// 存在原始数据会被修改的隐患;
// 定义常量指针,指针指向的值不可以被修改;可以防止误操作;仅可以读取
void print_stu2(const struct Student *stu){
// stu->score=10;
cout<<stu->age<<stu->name<<stu->score<<endl;
}
结构体案例
#include<iostream>
#include<string>
using namespace std;
struct Student{
string name;
float score;
};
struct Teacher{
string name;
struct Student students[5];
};
void scanf_data(struct Teacher *p);
void print_data(const struct Teacher *q);
int main(){
struct Teacher teachers[3];
scanf_data(teachers);
print_data(teachers);
//system("pause");
return 0;
}
void scanf_data(struct Teacher *p){
for(int i=0;i<3;i++){
cout<<"输入"<<i<<"个老师姓名:"<<' ';
cin>>(p+i)->name;
for(int j=0;j<5;j++){
cout<<"输入老师所带"<<j<<"个学生信息:"<<' ';
cout<<"学生姓名:"<<' ';
cin>>(p+i)->students[j].name;
cout<<"学生成绩:"<<' ';
cin>>(p+i)->students[j].score;
}
}
}
void print_data(const struct Teacher *q){
for(int i=0;i<3;i++){
cout<<"老师姓名"<<(q+i)->name<<endl;
for(int j=0;j<5;j++){
cout<<"\t学生信息:"
<<"姓名"<<(q+i)->students[j].name
<<"成绩"<<(q+i)->students[j].score<<endl;
}
}
}
#include<iostream>
#include<string>
using namespace std;
struct hero{
string name;
int age;
string sex;
};
void sort(struct hero *heros,int len);
int main(){
struct hero heros[5]={
{"刘备",23,"男"},
{"关羽",22,"男"},
{"张飞",20,"男"},
{"赵云",21,"男"},
{"貂蝉",19,"女"}
};
sort(heros,5);
for(int i=0;i<5;i++){
cout<<heros[i].age<<heros[i].name<<heros[i].sex<<endl;
}
//system("pause");
return 0;
}
void sort(struct hero *heros,int len){
for(int i=0;i<len-1;i++){
struct hero temp=heros[0];
for(int j=0;j<len-1;j++){
if(heros[j].age>heros[j+1].age){
temp=heros[j];
heros[j]=heros[j+1];
heros[j+1]=temp;
}
}
}
}