首页 > 编程语言 >20240408,C++数组,函数,指针

20240408,C++数组,函数,指针

时间:2024-04-08 23:38:37浏览次数:22  
标签:std int namespace C++ 20240408 using include 指针

是谁说回老家学习结果摆烂了两天,是我,Π—Π! Π—Π!! 

一,数组——同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一样,咱就是说,咱不敢跳啊
不过二倍速很快的

标签:std,int,namespace,C++,20240408,using,include,指针
From: https://blog.csdn.net/qq_51583806/article/details/137437168

相关文章

  • 【C++对C语言的拓展】引用--3
    1.8指针引用#include <iostream>usingnamespace std; struct Teacher { char name[64]; int age ; }; //在被调⽤用函数获取资源int getTeacher(Teacher **p) {     Teacher *tmp = NULL; if (p == NULL)......
  • C++奇迹之旅:我与类和对象相遇
    文章目录......
  • C++ 入门一
    C++入门一1.C++关键字2.命名空间2.1命名空间定义2.2命名空间使用3.C++输入&输出C++是一种高级编程语言,由BjarneStroustrup在20世纪80年代初期在贝尔实验室开发。它是对C语言的扩展,添加了面向对象编程的特性,如类和对象、继承、多态和封装。C++支持多种编程范式,......
  • 【C语言】深入了解指针(2),进来小白,出去大佬!
    目录1,const修饰指针1.1,const修饰变量1.2,const修饰指针变量2,指针运算2.1,指针+-整数 2.2,指针-指针2.3,指针的关系运算3,野指针3.1,野指针成因1, 指针未初始化2.,指针越界访问3,指针指向的空间释放3.2,如何规避野指针1,指针初始化2,⼩⼼指针越界3,指针变量不再使⽤时,及时......
  • 【简单讲解下C++max函数的使用】
    ......
  • C++中lambda表达式介绍
    c++在c++11标准中引入了lambda表达式,一般用于定义匿名函数,使得代码更加灵活简洁。lambda表达式与普通函数类似,也有参数列表、返回值类型和函数体,只是它的定义方式更简洁,并且可以在函数内部定义。什么是Lambda表达式最常见的lambda的表达式写法如下autoplus=[](intv1,int......
  • 【C++11】C++11深度解剖
    >作者:დ旧言~>座右铭:松树千年终是朽,槿花一日自为荣。>目标:了解在C++11相关知识>毒鸡汤:苦尽甘来的那一天,山河星月都作贺礼。>专栏选自:C嘎嘎进阶>望小伙伴们点赞......
  • C语言——指针(1)
    一、什么是指针?指针(Pointer)是编程语言中一种重要的数据类型它,它用于存储变量的内存地址。换句话说,指针指向内存中的某个位置,其内容就是地址,这个地址指向存储的数据,程序可以直接访问和操作内存中的数据,这为动态内存分配、函数传参、数组访问等操作提供了便利和灵活性。总的来说......
  • C++ 遍历queue
    在C++中,std::queue是一个遵循先进先出(FIFO)原则的容器。由于std::queue不提供直接访问容器内部元素的方法,因此不能直接遍历。但是,您可以使用一个临时队列来遍历。以下是如何做到这一点的示例代码: #include<iostream>#include<queue>intmain(){std::queue<i......
  • C++ [NOIP2009 普及组] 分数线划定
    文章目录一、题目描述[NOIP2009普及组]分数线划定题目描述输入格式输出格式样例#1样例输入#1样例输出#1提示二、参考代码一、题目描述[NOIP2009普及组]分数线划定题目描述世博会志愿者的选拔工作正在A市如火如荼的进行。为了选拔最合适的人才,A市对所......