1.2 c++注释
有两种表示
#include<iostream>
using namespace std;
int main()
{
//这是单行注释
/*
这是多行注释
*/
return 0;
}
#include <iostream>
该编译指令导致处理器将iostream文件的内容添加到程序中,实际上iostream文件的内容将取代程序中的代码行#include<iostream>.源文件没有被修改,只是将源代码文件和iostream组合成一个复合文件,编译的下一级段将使用该文件。
注意:使用cin和cout进行输入和输出的程序必须包含文件iostream
c++语句
cin与cout一样都是智能对象,cin使用>>运算符从输入流中抽取字符。
函数
#include<iostream>
using namespace std;
void fun(int);//fun()函数声明
int main()
{
fun(4);//调用这个函数
int num;
cin>>num;//输入整型数字fun(num)才可以正常运行
fun(num);
return 0;
}
void fun(int n)//fun()函数定义
{
cout<<"我有"<<n<<"好朋友"<<endl;
}
变量
变量名
- 在名称中只能使用字母字符、数字、下划线(-)。
- 名称的第一个字母不能是数字。
- 区分大写和小写字符
- 不能将c++关键字作为名称
基本数据类型
类型 |
占用字节数 |
void |
0 |
bool |
1 |
wchar_t |
2或4 |
char |
1 |
short |
2 |
int |
4 |
long |
4 |
long long |
8 |
float |
4 |
double |
8 |
unsigned chat |
1 |
unsigned short |
2 |
unsigned int |
4 |
unsigned long |
4 |
unsigned long long |
8 |
c++运算符
类别 |
运算符 |
结合性 |
后缀 |
()[] -> . ++ -- |
从左到右 |
一元 |
+ - !~ ++ -- sizeof |
从右到左 |
乘除 |
* / % |
从左到右 |
加减 |
+ - |
从左到右 |
关系 |
< >= > >= |
从左到右 |
相等 |
== != |
从左到右 |
逻辑and |
&& |
从左到右 |
逻辑or |
|| |
从左到右 |
条件 |
?: |
从右到左 |
赋值 |
= += -= /= %= |
从右到左 |
逗号 |
, |
从左到右 |
数组
- 存储在每个元素中的值的类型
- 数组名
- 数组中的元素数
#include <iostream>
using namespace std;
int main()
{
int arr[3];//数组声明
arr[0]=1;//数组赋值1,2,3
arr[1]=2;
arr[2]=3;
int arr1[3]={1,2,3};
int arr2[]={1,2,3,4,5,6};//三种方式数组的赋值
cout<<arr[0]<<endl;//读取数组中的元素
cout<<arr1[2]<<endl;
cout<<arr2[1]<<endl;
return 0;
}
string类
#include <iostream>
#include <string>//要是用string类必须包含头文件string
using namespace std;
int main()
{
char char1[];//创建一个空数组
char char2[10]="object";// 创建已经初始化的数组
string str1;//创建一个空的string对象
string str2="objectt";//创建一个有初始化值的string 对象
cin>>str1;//向str中输入字符
string str3;
str3=str1+str2;//将str1和str2拼接
cout<<str3<<endl;
return 0;
}
结构体
#include<iostream>
using namespace std;
#include<string>
struct student
{
string name;
int age;
float height;
};
int main()
{
//第一种赋值
student one;
one.name = "小明";
one.age = 12;
one.height = 120.5;
//第二种赋值
student two;
two = { "小刚",13,130.5 };
cout << "姓名 " << one.name << " 年龄 " << one.age << " 身高 " << one.height << endl;
cout << "姓名 " << two.name << " 年龄 " << two.age << " 身高 " << two.height << endl;
//创建结构体数组
student arr[3] =
{
{"张三",23,176.5},
{"李四",24,167.4},
{"王二",34,178.8}
};
//读取结构体数组中的数据
cout << "姓名 " << arr[0].name << " 年龄 " << arr[0].age << " 身高 " << arr[0].height << endl;
cout << "姓名 " << arr[1].name << " 年龄 " << arr[1].age << " 身高 " << arr[1].height << endl;
cout << "姓名 " << arr[2].name << " 年龄 " << arr[2].age << " 身高 " << arr[2].height << endl;
return 0;
}
/*
姓名 小明 年龄 12 身高 120.5
姓名 小刚 年龄 13 身高 130.5
姓名 张三 年龄 23 身高 176.5
姓名 李四 年龄 24 身高 167.4
姓名 王二 年龄 34 身高 178.8
F:\新建文件夹\myfirst\Debug\myfirst.exe (进程 10044)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
*/
共用体:是一种数据格式,它能够存储不同的数据类型,但只能同时存储其中的一种类型。
相比结构体可以同时存储int long 和double,共用体只能存储int long 或double。
#include<iostream>
using namespace std;
union one
{
int int_val;
long long_val;
double double_val;
};
int main()
{
one A;
A.int_val = 18;
cout << A.int_val << endl;
A.long_val = 1.24;
cout << A.long_val << endl;
return 0;
}
枚举:c++中的enum工具提供了另一种创建符号常量的方式,这种方式可以代替const.
列如:enum spectrum {red,orange,yellow,green,blue,violet,indigo,ultraviolet}
spectrum被称为枚举,red----ultraviolet作为符号常量,它们对应的整数值0~7,这些常量被称为枚举量。
#include<iostream>
using namespace std;
int main()
{
enum spectrum{red,orange,yellow,green,blue,violet};
cout << red << endl;
spectrum band;//可以用枚举名来声明这种类型的变量
band = red;
// band = 100;不被允许
//++red;不被允许
int coloe = 3 + green;
cout << "coloe " <<" = " << coloe << endl;
return 0;
}
/*
0
coloe = 6
F:\新建文件夹\myfirst\Debug\myfirst.exe (进程 21320)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
*/
指针
1
- 指针是一个变量
- 其存储的是值的地址
- &为地址运算符
例如
int a =100;
int *p=&a;
注意不可以 int*a,b。这只表明创建一个int指针a和int型变量b。
#include<iostream>
using namespace std;
int main()
{
int a = 100;
int* p = &a;
*p = 10000;
cout << "p指向的地址" << p << endl;
cout << "a 的地址" << &a << endl;
cout << "*p的值是多少" << *p << endl;
cout << "a的值是多少" << a << endl;
//由于p指向a;*p与a完全等价。
return 0;
}
/*
p指向的地址00EFF750
a 的地址00EFF750
*p的值是多少10000
a的值是多少10000
F:\新建文件夹\myfirst\Debug\myfirst.exe (进程 19944)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
*/
2
使用new开辟内存
egg:
int *p=new int ;//new运算符根据类型来确定需要多少字节的内存。
typeName * pointer_name=new typeName;数据对象可以是结构和基本类型。
#include<iostream>
using namespace std;
int main()
{
int good = 100;
int* p = new int;
*p = 100;
cout << "good 的地址" << &good << endl;
cout << "p指向的地址" << p << endl;//虽然值都是100但存储位置不同
double* p1 = new double;
long* p2 = new long;
cout << "p指针占用的字节大小" << sizeof(p) << endl;
cout << "p1指针占用的字节大小" << sizeof(p1) << endl;
cout << "p2指针占用的字节大小" << sizeof(p2) << endl;
return 0;
}
/*
good 的地址0116FD68
p指向的地址01489410
p指针占用的字节大小4
p1指针占用的字节大小4
p2指针占用的字节大小4
F:\新建文件夹\myfirst\Debug\myfirst.exe (进程 26680)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
*/
使用delete释放内存
egg:
int *p=new int ;
delete p;
delete p;//不允许
int *p=nullptr;
delete p;//不允许,一般new delete,new[] 和delete[]配对出现。
3指针与数组
#include<iostream>
using namespace std;
int main()
{
int arr[] = { 12,13,14,15 };
int* p = nullptr;//p初始化空
p = arr;//指针指向arr的首地址
cout << "*p=" << *p << endl;
cout << "*(++p)=" << *(++p) << endl;//++p将指针指向arr中下一位元素。
return 0;
}
/*
*p=12
*(++p)=13
F:\新建文件夹\myfirst\Debug\myfirst.exe (进程 14820)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
*/
for循环
- 设置初始值
- 执行测试,看循环是否应当继续
- 执行循环操作
- 更新用于循环的值
#include<iostream>
using namespace std;
int main()
{
for (int i = 0; i < 5; i++)//先执行int i=0并且只执行一次,其次cout <<"i="<<i<<endl;然后i++,再之i<5.
{
cout << "i="<<i << endl;
}
int x;
cin >>x;
for(int i=0;i<5;i=i+x)//修改步长i=i+x
{
cout<<"i="<<i<<endl;
}
return 0;
}
/*
i=0
i=1
i=2
i=3
i=4
F:\新建文件夹\myfirst\Debug\myfirst.exe (进程 5872)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
*/
递增运算符(++)递减运算符(--)
#include<iostream>
using namespace std;
int main()
{
int a = 1;
int b = 2;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
a++;
++b;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
int c = 0;
c = ++a;
cout << "c = " << c << endl;
c = b++;
cout << "c = " << c << endl;//前缀与后缀的区别只在与计算表达式的时间,前缀先进行递增再赋值,后缀先进行赋值在递增。
return 0;
}
/*
a = 1
b = 2
a = 2
b = 3
c = 3
c = 3
F:\新建文件夹\myfirst\Debug\myfirst.exe (进程 28088)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
*/
组合运算符
操作符 |
作用(L为左操作数,R为右操作数) |
+= |
将L+R赋给L |
-= |
将L-R赋给L |
*= |
将L*R赋给L |
/= |
将L/R赋给L |
%= |
将L%R赋给L |
#include<iostream>
using namespace std;
int main()
{
int a = 10;
int b = 20;
int c = 0;
c += a + b;//先执行a+b再执行+=操作。其他组合运算符雷同。
cout << "c = " << c << endl;
return 0;
}
/*
c=30;
*/
while循环
#include<iostream>
using namespace std;
int main()
{
char name[] = "object";
int i = 0;
while (name[i] != '\0')
{
cout << name[i] << endl;
i++;
}
return 0;
}
/*
o
b
j
e
c
t
*/
do while 循环
#include<iostream>
using namespace std;
int main()
{
char name[] = "object";
int i = 0;
do
{
cout << name[i] << endl;
i++;
} while (name[i] != '\0');
return 0;
}
/*
o
b
j
e
c
t
*/
if else 语句
#include<iostream>
using namespace std;
int main()
{
char name[] = "object";
int i = 0;
if (name[i] != '\0')
{
for (int i = 0; i < sizeof(name) / sizeof(name[0]); i++)
{
cout << name[i] << endl;
}
}
else
{
cout << "结束了" << endl;
}
return 0;
}
/*
o
b
j
e
c
t
*/
if else if else结构
#include<iostream>
using namespace std;
int main()
{
int a = 100;
int b = 100;
int c = 0;
if (a > b)
{
cout << "a大于b" << endl;
} else if(a<b)
{
cout << "a小于b" << endl;
}
else
{
cout << "a等于b" << endl;
}
return 0;
}
/*
a等于b
*/
逻辑表达式
运算符 |
运算符左 |
运算符右 |
结果 |
|| |
true |
false |
true |
&& |
true |
false |
false |
! |
true |
false |
? : 运算符
#include<iostream>
using namespace std;
int main()
{
int a = 100;
int b = 100;
int c = a > b ? a : b;//如果a>b则将a赋值给c,否则将b赋值给c;
cout << "c= " << c << endl;
return 0;
}
/*
c= 100
*/
switch 语句
#include<iostream>
using namespace std;
int main()
{
cout << "请输入你的选着" << endl;
int choice;
cin >> choice;
switch (choice)
{
case 1:
cout << "你选的是1" << endl;
break;
case 2:
cout << "你选的是2" << endl;
break;
default:
cout << "你选的是其他数字" << endl;
}
return 0;
}
/*
请输入你的选着
1
你选的是1
F:\新建文件夹\myfirst\Debug\myfirst.exe (进程 30276)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口.
*/
c++函数
#include<iostream>
using namespace std;
void fun();
int main()
{
fun();
return 0;
}
void fun()
{
cout << "你调用了fun()函数" << endl;
}
//你调用了fun()函数
函数可以分两类有返回值和无返回值
//egg:
1,void fun(){};
2.typeName functionName(parameterLIst)
{
statements;
return value;
}
#include<iostream>
using namespace std;
void fun();
int fun1(int a, int b);
int main()
{
fun();
int c = fun1(12, 1);
cout << c << endl;
return 0;
}
void fun()
{
cout << "你调用了fun()函数" << endl;
}
int fun1(int a,int b)
{
if (a > b)
{
return b;
}
}
/*你调用了fun()函数
1
*/
函数参数和按值传递
int fun(int a,int b,int c);含有多个参数的函数
int a=10;
int fun(int x){....};
int b=fun1(a);//按值传递
#include<iostream>
using namespace std;
int fun(int x);
int main()
{
int a = 10;
int c = fun(a);
cout << "c=" << c << endl;
return 0;
}
int fun(int x)
{
return x;
}
/*
c=10
*/
函数和数组
#include<iostream>
using namespace std;
int sum(int arr[], int n);
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9 };
int number=sum(arr, 9);
cout << "number= " << number << endl;
return 0;
}
int sum(int arr[], int n)
{
int total = 0;
for (int i = 0; i < n; i++)
{
total = total + arr[i];
return total;
}
}
/*
number= 1
*/