0基础学习c++ 与CPLEX
·
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
此文用来记录从0开始的研究生编程记录,学习c++与CPLEX用来对优化模型进行求解
以下是本篇文章正文内容,下面案例可供参考`
一、基础知识
1. 1.1选择结构
1.1.1if语句
1.单行格式if语句
#include<iostream>
using namespace std;
/*if语句
作用:执行满足条件的语句
三种形式:单行格式if语句、多行格式if语句、多条件的if语句
*/
//选择结构-单行if语句
//格式:if(条件){条件满足执行的语句}
//输入一个分数,如果分数大于600分,视为考上一本大学,并在屏幕上打印
int main411() {
int score = 0;
cout << "请输入一个分数:" << endl;
cin >> score;
cout << "您输入的分数为:" <<score<< endl;
//if语句
//注意事项,在if判断句后面,不要加分号
if (score >= 600) {
cout << "我考上了一本大学!!!" << endl;
}
system("pause");
return 0;
}
2.多行格式if语句
#include<iostream>
using namespace std;
/*多行格式if语句
格式:if(条件){条件满足执行的语句}else{条件不满足执行的语句};
*/
int main412() {
int score = 0;
cout << "请输入您的分数:" << endl;
cin >> score;
cout << "您输入的分数为:" << score << endl;
if (score >= 600) {
cout << "恭喜您考上一本大学!!!" << endl;
}
else {
cout << "遗憾您未考上一本大学!!"<<endl;
};
system("pause");
return 0;
}
3.多条件的if语句
#include<iostream>
using namespace std;
/*多条件的if语句
格式:if(条件1){条件1满足执行的语句}else if(条件2){条件2满足执行的语句 }...else {所有条件都不满足执行的语句}
*/
int main413() {
int score = 0;
cout << "请输入您的分数:" << endl;
cin >> score;
cout << "您输入的分数为:" <<score<< endl;
if (score >= 600) {
cout << "一本的料子" << endl;
}
else if (score >= 500) {
cout << "二本的胚子" << endl;
}
else if (score >= 400) {
cout << "三本的胚子" << endl;
}
else if (score >= 300) {
cout << "上专科吧" << endl;
}
else {
cout << "明年再来!!" << endl;
}
system("pause");
return 0;
}
4.嵌套if语句
#include<iostream>
using namespace std;
/*嵌套if语句
作用:在if语句中,可以嵌套使用if语句,达到更精准的条件判断
案例需求:
提示用户输入一个高考考试分数,根据分数做如下判断
分数如果大于600分视为考上一本,大于500分考上二本,大于400考上三本,其余视为未考上本科;
在一本分数中,如果大于700分,考入北大,大于650分,考入清华,大于600考入人大。*/
int main414() {
int score = 0;
cout << "请输入您的分数:" << endl;
cin >> score;
cout << "您输入的分数为:" << score << endl;
if (score >= 600) {
cout << "一本大学" << endl;
if (score >= 700) {
cout << "北京大学" << endl;
}
else if (score >= 650) {
cout << "清华大学" << endl;
}
else {
cout << "人大" << endl;
}
}
else if (score >= 500) {
cout << "二本大学" << endl;
}
else if (score >= 400) {
cout << "三本大学" << endl;
}
else {
cout << "未考上本科" << endl;
}
system("pause");
return 0;
}
5.嵌套if练习
#include<iostream>
using namespace std;
/*练习案例: 三只小猪称体重
有三只小猪ABC,请分别输入三只小猪的体重,并且判断哪只小猪最重?*/
int main415() {
int a = 0;
int b = 0;
int c = 0;
cout << "请输入a猪的体重:" << endl;
cin >> a;
cout << "请输入b猪的体重:" << endl;
cin >> b;
cout << "请输入c猪的体重:" << endl;
cin >> c;
if (a >= b) {
if (a > c) {
cout << "a猪最重" << endl;
}
else {
cout << "c猪最重"<<endl;
};
}else {
if (b > c) {
cout << "b猪最重" << endl;
}
else {
cout << "c猪最重" << endl;
};
}
system("pause");
return 0;
}
- List item
6.项目运算符
```cpp
#include<iostream>
using namespace std;
/*三目运算符
作用:通过三目运算符实现简单的判断
语法:表达式1?表达式2:表达式3;
解释:如果表达式1的值为真,执行表达式2,并返回表达式2的结果。
如果表达式1的值为假,执行表达式3,并返回表达式3的结果
总结:和if语句比较,三目运算符优点是短小整洁,缺点是如果用嵌套,结构不清晰
*/
int main421() {
int a = 10;
int b = 20;
int c = 0;
c = a > b ? a : b;
cout << "c = " << c << endl;
//c++从三目运算符返回的是变量,可以继续赋值
(c = a > b ? a : b) = 100;
cout << "c = " << c << endl;
cout << "b = " << b << endl;
cout << "a = " << a << endl;
system("pause");
return 0;
}
7.switch语句
```cpp
#include<iostream>
using namespace std;
/*switch语句
作用:执行多条件分支语句
语法:switch(表达式)
{
case 结果1:执行语句;break;
case 结果2:执行语句;break;
。。。
default:执行语句;break;
}
*/
int main422() {
//请给电影评分
//10-9 经典
//8-7 非常好
//6-5 一般
//5分以下 烂片
int score = 0;
cout << "请给电影打分:" << endl;
cin >> score;
switch (score)
{
case 10:
case 9:
cout << "经典" << endl;
break;
case 8:
case 7:
cout << "非常好" << endl;
case 6:
case 5:
cout << "一般" << endl;
default:
cout << "烂片" << endl;
}
system("pause");
return 0;
}
//注意:switch语句表达式类型中只能是整型或者字符型
//注意:case里如果没有break,那么程序会一直向下执行
//总结:与if语句相比,对于多条件判断时,switch的结构清晰,执行效率高,缺点是switch不可以判断区间
8.while循环语句
#include<iostream>
using namespace std;
/*while循环语句
作用:满足循环条件,执行循环语句
语法:while(循环条件){循环语句}
解释:只要循环条件的结果为真,就执行循环语句
*/
int main501() {
int num = 0;
while (num < 30) {
cout << "num = " << num << endl;
num++;//注意:在执行循环语句时候,程序必须提供跳出循环的出口,否则出现死循环
}
system("pause");
return 0;
}
9.while循环练习案例
#include<iostream>
#include<ctime>
using namespace std;
/*案例描述:系统随机生成一个1到100之间的数字,玩家进行猜测,
如果猜错,提示玩家数字过大或过小,如果猜对恭喜玩家胜利,并且退出游戏。*/
//暂时先这样吧,循环还没搭建起来
int main502() {
//添加随机数种子,作用是利用当前系统时间生成随机数,防止每次随机数都一样
srand((unsigned int)time(NULL));
int num = rand()%100 + 1;
int answer = 0;
cout << "请输入您的答案:" << endl;
cin >> answer;
while (1)//while(1)其中1代表一个常量表达式,他永远不会等于0。
//所以,循环会一直执行下去。除非你设置break等类似的跳出循环语句循环才会中止
{
if (answer == num) {
cout << "玩家胜利 游戏结束" << endl;
break;
}
else if (answer > num) {
cout << "提示:数字过大,请重新输入" << endl;
break;
}
else {
cout << "提示:数字过小,请重新输入" << endl;
break;
}
}
system("pause");
return 0;
}
10.do…while循环语句
#include<iostream>
using namespace std;
/*do...while循环语句
作用:满足循环条件,执行循环语句
语法:do{循环语句}while(循环条件);
注意:与while的区别在于,do...while会先执行一次循环语句,再判断循环条件*/
int main503() {
int num = 0;
do {
cout << "num = " <<num<<endl;
num++;
} while (num < 10);
system("pause");
return 0;
}
11.水仙花数
#include<iostream>
using namespace std;
/*案例描述:水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身
例如:1^3 + 5^3+ 3^3 = 153
请利用do…while语句,求出所有3位数中的水仙花数*/
int main504(){
int num = 100;
do {
/* int a = num / 100;
int b = (num / 10) % 10;
int c = num % 10;*/
int a = 0; //个位数字
int b = 0; //十位数字
int c = 0; //百位数字
a = num % 10; //获取各位数字
b = num / 10 % 10; //获取十位数字
c = num / 100; //获取百位数字
if (a*a*a + b*b*b + c*c*c*c == num) {
cout << num << endl;
}
num++;
} while (num < 1000);
system("pause");
return 0;
}
12.for循环语句
#include<iostream>
using namespace std;
/*for循环语句
作用:满足循环条件,执行循环语句
语法:for(起始表达式;条件表达式;末尾循环体){循环语句;}
详解:首先执行一次起始表达式,接着对条件表达式进行判断,决定是否之心循环语句,最后回到末尾循环体*/
int main505() {
for (int i = 0; i < 10; i++) {
cout << "i = " << i << endl;
}
system("pause");
return 0;
}
//for循环中的表达式,要用分号进行分隔
13.练习案例敲桌子
#include<iostream>
using namespace std;
/*案例描述:从1开始数到数字100, 如果数字个位含有7,
或者数字十位含有7,或者该数字是7的倍数,我们打印敲桌子,其余数字直接打印输出。*/
int main506() {
for (int i = 0;i < 100; i++) {
if (i / 7 == 0 || i / 10 == 7 || i % 10 == 7) {
cout << "敲桌子" << endl;
}
else {
cout << i << endl;
}
}
system("pause");
return 0;
}
14.嵌套循环
#include<iostream>
using namespace std;
/*嵌套循环
作用:再循环体中再嵌套一层循环,解决一些实际问题*/
int main507() {
//d打印10*10的星号
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++) {
cout <<" * "<< " ";
}
cout << "\n"<<endl;
}
system("pause");
return 0;
}
15.乘法口诀
#include<iostream>
using namespace std;
/*练习案例:乘法口诀表
案例描述:利用嵌套循环,实现九九乘法表*/
int main508() {
for (int i = 1; i < 10; i++) {
for (int j = 0; j <= i; j++) {
cout << i << "*" << j <<"="<< i*j << endl;
}
cout << endl;
}
system("pause");
return 0;
}
16.跳转break语句
#include<iostream>
using namespace std;
/*break语句
作用:用于跳出选择结构或者循环结构
break使用的时机
1.出现再switch语句中,作用是终止case并跳出swit
2.出现在循环语句中,作用是跳出当前的循环语句
3.出现在嵌套循环中,跳出最近的内层循环语句*/
int main601() {
//break在switch中
cout << "请选择游戏难度" << endl;
cout << "1.普通" << endl;
cout << "2.中等" << endl;
cout << "3.困难" << endl;
int num = 0;
cin >> num;
switch (num) {
case 1:
cout << "您选择的是普通难度!" << endl;
break;
case 2:
cout << "您选择的是中等难度!" << endl;
break;
case 3:
cout << "您选择的是困难难度!" << endl;
break;
}
system("pause");
return 0;
}
17.break在循环语句中
#include<iostream>
using namespace std;
//break语句在循环语句中
int main602() {
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
cout << i << endl;
}
system("pause");
return 0;
}
18.break在嵌套循环中
#include<iostream>
using namespace std;
//在嵌套循环语句中使用break语句,推出内层循环
int main603() {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (j == 5) {
break;
}
cout << "*" << " ";
}
cout << endl;
}
system("pause");
return 0;
}
19.continue语句
#include<iostream>
using namespace std;
/*continue语句
在循环语句中,跳过本次循环尚未执行的语句,继续执行下一次循环
注意:continue并没有是整个循环终止,而break会跳出循环*/
int main611() {
for (int i = 0; i < 100; i++) {
if (i % 2 == 0) {
continue;
}
cout << i << endl;
}
system("pause");
return 0;
}
20.goto语句
#include<iostream>
using namespace std;
/*goto语句
作用:可以无条件跳转语句
语法:goto 标记;标记:
解释:如果标记的名称存在,执行到goto语句时,会跳转到标记的位置
*/
//在程序中不建议使用goto语句,以免造成程序流程混乱
int main612() {
cout << "1" << endl;
goto FLAG;
cout << "2" << endl;
cout << "3" << endl;
FLAG:
cout << "4" << endl;
system("pause");
return 0;
}
21.一维数组定义方式
#include<iostream>
using namespace std;
/*数组
所谓数组,就是一个集合,里面存放了相同类型的数据元素
特点1:数组中的每个数据元素都是相同的数据类型
特点2:数组是由连续的内存位置组成的
*/
/*一维数组
一维数组定义方式
1.数据类型 数组名 [数组长度];
2.数据类型 数组名 [数组长度] = {值1,值2 ...};
3.数据类型 数组名 [] = {值1,值2,...};
*/
int main701() {
//定义方式一
//数组类型 数组名[数组长度]
int score[10];
score[0] = 100;
score[1] = 95;
score[2] = 90;
cout << score[0] << endl;
cout << score[1] << endl;
cout << score[2] << endl;
//第二种定义方式
int score1[10] = { 100,95,90,85,80,80,75,70,60,55 };
cout << "第二种定义输出:" << endl;
//利用for循环输出
for (int i = 0; i < 9; i++) {
cout << score1[i] << endl;
}
//第三种定义方式
int score2[] = { 199,55,8,8,3,8,6,4,64,100 };
cout << "第三种定义输出:" << endl;
for (int i = 0; i < 9; i++) {
cout << score2[i] << endl;
}
system("pause");
return 0;
}
//注意:数组名的命名规范与变量名命名规范一致,不要和变量重名
//总结:数组中下标从0开始索引
22.一维数组数组名
#include<iostream>
using namespace std;
/*一维数组名的用途:
1.可以统计整个数组在内存中的长度
2.可以获取数组在内存中的首地址
*/
int main702() {
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
//统计长度
cout << "整个数组所占内存空间为:" << sizeof(arr) << endl;
cout << "每个元素所占内存空间为:" << sizeof(arr[5]) << endl;
cout << "数组中元素的个数为:" << sizeof(arr) / sizeof(arr[0]) << endl;
//获取地址
cout << "数组首地址为:" << (int)arr << endl;
cout << "数组中第一个元素地址为:" << (int)&arr[0] << endl;
cout << "数组中最后一个元素地址为:" << (int)&arr[9] << endl;
//arr = 101 错误,数组名是常量,不可以赋值
system("pause");
return 0;
}
//总结:直接打印数组名,可以查看数组所占内存的大小
//总结:对数组名进行sizeof,可以获取整个数组所占内存空间大小
23.练习案例5只小猪称体重
#include<iostream>
using namespace std;
/*案例描述:
在一个数组中记录了五只小猪的体重,如:int arr[5] = {300,350,200,400,250};
找出并打印最重的小猪体重*/
int main703() {
int arr[5] = { 300,350,200,250,400 };
int max = 0;
for (int i = 0; i < 5;i++) {
if (arr[i] > max) {
max = arr[i];
cout << arr[i] << endl;
}
}
cout << "最重为:" << max << endl;
system("pause");
return 0;
}
24练习案例数组元素逆置
#include<iostream>
using namespace std;
/*练习案例2:数组元素逆置
案例描述:请声明一个5个元素的数组,并且将元素逆置.
(如原数组元素为:1,3,2,5,4;逆置后输出结果为:4,5,2,3,1);
*/
int main704() {
int arr[5] = { 1,2,3,4,5 };
int length = sizeof(arr) / sizeof(arr[0]);
cout << "length:" <<length<< endl;
cout << "数组逆置前:";
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
cout <<"\n"<< "数组逆置后:";
int score[5];
for (int i = 0; i < 5; i++) {
score[i] = arr[length -1- i];
cout << score[i] << " ";
}
system("pause");
return 0;
}
25.冒泡排序
#include<iostream>
using namespace std;
/*冒泡排序
作用: 最常用的排序算法,对数组内元素进行排序
比较相邻的元素。如果第一个比第二个大,就交换他们两个。
对每一对相邻元素做同样的工作,执行完毕后,找到第一个最大值。
重复以上的步骤,每次比较次数-1,直到不需要比较
*/
int main705() {
int arr[9] = { 4,2,8,0,5,7,3,1,9 };
for (int i = 0; i < 9 - 1; i++) {
for (int j = 0; j < 9 - 1 - i ; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i < 9; i++) {
cout << arr[i] << ",";
}
system("pause");
return 0;
}
26.二维数组定义方式
#include<iostream>
using namespace std;
/*二维数组
二维数组就是在一维数组上,多加一个维度
二维数组定义的四种方式:
1.数据类型 数组名[行数][列数];
2.数据类型 数组名[行数][列数] = {{数据1,数据2}{数据3,数据4}};
3.数据类型 数组名[行数][列数] = {数据1,数据2,数据3,数据4};
4.数据类型 数组名[][列数] = {数据1,数据2,数据3,数据4};如果初始化了数据,可以省略行数
建议:利用第二种更加直观,提高代码的可读性
*/
int main711() {
//第一种定义
int arr[2][3];
arr[0][0] = 1;
arr[0][1] = 2;
arr[0][2] = 3;
arr[1][0] = 4;
arr[1][1] = 5;
arr[1][2] = 6;
cout << "第一种定义:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << arr[i][j] << " ";
if (j == 2) {
cout << endl;
}
}
}
cout << endl;
//第二种定义
int arr1[2][3] = {
{1,2,3},
{4,5,6}
};
cout << "第二种定义:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << arr1[i][j] << " ";
if (j == 2) {
cout << endl;
}
}
}
cout << endl;
//第三种定义
int arr2[2][3] = { 1,2,3,4,5,6 };
cout << "第三种定义:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << arr2[i][j] << " ";
if (j == 2) {
cout << endl;
}
}
}
cout << endl;
//第四种定义
int arr3[][3] = { 1,2,3,4,5,6 };
cout << "第四种定义:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << arr3[i][j] << " ";
if (j == 2) {
cout << endl;
}
}
}
cout << endl;
system("pause");
return 0;
}
27.二维数组数组名
#include<iostream>
using namespace std;
/*二维数组数组名
1.查看二维数组所占内存空间
2.获取二维数组首地址*/
int main712() {
int arr[2][3] = {
{1,2,3},
{4,5,6}
};
cout << "二维数组所占内存大小:" << sizeof(arr) << endl;
cout << "二维数组一行所占内存大小:" << sizeof(arr[0]) << endl;
//cout << "二维数组一列所占内存大小:" << sizeof[][1] << endl; 疑问,怎么计算一行所占内存大小??
//二维数组应该是按行来存放的
cout << "数组中元素所占内存大小:" <<sizeof(arr[1][1]) << endl;
cout << "二维数组首地址:" << arr << endl;
cout << "二维数组第一行地址:" << arr[0] << endl;
cout << "二维数组第4个元素地址" << &arr[1][0] << endl;
system("pause");
return 0;
}
28.二维数组应用案例
#include<iostream>
#include<string>
using namespace std;
/*二维数组应用案例
考试成绩统计:三名同学,三门课程,输出他们总成绩
*/
int main713() {
int score[3][3] = {
{100,100,100},
{90,50,100},
{60,70,80}
};
string name[3] = { "张三","李四","王五" };
for (int i = 0; i < 3; i++) {
int sum = 0;
for (int j = 0; j < 3; j++) {
sum += score[i][j];
}
cout << name[i] << ":" ;
cout << sum << endl;
}
system("pause");
return 0;
}
29.函数地定义
#include<iostream>
using namespace std;
/*函数
作用:将一段经常使用的代码封装起来,减少重复代码
一个较大的程序,一般分为若干个程序块,每个模块实现特定的功能*/
/*函数的定义一般主要有5个步骤:
1.返回值类型:一个函数可以返回一个值,在函数定义中
2.函数名:给函数起个名称
3.参数表列:使用该函数时,传入的数据
4.函数体语句:花括号内的代码,函数内需要执行的语句
5.return表达式:和返回值类型挂钩,函数执行完后,返回相应的数据
语法:返回值类型 函数名(参数列表)
{ 函数体语句
return表达式
}
*/
//定义两个数的和
/*函数调用
功能:使用定义好的函数
语法:函数名(参数)
*/
int add(int num1, int num2) {
int sum = num1 + num2;
return sum;
}
//函数定义里小括号内称为形参,函数调用时传入的参数称为实参
int main801() {
int a = 10;
int b = 20;
int sum = add(a, b);
cout << sum << endl;
a = 100;
b = 200;
sum = (a, b);
cout << sum << endl;
system("pause");
return 0;
}
30.值传递
#include<iostream>
using namespace std;
/*值传递
所谓值传递,就是函数调用时实参将数值传递给形参
值传递时,如果形参发生改变,并不会影响实参*/
//如果函数没有返回值,那么应声明为void类型
void swap(int num1, int num2) {
cout << "交换前:" << endl;
cout << "num1 = " << num1 << endl;
cout << "num2 = " << num2 << endl;
int temp = num1;
num1 = num2;
num2 = temp;
cout << "交换后:" << endl;
cout << "num1 = " << num1 << endl;
cout << "num2 =" << num2 << endl;
//当函数声明时,不需要返回值,可以不写return
}
int main802() {
int a = 10;
int b = 20;
swap(a, b);
cout << "main中a = " << a << endl;
cout << "main中b = " << b << endl;
system("pause");
return 0;
}
31.函数的常见样式
#include<iostream>
using namespace std;
/*常见的函数样式有4种
1.无参无返
2.有参无返
3.无参有返
4.有参有返
*/
//1.无参无返
void test01() {
//void a = 0;//无类型不可以创建变量,原因无法分配内存
cout << " this is test01" << endl;
//test01()函数调用
}
//2.有参无返
void test02(int a) {
cout << "this is test02" << endl;
cout << a << endl;
}
//3.有参无返
int test03() {
cout << "this is test03" << endl;
return 10;
}
//4.有参有返
int test04(int a, int b) {
cout << "this is test04" << endl;
int sum = a + b;
return sum;
}
int main803() {
system("pause");
return 0;
}
32.函数的声明
#include<iostream>
using namespace std;
/*函数的声明
作用:告诉函数编译器函数的名称以及如何调用函数,函数的实际主体可以单独定义
函数的声明可以多次,但是函数的定义只有一次*/
int max(int a, int b);
int max(int a, int b);
int max(int a, int b) {
return a > b ? a : b;
}
int main804() {
int a = 10;
int b = 20;
cout << max(a, b) << endl;
system("pause");
return 0;
}
33.函数的分文件编写
#include<iostream>
#include"swap.h"
using namespace std;
/*函数的分文件编写
作用:让代码结构更加清晰
函数文件编写一共有4个步骤
1.创建后缀名为.h的头文件
2.创建后缀名为.cpp的源文件
3.在头文件中写函数的声明
4.在源文件中写函数的定义
*/
void swap1(int a, int b) {
int temp = a;
a = b;
b = temp;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
int main805() {
int a = 10;
int b = 20;
swap1(a, b);
system("pause");
return 0;
}
34.指针变量的定义和使用
#include<iostream>
using namespace std;
/*指针
作用:通过指针间接访问内存
1.内存编号是从0开始记录的,一般用十六进制数字表示
2.可以利用指针变量保存地址*/
/*指针变量的定义和使用
定义语法:数据类型*变量名*/
int main901() {
int a = 10;//整型变量定义
int * p;//指针定义
//指针变量赋值
p = &a;//指针指向变量a的位置
cout << &a << endl;//打印数据a的地址
cout << p << endl;//打印指针变量p
//指针的使用
//通过*,操作指针变量指向的内存
cout << "*p = " << *p << endl;
system("pause");
return 0;
}
//指针变量和普通变量的区别
//普通变量存放的是数据,指针变量存放的是地址
//指针变量可以通过“*”操作符,操作指针变量指向的内存空间,这个过程称为解引用
//总结1:我们可以通过 &符号 获取变量的地址
//总结2:利用指针可以记录地址
//总结3:对指针变量解引用,可以操作指针指向的内存
35.指针所占内存空间
#include<iostream>
using namespace std;
//所有指针在32位操作系统下是4个字节
int main902() {
int a = 10;
int * p;
p = &a;
cout << *p << endl;
cout << sizeof(p) << endl;
cout << sizeof(char *) << endl;
cout << sizeof(float *) << endl;
cout << sizeof(double *) << endl;
system("pause");
return 0;
}
36.空指针和野指针
#include<iostream>
using namespace std;
/*空指针
指针变量指向内存中编号为0的空间
用途:初始化指针变量
注意:空指针指向的内存是不可访问的 */
int main903() {
int *p = NULL;
//访问空指针报错
//内存编号为0`255为系统占用内存,不允许用户访问
//cout << *p << endl;
/*野指针
指针变量指向非法的内存空间
*/
//指针变量指向内存地址编号为0x1100的空间
int *o = (int *)0x1100;
//访问野指针报错
//cout << *o << endl;
system("pause");
return 0;
}
//总结:空指针和野指针都不是我们申请的空间,因此不要访问
37.const修饰指针
#include<iostream>
using namespace std;
/*const修饰指针有3种情况,指定某个值保持不变
1.const修饰指针-常量指针
2.const修饰常量-指针常量
3.const及修饰指针,又修饰常量
*/
int main904() {
int a = 10;
int b = 20;
//const修饰的是指针,指针指向可以改,指针指向的量不可以更改
const int*p1 = &a;
p1 = &b;
//*p1 =100; 错误
//const修饰的是常量,指针指向不可以改,指针指向的值可以更改
int * const p2 = &a;
//p2 = &b; 报错
*p2 = 100;
//const既修饰指针又修饰常量
const int *const p3 = &a;
//p3 = &b; 报错
//*p3 = 100; 报错
system("pause");
return 0;
}
//技巧:看const右侧紧跟着的是指针还是常量,是指针就是常量指针,即指针本身是常量;是常量就是指针常量,即指针指向为常量。
38.指针和数组
#include<iostream>
using namespace std;
/*指针和数组
作用:利用指针访问数组种元素*/
int main905() {
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int *p = arr;//指向数组的指针
cout << "第一个元素;" << arr[0] << endl;
cout << "指针访问第一个元素:" << *p << endl;
for (int i = 0; i < 10; i++) {
//利用指针遍历数组
cout << *p << endl;
p++;
}
system("pause");
return 0;
}
39.指针和函数
#include<iostream>
using namespace std;
/*指针和函数
作用:利用指针作函数参数,可以修改实参的值
*/
//值传递
void swap2(int a, int b) {
int temp = a;
a = b;
b = temp;
}
//地址传递
void swap1(int *p1, int *p2) {
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
int main906() {
int a = 10;
int b = 20;
swap2(a, b);
swap1(&a, &b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
system("pause");
return 0;
}
//总结:如果不想修改实参,就用值传递,如果想修改实参,就用地址传递
二、问题
1.安装与链接问题
更多推荐
所有评论(0)