c++2

c到c++2

作业:

实现函数fun,调用这个fun函数能完成对int+int或者float+float的和,(必须传参是引用传参);

#include<iostream>
using namespace std;

int fun(int &a, int &b)
{
	return a + b;
}

float fun(float &a, float &b)
{
	return a + b;
}

int main()
{
	int a = 1, b = 2;
	float x = 1.1f, y = 2.3f;
	cout << fun(a, b) << endl;
	cout << fun(x, y) << endl;
	system("pause");
	return 0;
}

一、new和delete

1.知识点

在c++中用来申请堆区的内容空间,new等价与c语言中的malloc,delete等价于C语言中的free

(c++兼容c)

double* p=(double*)malloc(sizeof(double)*10);
*p=5;//访问的是8个字节

对于指针解引用访问的是 指针的类型(不加*的部分)大小的字节数

指针+n,移动的字节数是 指针的类型(不加*的部分)大小的字节数

    int a = 0;
	char* p = (char*)&a;
	*p = 'a';
	*(p + 1) = 'b';
	*(p + 2) = 'c';
	//*(p + 3) = '';
	printf("%s
", (char*)&a)

new delete

int* p=new int;
int** p1=new int*;
int*** p2=new int**;

2.new使用

//1.申请单个内存
int* p=new int;
//2.申请的单个内存初始化
int* p1=new int(100);
//3.申请多个连续的空间
int* p2=new int[10];

3.new的特性

1.new是运算符

2.new可以分配两种内存,一种是单个内存,一种是连续的内存

3.new只有分配单个内存的时候可以同时给该内存复赋值,分配连续多个内存是无法同时赋值的

4.如果要给连续的内存同时赋值用memset赋值,赋值为0

4.delete的使用

//1.释放单个内存
delete p;
p=NULL;
//2.释放多个内存
delete []p;
p=NULL;

5.delete的特性

1.delete是运算符

2.delete可以释放两种内存,一种是单个内存,一种是连续内存

3.delete释放内存时,指针必须时指向内存首地址

4.释放完内存之后要记得给指针赋值NULL操作,否者会产生野指针

6.动态内存操作中常见的问题

//1.没有给指针分配内存,就对改指针解引用赋值
int* p;
*p=10;
//2.给指针分配内存成功,但是没有初始化就使用了
int* p=new int;
printf("%d
",*p);
//3.内存分配成功,也初始化了,但是操作越界
int* p=new int[5];
*(p+7)=123;
/*
这种操作通常情况下应该都可以正常运行,但是会有很大的问题,因为你不知道越界的地方是否有在使用的数据,如果有那么这种越界操作就会改变那个地方的数据,然后出现一些问题
*/
    

4.忘记释放内存,造成内存泄漏

//5.释放内存后继续使用
int* p=new int;
*p=10;
delete p;
*p=20;//释放了内存,就不能继续使用了
    

动态数组

#include <stdio.h>
#include<string.h>
int* p = NULL;//用来申请内存的指针
int len = 0;//元素个数 , 插入位置
int maxSize = 0;//最大容量

void insert(int data) //尾插法插入元素
{
	if (len >= maxSize)//元素个数>=最大容量,说明放满了,需要扩容
	{
		maxSize = maxSize + ((maxSize >> 1) > 1 ? (maxSize >> 1) : 1);
		//右移运算符--减一半 1右移--0 2右移--1 3右移--1 4右移--2 5右移--2 6右移--3
		//左移运算符--加一半
		//1 2 3 4 6 9 13 19 
		printf("<%d>", maxSize);
		int* temp = new int[maxSize];
		memcpy(temp, p, sizeof(int)*len);//将原来p中的内存有多少就拷贝多少到temp中
		if (p != NULL)//如果指针里面有内容,将其释放
		{
			delete[]p;
		}
		p = temp;//p指向新的内存(先释放再指)
	}
	p[len] = data;//尾插
	len++;//元素+++1

}

void insert(int index, int data)
{
	if (index<0 || index>len)
	{
		return;
	}
	if (len >= maxSize)
	{
		maxSize = maxSize + ((maxSize >> 1) > 1 ? (maxSize >> 1) : 1);
		int* temp = new int[maxSize];
		memcpy(temp, p, sizeof(int)*len);
		if (p != NULL)
		{
			delete[]p;
		}
		p = temp;//p指向temp,也得到了里面的内容
	}
	for (int i = len - 1; i >= index; i--)
	{
		p[i + 1] = p[i];
	}
	p[index] = data;
	len++;
}


void delete_last()//删除最后一个元素,只是从我们的视觉里被删除了,实际上还是存在于计算机内存中
{
	len--;
}

void dele(int index)//元素位置
{
	if (index<0 || index>len)
	{
		return;
	}
	for (int i = index; i < len; i++)
	{
		p[i] = p[i + 1];
	}
	len--;
}

//修改元素
void change(int index, int data)
{
	if (index<0 || index>len)
	{
		return;
	}
	for (int i = 0; i < len; i++)
	{
		if (i == index)
		{
			p[index] = data;
			return;
		}
	}
	printf("没有这个元素
");
	
}

//查找元素
void find(int index)
{
	if (index<0 || index>len)
	{
		return;
	}
	for (int i = 0; i < len; i++)
	{
		if (i == index)
		{
			printf("数组下标%d对应的元素为:%d
", index, p[index]);
			return;
		}
	}
	printf("没有这个下标
");
}

void print()
{
	for (int i = 0; i < len; i++)
	{
		printf("%d	", p[i]);
	}
}



int main()
{

	for (int i = 0; i < 10; i++)
	{
		insert(i + 1);
	}
	printf("
");
	print();
	printf("
");
	delete_last();
	insert(3, 100);
	dele(5);
	change(3, 20);
	find(6);
	print();
	delete[]p;
	p = NULL;

	getchar();
	getchar();
	return 0;
}


二、命名空间

1.知识点

1.命名空间是用来组织和重用代码的编译单元

2.在编写代码时写的程序不可能所有的标识符都没有重名现象,在多人协同开发时更加不可控,尤其对于库来说问题更加严重。为了解决重名现象,通过命名空间来解决冲突。

3.命名空间中的东西对外是不开放的,外面不能直接访问命名空间中的成员

2.命名空间的定义

namespace 命名空间标识符
{
    命名空间成员
}
//花括号只有两种情况:1.定义域(必须加分号)
//                  2.作用域(可加可不加)

3.注意

1.命名空间标识符必须满足标识符的命名规则和命名规范,习惯名字唯一,通常以开发团队的名字(项目名)来命名

2.命名空间可以在全局,也可以在局部(命名空间接受嵌套定义),但不能在函数内和类中定义

3.命名空间的花括号是作用域

4.注意命名污染,尽量规避同名的出现,如果两个命名空间同名,那么就会合并两个命名空间

4.命名空间访问

#include <stdio.h>

namespace yunfei
{
	int age = 18;
	void fun1()
	{
		printf("age:%d",age);
	}
	namespace feiyun
	{
		int age = 19;
		void fun2()
		{
			printf("age:%d", age);
		}
	}
}


int main()
{

	getchar();
	getchar();
	return 0;
}


作用域运算符 ::

可以理解为,什么的什么

1.using声明

using 命名空间名称 :: 空间成员名称;

using yunfei::feiyun::fun2;

2.using 指示

using namespace 命名空间名

放开该命名空间的所有权限(所有成员都在空间外可见),适用于该空间的大部分成员都需要经常被使用

using namespace yunfei;
//把yunfei空间下的所有成员访问都公开出来,都可以在外面访问

3.命名空间名称::空间成员名称,直接访问空间下的某一个成员

//yunfei::feiyun::fun2();

5.命名空间取别名

1.namespace 别名=命名空间名

2.当命名空间标识符过长或不太方便记忆,可通过取别名的方式来表示该命名空间,别名的操作等价于原命名空间

6.命名空间成员的声明及定义

namespace A                 void A::fun()
{								{
    void fun();						//函数功能
}								}
//声明					命名空间外实现

三、cin和cout

1.知识点

cin的作用和scanf一样

cout的作用和printf一样

2.使用前准备

1.需要包含头文件

2.需要声明命名空间std中的权限

3.c++的头文件,不带.h , 如果你要包含C语言的头文件正常写,c++兼容C语言,或者#include

int main()
{
	int a = 10;
	char str[] = "awubdjajs";
	cout << str << a << endl;
	cout << "ei	fiarv
prv" << endl;
	cin >> a >> str;//两个输入之间用空格区分
	cout << a << endl << str << endl;
	system("pause");
	return 0;
}

四、string的基本使用

1.知识点

1.string是c++中的字符串。类似于C语言中的字符数组

2.string是系统提供的一个类

2.基本使用

1.需要包含头文件和声明命名空间std中的权限

2.赋值:string str1=“adnwd”,str2;str2 = str1;

3.求长度: int len = str1.length();

4.清空字符串:str1.clear();

5.判断字符串是否为空: if(str1.empty()==NULL)

6.比较字符串是否相同:if(str1==str2)

7.得到字符串中某一个位置的字符

char c=str1[3];

char a=str1.at(3);

注意:这两个3指的是字符串中下标(从0开始)为3的字符,不能越界。

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string str1 = "asgwd";
	string str2 = "aaaaaaa";
	if (str1 == str2)
	{
		cout << "相等" << endl;
	}
	else
	{
		cout << "不相等" << endl;
	}
	cout << str1.length() << endl;
	//得到w
	cout << str1.at(3) << endl;
	cout << str1[3] << endl;
	str1[3] = 'a';//修改字符串中的字符
	cout << str1 << endl;

	//字符串之间直接赋值,不需要函数(重载)
	str1 = str2;


	system("pause");
	return 0;
}

补充:

1.三名运算符,在C语言中,返回的是一个常量,不能被赋值的,c++返回的是变量

2.c++的函数必须要写返回值类型

3.在全局下面,c++不允许 int a;int a=10;这种二义性操作

4.C语言用const修饰的变量

原文地址:https://www.cnblogs.com/Kissfly123/p/14409784.html