C++Day2

复习:

1.名字空间作用

避免名字冲突

2.名字空间的使用方法 如:endl

using namesapce std;
std::endl
using std::endl;

3.c++对c增强有哪些

bool类型:真1 true 假 0 false bool类型a 与0比较 if(!a)
a>b?a:b=90
char p=(char)malloc(sizeof(char))
for(int i=0;i<3;i++)
{

}

4.string类型使用前需要注意添加什么?

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

作业:

随机点名 定义名字数组string names[10]={“rose”,“jack”,“tom”}; 字符串数组
随机抽取一个数组中的人员

	头文件:#include<cstdlib>
			#include<ctime>
	方法:srand(time(0));初始化随机种子
			rand()%10;生成随机数

实现:
#include
#include
#include
#include
using namespace std;
int main()
{
int index;
string names[]={“rose”,“jack”,“tom”,“xiaoming”};
srand(time(0));
while(1)
{
index = rand()%4;
cout<<names[index]<<endl;
sleep(1);
system(“clear”);
}
}

一、string类型续

string不用考虑内存释放和越界。
string提供了一系列的字符串操作函数
查找find,拷贝copy,删除erase,替换replace,插入insert

1.c++风格字符串与c风格字符串转换

(1)c中字符串 自动转为c++风格 	
    string s="hello";

(2)c++风格转为c风格--字符串格式化或者传递char*参数时需要转换
 #include<iostream>
#include<string>
using namespace std;
int main()
{
    string s="good day";
    const char *p = s.c_str();

    cout<<p<<endl;//printf("%s",p);
}

注意://C++不提倡使用C风格字符串,但是在调用一些API函数时会使用到C风格字符串,
	  //这时才有必要进行转换char*

思考:在s2后面拼接 数字 12 —不同类型

2.c++中没有函数能直接将字符串转为其他类型 想转

#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
int main()
{
    string s1="good day";
    int a=12;

    char buf[20]="\0";

//拼接
    sprintf(buf,"%s%d",s1.c_str(),a);
    cout<<buf<<endl;
}

二、引用

1.引用的基本概念

(1)变量名回顾
变量名实质上是一段连续存储空间的别名,是一个标号(门牌号)

在C/C++中,作用域限制的不是变量的内存,而是变量的名字。
在C语言中我们要在不同的函数中对同一个变量操作,使用指针。
但是指针的操作不是那么的简单!

(2)引用 reference 属于C++编译器对C的扩展
功能:为一个变量起别名(小名)

引用的语法:Type& name = var; (相当于指针功能  )

1)引用变量是一个别名,是c++对c的重要扩充。也就是说,它是某个已存在变量的另一个名字。
2)一旦把引用初始化为某个变量,就可以使用该引用名称或变量名称来指向变量。
3)C++之所以增加引用类型, 主要是把它作为函数参数,以扩充函数传递数据的功能。

(3)引用使用
例子1://定义ref为a的引用 &是引用声明符

#include<iostream>//input output stream  输入输出头文件
using namespace std;
int main()
{
    int a=100;
    int &ref=a;//声明ref为变量a的引用  & 引用声明符

    cout<<"a:"<<a<<endl;
    cout<<"ref:"<<ref<<endl;

    ref = 200;//通过引用改变a的值
    cout<<"a:"<<a<<endl;
    cout<<"ref:"<<ref<<endl;
}


例子2://引用必须初始化 引用不可以改变指向 
#include<iostream>//input output stream  输入输出头文件
using namespace std;
int main()
{
    int a=100;
    int &ref=a;//声明ref为变量a的引用  & 引用声明符

    cout<<"a:"<<a<<endl;
    cout<<"ref:"<<ref<<endl;


    int b=88;
    ref = b;//赋值 不可以改变引用的指向

    cout<<"a:"<<a<<endl;//88
    cout<<"ref:"<<ref<<endl;//88      

}

总结:引用的特点:
		1.必须初始化 int &b; 
		2.引用不能单独存在,也不能改变指向 

2.引用的应用

(1)引用做形参
#include//input output stream 输入输出头文件
using namespace std;

void fun(int &x)//int &x=a
{
    x=200;
}
int main()
{
    int a=100;
    fun(a);

    cout<<a<<endl;
}

练习:swap

int main()
{
    int i=89,j=23;
    swap()
    cout<<i<<endl;//23 
    cout<<j<<endl;//89
}

练习:swap

#include<iostream>//input output stream  输入输出头文件
using namespace std;

void swap(int &x,int &y)  //  p=&a; 
{
    int tmp;
    tmp = x;
    x = y;
    y = tmp;
}
int main()
{
    int j=89,i=23;
    swap(j,i);
    cout<<j<<endl;//23 
    cout<<i<<endl;//89
}

(2)引用做返回值 函数返回值是引用(引用当左值)
引用作为返回值,是返回变量本身,而不是一个临时的值,要保证变量的生命周期足够长。

实例:
#include//input output stream 输入输出头文件
using namespace std;

int fun()
{
    static int a=90;
    return a;
}

int& fun1()
{
    static int a=90;
    return a;
}
int main()
{

    //fun()=300;//返回常量值

    fun1()=300;//返回值是变量
}

3、引用分类

引用分为普通引用和常引用

(1)引用有变量的引用和常量的引用 const修饰的引用是常引用 const引用让变量拥有只读属性

(2)常量引用初始化 分为两种

1) 用变量 初始化 常量引用

    #include<iostream>//input output stream  输入输出头文件
    using namespace std;
    int main()
    {
        int a=66;
        const int &ref = a;
        cout<<ref<<endl;
        cout<<a<<endl;
    }

2)用临时值及常量 初始化 常量引用

#include<iostream>//input output stream  输入输出头文件
using namespace std;
int main()
{

     const int &ref=100;

    cout<<ref<<endl;

    int a=66;
    const int &r=a+2;
    cout<<r<<endl;
}

(3)常引用应用

1)做形参
#include<iostream>//input output stream  输入输出头文件
using namespace std;

void fun(const int &x)//const int &x=a
{
    //x = 99;//作用:保护实际参数不被修改
}
int main()
{
    int a=100;
    fun(a);
}

巩固小练习
(1)判断下面的几种定义引用对错
int& b;//错 引用必须初始化
int& c = 10;//错 应该是常引用 const int &c=10
const int& d = 10; //正确

(2)判断对错
#include
using namespace std;
int main()
{
int a = 10; //对
const int& b = a;//对
b = 20; //不可以 b是常引用
a = 30;// 正确
cout<<a<<endl;
}
(3)
int a = 10;//
int& c = a + 2; //const int &c=a+1
const int& c = a + 2;//正确
cout<<c<<endl;

总结:
{
    引用必须初始化。
    引用不能单独存在。
    引用也不能改变指向。
    普通引用不能指向常量与临时量。常引用可以指向常量与临时量。
}

参考:
1)当使用常量(字面量)对const引用进行初始化时,C++编译器会为常量值分配空间,并将引用名作为这段空间的别名
2)使用字面量对const引用初始化后,将生成一个只读变量
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

encounter♌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值