是谁说回老家学习结果摆烂了两天,是我,Π—Π! Π—Π!!
一,数组——同C
1.1一维数组
1.0 相同类型,连续内存,
1.1 定义格式:数据类型 数组名【长度】;数组类型 数组名【长度】={1,2,3,……};数组类型 数组名【】={1,2,3,……};
1.2 遍历数组,初始化,下标【0-N】
1.3 数组名:数组内存长度,数组首地址
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 1,2,4,5,6,7,8 };
for (int i = 0; i < 7; i++)
{
cout <<i<<" " << arr[i] << "\t";//arr[8]未定义?强行输出会输出某超小负数
cout << "arr[]——" << &arr[i] << endl;
cout << "arr[]——" << (int)&arr[i] << endl;
}
cout << endl;
cout << "数组大小" << sizeof(arr) << endl;
cout << "数组每个元素大小" << sizeof(arr[1]) << endl;
cout << "数组元素个数" << sizeof(arr)/sizeof(arr[1]) << endl;
//取地址
cout << "arr——" << arr << endl;
cout << "arr——" << (int)arr << endl;//十六进制地址强转为十进制
return 0;
system("pause");
}
1.1.1 冒泡排序
冒泡排序是两轮循环,对比轮数==数组大小—1;每轮对比次数==数组大小—当前轮数—1;
for (int i = 0; i < 9-1; i++)//错误,只走了一层循环
{
if (a[i] > a[i + 1])
{
a[i] +=a[i + 1];
a[i+1] = a[i] - a[i+1];
a[i] = a[i] - a[i + 1];
if (i == sizeof(a) / sizeof(a[0]) - 1 - i)
{
break;
}
}
}
#include <iostream>//正确
using namespace std;
int main()
{
int a[9] = { 4,2,8,0,5,7,1,3,9 };
//cout << sizeof(a) / sizeof(a[0]) << endl;
for (int i = 0; i < 9-1; i++)
{
for (int j = 0; j < 9 - i - 1; j++)
{
if (a[j] > a[j + 1])
{
a[j] += a[j + 1];
a[j + 1] = a[j] - a[j + 1];
a[j] -= a[j + 1];
}
}
}
for (int t = 0; t < 9; t++)
{
cout << a[t] << endl;
}
return 0;
system("pause");
}
1.2 二维数组
同C ,数组定义:四种,行可以省,列不能
#include <iostream>
using namespace std;
int main()
{
int A[3][5];
int B[2][3] = { {1,3,3},{2,2,2} };
int C[2][2] = { 2,33,4,3, };
int D[][3] = { 2,33,4,3 };
cout <<"数组B内存空间\t"<< sizeof(B) << endl;
cout << "数组B一个元素占内存空间\t"<<sizeof(B[0][0]) << endl;
cout << "数组B一行占内存空间\t" << sizeof(B[0]) << endl;
cout << "数组B行数\t" << sizeof(B) / sizeof(B[0]) << endl;
cout << "数组B列数\t" << sizeof(B[0]) / sizeof(B[0][0]) << endl;
cout << B << endl;
cout << (int)B << endl;
cout << &B[0][0] << endl;
cout << (int)&B[0][0] << endl;
cout << B[0] << endl;
cout << (int)B[0] << endl;
cout << B[1] << endl;
cout << (int)B[1] << endl;
cout << "每一个元素的地址" << endl;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << &B[i][j] << endl;
cout << (int)&B[i][j] << endl;
}
}
return 0;
system("pause");
}
1.2.1统计成绩
#include <iostream>
#include<string>
using namespace std;
int main()
{
int scores[3][3] = { {100,100,100},{90,50,100},{60,70,80} };
//int SCORES[3] = { 0 };
string names[3] = { "张三","李四","王五" };
for (int i = 0; i < 3; i++)
{
int sum = 0;
cout << names[i];
for (int y = 0; y < 3; y++)
{
//SCORES[i] += scores[i][y];
sum+= scores[i][y];
}
cout << sum << endl;
}
system("pause");
}
二,函数——同C
返回值类型,函数名,参数列表,函数体语句,RETURN表达式
实参,形参,值传递时,形参的改变不会影响实参
声明(可以多次),定义(一次),同C
分文件编写:.H头文件,写函数声明,.CPP源文件,写函数定义
#include<iostream>//头文件
using namespace std;
int swap(int num1, int num2);
#include<iostream>//函数文件
#include "swap.h"
using namespace std;
int swap(int num1, int num2)
{
cout << "函数内交换前num1=" << num1 << endl;
cout << "函数内交换前num2=" << num2 << endl;
num1 += num2;
num2 = num1 - num2;
num1 = num1 - num2;
cout << "函数内交换后num1=" << num1 << endl;
cout << "函数内交换后num2=" << num2 << endl;
return num1, num2;
}
#include<iostream>//文件
#include "swap.h"
using namespace std;
int main()
{
int a = 90;
int b = 45;
cout << "函数外交换前a=" << a << endl;
cout << "函数外交换前b=" << b << endl;
swap(a, b);
cout << "函数外交换后a=" << a << endl;
cout << "函数外交换后b=" << b << endl;
return 0;
system("pause");
}
三,指针——同C
数据类型 * 指针变量名,int * p=&a;
指针所占空间:32位4,64位8
#include<iostream>
using namespace std;
int main()
{
int a = 10;
int* p = &a;
cout << *p << endl;
cout << p << endl;
cout << sizeof(p) << endl;
cout << sizeof(char*) << endl;
cout << sizeof(float*) << endl;
cout << sizeof(double*) << endl;
return 0;
system("pause");
}
3.1 空指针和野指针
INT*P=NULL,不可访问,常用于初始化,0-255的内存编号系统占用
野指针,没有申请访问权限,不可访问
#include<iostream>
using namespace std;
int main()
{
int* p = NULL;
//cout << *p << endl; 不可访问
cout << p << endl;
int* pp = (int*)0x1100;
//cout << *p << endl;没有申请权限
cout << pp << endl;
return 0;
system("pause");
}
3.2 常量指针&指针常量
const int*p,常量指针,指针指向的值不能修改,指针的指向可以该,就是这类指针只能访问,不能修改
int *const p,指针常量,指向不可修改,指向的数据可以访问和修改【即P的地址的数值不可修改】
const int * const p,指向和数据都不可以修改,只能访问
const后面紧跟的是指针还是常量,指针-常量指针,常量-指针常量
#include<iostream>
using namespace std;
int main()
{
int a = 90;
int b = 100;
const int* p = &a;
cout << p << endl;
cout << *p << endl;
a = 111;
cout << p << endl;
cout << *p << endl;
//*p = 222;不可以
p = &b;
cout << p << endl;
cout << *p << endl;
cout << endl;
int * const pp = &a;
cout << pp << endl;
cout << *pp << endl;
//pp = &b;不可以,指向不可修改
*pp = 77;
cout << pp << endl;
cout << *pp << endl;
const int* const ppp = &a;
cout << ppp << endl;
cout << *ppp << endl;
//ppp = &b;
//*ppp = 44;
//指向不可以修改,数据只能访问不可以修改
return 0;
system("pause");
}
3.3 指针和数组
#include<iostream>
using namespace std;
int main()
{
int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
int* p = a;
cout << "第1个数组元素访问——" << *p << endl;
p++;
cout << "第2个数组元素访问——" << *p << endl;
p = a;//遍历数组1
for (int i = 0; i < 10; i++)
{
//cout << a[i] << " ";
cout << *p << " ";
p++;
}
cout << endl;
p = a;//遍历数组2
for (int i = 0; i < 10; i++)
{
cout << p[i] << " ";
}
cout << endl;
return 0;
system("pause");
}
3.4 指针和函数——地址传递(可以改变实参)
#include<iostream>//头文件
using namespace std;
int swap(int *num1, int *num2);
#include<iostream>//函数文件
#include "swap.h"
using namespace std;
int swap(int *num1, int *num2)
{
cout << "函数内交换前*num1=" << *num1 << endl;
cout << "函数内交换前*num2=" << *num2 << endl;
*num1 += *num2;
*num2 = *num1 - *num2;
*num1 = *num1 - *num2;
cout << "函数内交换后*num1=" << *num1 << endl;
cout << "函数内交换后*num2=" << *num2 << endl;
return *num1, *num2;
}
#include<iostream>//文件
#include "swap.h"
using namespace std;
int main()
{
int a = 90;
int b = 45;
cout << "函数外交换前a=" << a << endl;
cout << "函数外交换前b=" << b << endl;
swap(&a, &b);
cout << "函数外交换后a=" << a << endl;
cout << "函数外交换后b=" << b << endl;
return 0;
system("pause");
}
3.5 指针,数组,函数——例:冒泡排序函数版
#include<iostream>//头文件
using namespace std;
void bubblesort(int* p,int n);
#include<iostream>//函数文件
#include "bubblesort.h"
using namespace std;
void bubblesort(int* p,int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int y = 0; y < n - 1 - i; y++)
{
if (p[y] > p[y + 1])
{
p[y] += p[y + 1];
p[y + 1] = p[y] - p[y + 1];
p[y] -= p[y + 1];
}
}
}
}
#include<iostream>//文件
#include "bubblesort.h"
using namespace std;
int main()
{
int a[9] = { 4,2,8,0,5,7,1,3,9 };
bubblesort(a, 9);
for (int i = 0; i < 9; i++)
{
cout << a[i] << endl;
}
return 0;
system("pause");
}
学了才知道和C一样,咱就是说,咱不敢跳啊
不过二倍速很快的