C++学习笔记1
C++学习笔记1
学习代码要先认识代码
第一个C++程序
#include"stdafx.h" //#include是预处理指令表示包含,引入的意思
//stdafx.h是引入的标准头文件,.h表示header,所以用""引入
#include<iostream> //iostream中i表示input输入,o表示output输出
//stream河流表示流,iostream表示输入输出流
//所以在C++里面cin和cout就属于iostream
//iostream属于C++内置库或者文件所以用<>引入
//
//function
int main() //main函数作为主要函数,程序运行必须要有。
{
using namespace std; //使用命名空间std,这是简洁的写法
cout<<"输出语句"; //cout,cin,endl都是属于命名空间std的函数
cout<<endl; //endl表示endline行结束,起换行的作业。
//cout<<"语句"<<endl;
//cout<<"语句"<<"\n";//n表示enter回车的缩写
//cout<<"语句\n";
//cout<<endl<<"语句";
//这样写也可以起相同效果
cout<<"输出语句";
cin.get(); //使控制台不主动关闭
return 0; //返回值,主函数可以不用写
//但是主函数和void之外的数据类型函数基本都要有返回值
}
using namespace std;放在函数内时,只对该函数起作用,放在函数外时对所有函数起作用,就像下面这样。
#include<iostream>
using namespace std;
但是std命名空间包含了上百个功能函数,using namespace std;是将std下的所有函数都引入了进来,是一种偷懒的方法,不建议在大型项目中使用。相当于给你一箱子笔,你不一定都能用的上。但是这一箱笔你可以随时用。
用下面这种严谨的方法则是只从命名空间中引入了我们需要的函数。相当于从箱子里选出你要用的笔,其他的笔不要,你挑出来的笔可以随时用。
int main()
{
using std::cout;
using std::cin;
using std::endl;
cout<<"输出语句";
cout<<endl;
cout<<"输出语句";
return 0;
}
用下面这种方法则是只在使用函数的的地方使用该命名空间,由于每个函数前面添加命名空间所以比较麻烦。相当于你每次你从箱子里只拿一根你要用的笔,用完了放回箱子,下次又要用相同的笔时又要去箱子里拿,用完了放回箱子。
int main()
{
std::cout<<"输出语句";
std::cout<<std::endl;
std::cout<<"输出语句";
return 0;
}
注释快捷键CTRL+k CTRL+c
取消注释CTRL+k CTRL+u
//行注释
/块注释/
定义变量
根据年龄计算有多少个月
#include<iostream>
int main()
{
using namespace std;
int age; //定义一个age的整型变量
cout<<"该程序可以根据你输入的年龄计算多少个月\n";
cout<<"请输入你要计算的年龄\n";
cin>>age;
int months = age * 12; //定义一个month的变量
cout<<"一共有"<<months<<"个月\n";
cin>>age;
return 0;
}
变量名 C++命名规则
只能使用字母 数字和下划线组成
不能以数字开头
不能使用C++关键字
以两个下划线或下划线和大写字母打头的名称被保留
以一个下划线打头的名称被保留
变量命名方案和函数命名方案是一样的
关于命名风格 myWeight nMyWeight my_weight intMyWeight (在编程中根据自己的喜好个人风格来命名,必要时可以跟雇主和企业风格保持一致)
函数体{}
语句和分号(关于语句:c++每条完整的指令都是语句,所有语句都以分号结束)
整形(整数类型)
没有小数部分的数字
sizeof 取得类型大小
short
int
long
long long
无符号unsigned
在数据类型前面加上unsigned表示不为负数,存储范围扩大两倍。
变量赋值时不能超出范围外。
可以通过下面代码查询取值范围;
#include<iostream>
#include<climits>
int main()
{
using namespace std;
cout<<INT_MAX<<endl;
cout<<INT_MIN<<endl;
cout<<SHRT_MAX<<endl;
cout<<SHRT_MIN<<endl;
cout<<LONG_MAX<<endl;
cout<<LONG_MIN<<endl;
cin.get();
}
字符(基本类型)
char c
字符实际上是另一种整型
ASCII码表 http://ascii.911cha.com/
cout.put(char)直接输出字符
‘a’ ‘2’ 等表示字符,和a表示字母 2表示数字不一样。
#include<iostream>
int main()
{
using namespace std;
char c= '2';
char c2= 'a';
char c3= '\n';
char c4=97; //把97赋值给字c4 ,则输出c4得到a
cout<<c<<c2<<endl;
int a='a';
cout<<a<<endl; //输出是97
int b='2';
cout<<b<<endl;
cout.put('a'); //输出得到a
return 0;
}
布尔(基本类型)和常量
bool
true false
true 非零值 真 1
false 0 假
const int Months = 12;
常量
常量与变量相对,是不可变化的量,是一个定值。
#include <iostream>
using namespace std;
int main()
{
//bool a = true;// 真 存在的 非零 1
//bool b = false;//假的 不存在 零 0
//cout << a << endl; //输出得到1
//a = 100;
//cout << a << endl;//输出得到1
//cout << b << endl;//输出得到0
// 0 1 2 .... true false 'a'
int i = 100;
i = 90;
const int j = 90;//常量
//j = 89;
int t;
cin >> t;
return 0;
}
浮点类型(基本类型)
浮点数: 12.34 9034.3 0.000034 9.0
E表示法
+5.38E+18
2.33e+8 2.33e8 8.21E-3 -12.2e10
浮点类型 float double 和 long double
整型和浮点型都是算术类型 也是基本类型
#include <iostream>
#include <climits>
using namespace std;
int main()
{
//12.34 82394.34 9.0 0.00034
//E表示法
//-3.4e-9 // 3.4E9 3.4/1000000000
float a = 12.2;
double b = 24.3;
long double c = 21321.2;
cout << FLT_MAX << endl;
cout << FLT_MIN << endl;
int t;
cin >> t;
return 0;
}
C++算术运算符
5种基本的算术运算:加法、减法、乘法、除法和求模(求余)
-
-
- / %
%针对的是整数
- / %
-
算术优先级
int res1 = 3+7*3;
int res1 = (3+7)3;
int res3 = 42/73;
关于除法运算
如果两个都是整数,那么结果也是整数
如果其中一个为浮点数,那么进行浮点数除法
#include <iostream>
using namespace std;
int main()
{
// + - * / %
//float a;
//cout << "请输入第一个数字:";
//cin >> a;
//float b;
//cout << "请输入第二个数字:";
//cin >> b;
//int res1 = a + b;
//int res2 = a - b;
//int res3 = a * b;
//int res4 = a / b;
//cout << res1 << " " << res2 << " " << res3 << " " << res4 << endl;
//int a = 7;
//int b = 2;
//float res1 = a / b;
//cout << res1 << endl;
//int res1 = 13 % 5;
//int res2 = 13 % 5.1;
//cout << res1 << endl;
int res0 = 3 + 3;
int res1 = 3 + 7 * 3;
int res2 = (3 + 7) * 3;
int res3 = (42 / 7) * 3;
cout << res1 << " " << res2 << " " << res3 << " " << res0 << endl;
int t;
cin >> t;
return 0;
}
类型转换和auto自动推断类型
1,讲一种类型的值赋值给另一种类型的变量
float i = 3;
int i = 3.4;
int i = 3e20;
2,表达式中有不同类型的时候
float i = 3 + 3.3;
3,将参数传递给函数的时候
4,强制类型转换
float f = 34.3;
int i = (int)23.9;//旧c语言语法
int i = int(23.9);//新c++语法
int i = int(f);
#include <iostream>
using namespace std;
int main()
{
//float a = 3;//int
//int b = 4.3;//double
//int c = 3e20;//错误,3e20超出了int的取值范围
//float res = 4 + 4.4;
//float f = 34.4;
//int c = (int)23.8;
//cout << c << endl;
//int d = int(23.8);
//cout << d << endl;
//int e = int(f);
//cout << e << endl;
auto a = 'a';
cout << a << endl;
auto b = 100;
cout << b << endl;
//auto c;
auto x = 0.0;// double
double y = 0.0;
auto z = 0;//short
return 0;
}
自动推断类型
auto x = 0.0
double y = 0
auto z = 0
练习题
1,让用户输入自己的身高(米),然后把它转换为厘米,并输出出来
#include <iostream>
using namespace std;
int main()
{
cout << "请输入你的身高(米):";
float height;
cin >> height;
int heightCM = height * 100;
cout << "你的身高是" << heightCM << "厘米" << endl;
}
2,编写一个程序,让用户输入秒,然后把它转换为 多少天多少小时,多少分钟和多少秒显示出来
输入一个四位数 个位 十分位 百分位 千分位
int num;
cout << "请输入一个数字(四位数):";
cin >> num;
// 8739
int ge = num % 10;
int shi =(num % 100) / 10;
int bai = (num % 1000) / 100;
int qian = num / 1000;
cout << ge << " " << shi << " " << bai << " " << qian << endl;
#include <iostream>
using namespace std;
int main()
{
int seconds;
cout << "请输入秒数:";
cin >> seconds;
int days = seconds / (24 * 60 * 60);
int hours = (seconds - days * (24 * 60 * 60)) / (60 * 60);
int minutes = (seconds % (60 * 60)) / 60;
int secondsLeft = seconds % 60;
cout << days << "天 " << hours << "小时 " << minutes << "分钟 " << secondsLeft << "秒 " << endl;
return 0;
}
3,要求用户输入一个班级的 男生和女生的数量,并输出女生的比例(百分比)
#include <iostream>
using namespace std;
int main()
{
int girlsNumber; int boysNumber;
int total;
cout << "请输入女生数量:";
cin >> girlsNumber;
cout << "请输入男生数量:";
cin >> boysNumber;
total = girlsNumber + boysNumber;
int percent = (girlsNumber / float(total)) * 100;
cout << percent <<"%"<< endl;
return 0;
}
数组 复合类型
数组是一种数据格式,能够存储多个同类型的值。
数组声明三要素
存储的元素的类型
数组名
数组中的元素数
short months[12]
typeName arrayName[arraySize]
为什么是复合类型
使用其他类型来创建的
#include <iostream>
using namespace std;
int main()
{
//int score = 100;
//char cArray[10];
//bool bArray[90];
//float fArray[34];
//double dArray[90];
int scoreArray1[4] = { 34,3,234,45 };
int scoreArray2[4] = { 34,3 };//0
int scoreArray3[] = { 34,3,123,45,4,23 };
/*int scoreArray4[4];
scoreArray4[4] = { 34,3,3,32 };*/ //error
//scoreArray2 = scoreArray1;
int scoreArray4[]{23,2,3}; //索引 下标
cout << scoreArray1[0] << endl;
cout << scoreArray1[-1] << endl;//
cout << scoreArray1[8] << endl;//
cout << scoreArray2[3] << endl;//
cout << scoreArray4[0] << endl;//
scoreArray4[1] = 100;
cout << scoreArray4[1] << endl;//
return 0;
}
数组 初始化和访问
1,int cards[4]={4,3,2,34};
2,int cards[4]={2,1}; (其他默认初始化为0)
3,int cards[] = {234,3,2,4,234,23,4,213};
int cards[4];
cards[4]={3,3,3,3};
cards = cards2;
int cards[4]={3,3,3,3,3,3};
C++11的做法
4,int cards[4]{4,3,3,32};
5,int cards[4]{}; int cards[4]={};
禁止缩宽转换
int cards[4]{34,3,1.2};
char c[]{‘h’,‘a’,7898798,’\n’};
char c[]{‘a’,98,‘e’};
可以单独通过索引访问或者设置某个元素的值
字符串
C语言风格字符串 基于string类库的字符串
1,如何表示字符串
char website[]={‘s’,‘i’,‘k’,‘i’,‘e’,‘d’,‘u’};
char website[]={‘s’,‘i’,‘k’,‘i’,‘e’,‘d’,‘u’,’\0’};
char website[8]=”sikiedu”;
char website[]=”sikiedu”;
在确定字符串所需要的最短数组的时候,别忘记把结尾的\0计算在内
2,拼接字符串 如果字符串很长,无法放在一行,C++允许拼接字符串
cout<<”Hello enemy, i will kill you””and then the game is over”;
3, cin>>website;
cout<<website;
strlen(website);
website[3]=’\0’;
#include <iostream>
using namespace std;
int main()
{
//char website[] = { 's','i','k','i','\0','d','u' };
//char website2[]= { 's','i','k','i','e','d','u','\0' };// 空字符 空格字符
//cout << website2 << endl;
//cout << website << endl;
//char website[] = "sikiedu";
//cout << website[7] << endl;
//cout << strlen(website) << endl;
//cout << website[3] << endl;
//char website[20];
//cin >> website;// sikiedu.com sikiedu.com\0
//cout << website << endl;
//char str[] = "My name is Micheal. I come from china."
// "I like food! I like tralving!";
char name[30];
char food[40];
cout << "你的名字是:";
//cin >> name;
cin.getline(name, 30);
cout << "你喜欢的食物是:";
//cin >> food;
cin.getline(food, 40);
cout << name << "喜欢吃" << food;
return 0;
}
字符串的输入
char name[10];
char food[10];
cin >> name;
cin >> food;
cin是按照空白来区分输入的(空格 Tab Enter)
字符串的输入 其他方法
1,得到一行输入
cin.getline(name,20);
string类
#include
string str1;
string str2=”sikiedu.com”;
str2[n]单独访问某个字符
字符串数组之间不能赋值,string对象之间可以赋值
cin>>str1;cout<<str1;
str= str1 + str2; 拼接
str+=str2;
str.size()
读取一行 getline(cin,str)
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1;
string str2 = "www.sikiedu.com";
cout << str1 << endl;
cout << str2 << endl;
str1 = str2;
cout << str1 << endl;
//cin >> str1;
//cout << str1 << endl;
//getline(cin, str1);
//cout << str1 << endl;
cout << str2[5] << endl;
str1 = "sikiedu";
str2 = ".com";
string s = str1+str2;
s += str1;// s = s + str1; sikiedu.com + sikiedu sikiedu.comsikiedu
cout << s << endl;
cout << s.size() << endl;
return 0;
}
结构体
结构体比数组更灵活,结构体里面可以存储多种类型的数据
结构体需要先定义,再使用定义好的结构体声明变量
struct Vector3{ 可以放在外部也可以放在函数内
int x;
int y;
int z;
};
Vector3 v = {3,34,3};
Vector3 v{3,3,3};
Vector3 v{};//按照默认值初始化 0
v.x v.y
结构里面可以使用 基本类型和复合类型作为成员
结构体可以赋值 v1=v2;
结构数组 Vector3 v[]={{},{},{}}
#include <iostream>
using namespace std;
struct Position {
float x;
float y;
float z;
};
struct Hero {
string name;
int hp;
int attack;
Position pos;
};
int main()
{
//float playerPositionX;
//float playerPositionY;
//float playerPositionZ;
//Position playerPos = { 3,4,6.7 };
Position playerPos{ 3,4,6.7 };
//Position playerPos{ };
cout << playerPos.x << playerPos.y << playerPos.z << endl;
playerPos.y = 100;
cout << playerPos.y << endl;
Position enemyPos;
Position enemysPos[]{ {1,1,1},{2,4,7},{},{6,4,5},{123,2,2} };
cout << enemysPos[1].x << enemysPos[1].y << enemysPos[1].z << endl;
return 0;
}
共用体
共用体能够存储不同数据类型,但不能同时存储,同时只能存储其中一种类型。结构体可以同时存储多种数据类型的数据
union goodid{
long id_num;
string id_str;
};
好处节约内存,没有必要
基本类型 复合类型 数组 结构体 共用体所占用的内存大小问题
#include <iostream>
#include <string>
using namespace std;
union test
{
long id_num;
string id_str;
};
int main()
{
;
return 0;
}
枚举类型
enum enum_name{value1,value2,value3,value4};
枚举类型是整型,可以当做整型使用
整型可以强制转型为枚举类型
设置枚举量的值
#include <iostream>
using namespace std;
//枚举
enum HeroType {
Tank =1,
Magic =4,
ADC =7,
Assist
};
int main()
{
//int heroType = 1;
HeroType heroType = Tank;
heroType = ADC;
cout << heroType << endl;
int i = ADC + 2;
cout << i << endl;
heroType = HeroType(3);
cout << heroType << endl;
cout << Assist << endl;
return 0;
}
指针
指针的声明和初始化
博文 http://blog.csdn.net/xierhacker/article/details/52516742
通过new申请内存,通过delete释放内存(只能释放new的内存)
int * ps = new int;
delete ps;
delete ps;//error
int a = 3;
int * pa = &a;
delete pa;//error
#include <iostream>
using namespace std;
int main()
{
//int a = 10;
//float b = 9.7f;
//int c = 20;
// &取得一个变量的内存地址
//cout << &a << endl;
//cout << &b << endl;
*从内存地址所对应的内存处取得数据
//cout << *(&a) << endl;
//cout << a << endl;
// error:cout << *a << endl;
//int* pa = &a;
//float* pb = &b;
//cout << pa << endl;
//cout << pb << endl;
//cout << *pa << endl;
//cout << *pb << endl;
//int* p;
//p = pa;
//cout << *p << endl;
//cout << *pa << endl;
//*pa = 100;
//cout << a << " " << *pa << endl;
//*p = 300;
//cout << a << " " << *pa << " " << *pa << endl;
//int * p1 = 0;
//int * p1 = NULL;
//int * p2=0;
//int * p3 = nullptr;//c++
//cout << p1 << " " << p2 << " "<<p3<<endl;
//void * p;// p可以接收任意类型的对值
//p = &a;
p = &b;
//cout << *((int*)p) << endl;
int * p = new int;
*p = 100;
cout << *p << endl;
delete p;
return 0;
}
使用new创建数组
1,创建动态数组
int * p = new int[10];
delete[] p; // delete p is error;
数组再内存中的位置是连续的,指针指向第一个元素的地址
2,使用动态数组
p[0] 访问和设置元素
p = p +1;
指针、数组和指针运算
1,C++里面的数组其实是一个指针
2,指针运算
p+1 p-1 目前只存在于数组指针里面