C++基础语法1( namespace,输入与输出C++关键字升级)

本文介绍了C++中的命名空间用于解决命名冲突,头文件避免重定义方法,以及关键字如register、const、typedef和auto的使用。此外,还对比了new与malloc的区别,涉及内存管理和数组分配。
摘要由CSDN通过智能技术生成

目录

 namespace命名空间

命名空间的定义:

命名空间注意事项:

头文件避免重定义方法:

输入和输出:

关键字的升级:

1.register:

2.const:

3.typedef:

4.auto:类型推导

5.new 与delete:

6.new 与 malloc 区别


 namespace命名空间

命名空间的定义:

为解决合作开发时或者不同代码段之间的命名冲突问题,引入命名空间

语法格式:

namespace name
{
    //变量,函数,类等
}

命名空间注意事项:

无法访问其他文件命名空间的内容,放在头文件也无法访问

命名空间名称相同,成员名字不同,那么他们会自动合并为一个命名空间,理解为追加

命名空间相同,成员名字相同,那么程序会在调试过程的link时报错,使用extern解决

命名空间举例:

#include <iostream>

namespace A
{
    int a = 5;
}
 //int a = 5;

void func1()
{
    //std::cout << a << std::endl;
    // :: 运算符:作用域限制
    std::cout << A::a << std::endl;
    //在文件头申明using namespace std; 后此处代码可更改为:
    //cout << A::a << endl; 
}

func2.cpp

#include <iostream>

namespace B
{
    int a = 5;
}

//int a = 5;

void func2()
{
    //std::cout << B::a << std::endl;
    std::cout << a << std::endl;
}

main.cpp

#include <iostream>

extern void func1();
extern void func2();

using namespace std;

int main()
{
    func1();
    func2();
}

头文件避免重定义方法:

#pragma once 

//等同于

 #ifndef __FUNC1_H
 #define __FUNC1_H

 #endif

输入和输出:

cin表示标准输入,cout表示标准输出,cerr表示标准错误 clog标志日志流

示例:

#include <iostream>

using namespace std;
//cout 行缓冲
//cerr clog 无缓冲

int main()
{
    cout << "hello world" << endl;

    int num1;
    int num2;
    char src[100];

    cin >> num1 >> num2;  //遇到空格会结束

    cout << "num1 =" << num1 <<endl;  
    cout << "num2 =" << num2 <<endl;
    
    //解决缓冲区垃圾问题
    cin.get(); //读一个字符,将上一个cin 输入后的\n 读走
    cin.getline(src,100);//避免读到空格或换行符就停止写入

    cout << "src=" << src << endl;

    return 0;
}

关键字的升级:

1.register:

尽可能将变量保存到CPU内部寄存器,从而省去了CPU从内存获取的时间,从而提高了运行效率

注意事项:

1.只能修饰局部变量,不能修饰全局变量和函数

2.register修饰的变量不能通过&获取变量的地址;(C++升级:当用&获取变量地址时,该变量不会保存到寄存器中,重新保存到内存中)

3.register修饰的变量类型一定是CPU所能接受的数据类型

register int num = 5;
cout << &num << endl;

2.const:

c++升级:const修饰的变量就是常量,无法通过指针来进行修改

const 离谁近谁不能自加

C:

const int count = 5;
int *p = &count;
*p = 7;
printf("count = %d\n",count);
//最后count为7

C++:

const int count = 5;
const int *p = &count; //const对const:const地址必须由const指针保存
const char *p = "hello world";  //"hello world"也为常量

3.typedef:

给数据类型重命名,提高代码的移植性,提高代码的编码效率。

使用typedef如何重命名函数指针 :

C:

int add(int a , int b)
{
     return a + b;
}
typedef int(*P_FUNC)(int, int); // p_add函数指针变量

P_FUNC p_func //等同于int(*p_func)(int,int);
p_func = add;
p_func = (5,6);

C++:

using P_FUNC2 = int(*)(int, int);
P_FUNC2 p_func2 = add;
p_func2(5,6);

using INT = int;

4.auto:类型推导

C++升级:auto类型推导,目标:现代化编程(提高开发指令),根据赋予的值推导变量类型

C++:

auto a = 5;
auto b = add;

5.new 与delete:

#include<iostream>

using namespace std;

int main()
{
    //malloc free 如何知道要释放多少内存

    //c++/new/delete

    int *p = new int(5);//分配空间并初始化
    char *ptr = new char[100];

    delete p;
    delete [] ptr;

    cout << *p << endl;

    return 0;   
}

#include<iostream>

using namespace std;

int main()
{

	//分配二维数组
	int(*p)[5] = new int[4][5]; //规则数组

	printf("p = %p\n", p);
	printf("p+1 = %p\n", p+1);

	char(*ptr)[100] = new char[3][100];


	int** array2;
	array2 = new int *[2]; //不规则多维数组 地图

	array2[0] = new int[10];
	array2[1] = new int[20];
	for (int i = 0; i < 2; i++)
	{
		array2[i] = new int[3];
	}
	printf("array2 = %p\n", array2);
	printf("array2 +1 = %p\n", array2+1);

}

6.new 与 malloc 区别

malloc是按照字节为单位分配,new按照数据类型为单位分配空间;

malloc是函数,new是运算符;

malloc只分配空间,不初始化,new 既可以分配空间又可以初始化

new delete 如果分配失败产生异常, malloc free 返回NULL

new 底层调用的malloc delete底层调用的是free

new malloc: 频繁分配小内存会导致内存产生碎片,同时会增加开销 通过池化技术解决

举例:

使用new给多维数组分配空间 int a[2][2];

一维数组名的作用:保存数据首个元素的地址

二维数组名的作用:保存数据首个一维数组的地址

三维数组名的作用:保存数据首个二维数组的地址

int(*pa)[2] = new int[2][2];
int ** pa2 = new int *[2];

for (int i = 0; i < 2; i++)
{
    pa2[i] = new int *[2];
}

for (int i = 0; i < 2; i++)
{
    for(int j = 0; j < 2; j++)
    {
        pa2[i][j] = j+1;
    }
}

//遍历
int a[2] = {11, 12};
for(int temp :a)
{
    cout << temp << endl;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值