C++学习day2

day2
从 C 到 C++
1. 引用 reference
1.1 概念
引用与指针类似,但是使用更加简便,功能更加简单,可以认为引用是一个变量的“别
名”,对引用进行操作与直接操作变量完全相同。
1.2 性质
1. 可以改变引用的变量值,但是不能再次成为其它变量的引用。
#include <iostream>
using namespace std;
int main()
{
 int a = 100;
 int& b = a; // b 是 a 的引用
 b++;
 cout << a << " " << b << endl; // 101 101
 cout << &a << " " << &b << endl; // 0x61fe88 0x61fe88
 return 0;
}
2. 声明引用时,必须对其进行初始化。
#include <iostream>
using namespace std;
int main()
{
 int a = 100;
 int& b = a; // b 是 a 的引用
 int c = 200;
 b = c; // 赋值操作
 cout << &a << " " << &b << " " << &c << endl; // 0x61fe88 
0x61fe88 0x61fe84
// &b = c; 错误
// int& b = c; 错误
 return 0;
}
3. 声明引用时初始化的值不能是 NULL
#include <iostream>
using namespace std;
int main()
{
 int a = 100;
// int& b; 错误
// b = a;
// cout << b << endl;
 return 0;
}
5. 可以将变量引用的地址赋值给一个指针,此时指针指向的还是原来的变量。
#include <iostream>
using namespace std;
int main()
{
// int& b = 100; 错误
 const int& b = 100;
 cout << b << endl;
 return 0;
}
#include <iostream>
using namespace std;
int main()
{
 int a = 10;
 int* p = &a; // 指针 p 指向 a
 int*& t = p; // t 是 p 的引用
 cout << t << " " << *t << endl; // 0x61fe88 10
 return 0;
}
7. 可以使用 const 对引用进行修饰,此时虽然不能直接改变引用的值,但是可以间接改变
原变量值。
#include <iostream>
using namespace std;
【思考】写一个函数,通过传入两个参数可以交换传入参数的两个整型数值。
/**
* @brief swap1 交换失败
*/
void swap1(int a,int b)
{
 a = a^b;
 b = a^b;
 a = a^b;
 cout << a << " " << b << endl;
}
/**
* @brief swap2 交换成功,但是繁琐
*/
void swap2(int* a,int* b)
{
 *a = *a^*b;
 *b = *a^*b;
 *a = *a^*b;
}
/**
* @brief swap3 交换成功
*/
void swap3(int& a,int& b)
{
 a = a^b;
 b = a^b;
 a = a^b;
}
int main()
{
 int a = 1;
 int b = 2;
 swap1(a,b);
使用引用参数还可以使参数传递的效率提高,因为不产生副本。
引用参数应该在能被定义为 const 的情况下,尽量定义 const,以达到引用的安全性。
2. 赋值
C++中除了可以使用=赋值外,还可以使用下面的方式赋值。
C++11 中增加了数据窄化的写法与提示。
void show(const int& a)
{
 cout << a << endl;
}
#include <iostream>
using namespace std;
int main()
{
 int a(1); // 给 a 赋值 1
 int b(a); // 给 b 赋值 a
 cout << a << " " << b << endl; // 1 1
 
 return 0;
}
3. 键盘输入
在 C++中可以使用 cin 来获得键盘输入,也支持连续输入,也在头文件 iostream 里。连
续输入的内容可以使用空格或回车来分割。
cin 输入的字符串类型是 string,这是 C++的字符串类型。
#include <iostream>
using namespace std;
int main()
{
 double a = 3.14;
 int b(a); // 传统 C++写法
 int c{a}; // C++11 写法:给出警告
 cout << b << endl;
 cout << c << endl;
 return 0;
}
4. 字符串类型 string
4.1 基础使用
string 并不是 C++的基本数据类型,string 是 C++内置的字符串类,使用时需要引入头文
件<string>,而不是<string.h>,string 在绝大多数情况下可以代替 char*,不必担心内存是否
足够和字符串长度。
string 使用 ASCII 编码,只支持英文字符,严禁使用中文字符!!!
string 内部集成了大量的字符串处理函数。
#include <iostream>
using namespace std;
int main()
{
 string s; // 创建一个内容为空的字符串变量
 cout << s << endl; // 无输出结果
 int i;
 cout << "请输入一个字符串和一个数字:" << endl;
 cin >> s >> i;
 cout << "请输入的内容是:" << s << endl << i << endl;
 return 0;
}
4.2 取出元素
string 不光支持中括号 [ ] 的方式取出元素,还支持使用 at 函数的方式取出元素。
这两种方式的区别是:
 前者取出的效率更高
 后者更加安全
#include <iostream>
using namespace std;
int main()
{
 string s = "yui%^";
 // 获取字符串长度
 cout << s.size() << " " << s.length() << endl; // 5 5
 cout << "--------for 循环遍历--------" << endl;
 for(int i = 0;i<s.size();i++)
 {
 // 使用下标取出对应的字符
 cout << s[i] << " ";
 }
 cout << endl;
 // C++11 支持
 cout << "--------for each 循环遍历--------" << endl;
 for(char c:s)
 {
 cout << c << " ";
 }
 // 迭代器:略
 return 0;
}
5. 函数
5.1 内联函数 inline
内联函数的目的是取代宏定义的函数,使用关键字 inline 放在函数定义(非声明)的前
面,可以将函数制定为内联函数。
内联函数在编译时,会直接展开函数体到主函数中编译,因此可以提升程序的运行效率。
一般将代码长度较小(5 行以内,不能包含复杂的控制语句)且频繁使用的函数写为内联函
数。
#include <iostream>
using namespace std;
int main()
{
 string s = "yui%^";
 // 使用中括号取出元素
 cout << s[1] << endl; // u
 // 使用 at 函数取出元素
 cout << s.at(1) << endl; // u
 // 范围越界
 cout << s[-999] << endl; // '\0'
 cout << s.at(20) << endl; // 运行终止
 cout << "主函数执行完毕" << endl;
 return 0;
}
所有的成员函数默认为内联函数,无需手动使用 inline 修饰。
5.2 函数重载 overload(重点)
C++中允许使用同一个函数名臣定义多个函数,这就是函数重载。函数重载的前提是各个
重载的函数之间参数(类型或个数)不同,与返回值类型无关。
#include <iostream>
using namespace std;
// 声明函数
void test();
// 定义函数
inline void test()
{
 cout << "aaa" << endl;
}
int main()
{
 test();
 return 0;
}
#include <iostream>
using namespace std;
void show()
{
 cout << "1" << endl;
}
void show(int a)
{
 cout << "2" << a << endl;
}
void show(double d)
{
 cout << "2.5" << d << endl;
}
void show(string s)
{
 cout << "3" << s << endl;
}
void show(string s1,string s2)
{
 cout << "4" << s1 << s2 << endl;
}
int main()
{
 show(1); // 21
 show(3.14); // 2.53.14
 show("Hello"); // 3Hello
 show(); // 1
 show("AA","BB"); // 4AABB
 return 0;
}
除了上述的普通函数支持重载外,成员函数和构造函数等也支持函数重载,但是析构函数
不支持函数重载。
5.3 函数的参数默认值(缺省值)
C++中允许给函数的参数设定默认值,在调用函数时,如果传入参数,则传入的参数会覆
盖默认值;如果不传入参数,则使用默认值作为参数值。
参数的默认值只允许在声明和定义中出现一次。
向右(后)原则:如果函数参数有多个,此时给某个参数设定了默认值后,其右边(后
边)所有的参数都必须设定默认值。
#include <iostream>
using namespace std;
void func1(int a = 0); // 声明时制定参数默认值
void func2(int a);
void func1(int a)
{
 cout << a << endl;
}
void func2(int a = 1) // 定义时制定参数默认值
{
 cout << a << endl;
}
int main()
{
 func1(); // 0
 func1(666); // 666
 func2(); // 1
 func2(888); // 888
 return 0;
}
当函数的参数默认值与函数重载同时使用时,非常容易导致二义性问题(ambiguous),二
义性问题指的是编译器无法做出两种情况的抉择。
尽量不要同时使用函数重载和默认值。
#include <iostream>
using namespace std;
void method1(int a,int b=1,int c=2)
{
 cout << a << b << c << endl;
}
int main()
{
// method1(); 错误
 method1(0); // 012
 method1(0,0); // 002
 return 0;
}
5.4 哑元函数
如果一个函数的参数只有类型,没有名字,这个参数在函数体中无法使用,这样的参数被
称为哑元,这样的函数被称为哑元函数。
#include <iostream>
using namespace std;
void method(int a=0,int b=1,int c=2)
{
 cout << a << b << c << endl;
}
void method()
{
 cout << "aaa" << endl;
}
int main()
{
// method(); 错误
 return 0;
}
哑元函数的主要有以下用途:
 区分重载函数,后面在运算符重载中使用。
 保持函数的向前兼容性
Tips:
快捷键:
Alt + 0 隐藏/显示边栏
Alt + 左或右 光标回到上次编辑位置/前进到更新的编辑位置
先 Ctrl + A,再 Ctrl + I 对齐代码
Ctrl + Z 撤销
Ctrl + Y 重做
Ctrl + R 运行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值