目录
【1】引用
1》基础使用
引用就是某个变量或常量的别名,对引用进行操作与操作原变量或常量完全相同。
#include <iostream>
using namespace std;
int main()
{
int a = 1;
int& b = a; // b是a的引用
b++;
cout << a << " " << b << endl; // 2 2
cout << &a << " " << &b << endl; // 0x61fe88 0x61fe88
return 0;
}
2》特性
1.可以改变引用变量的值,但是不能再次成为其他变量的引用。
#include <iostream>
using namespace std;
int main()
{
int a = 1;
int& b = a; // b是a的引用
int c = 8;
b = c; // 仅仅是赋值,b仍然是a的别名
cout << a << " " << b << " " << c << endl; // 8 8 8
cout << &a << endl; // 地址1
cout << &b << endl; // 地址1
cout << &c << endl; // 地址2
// &b = c; 错误
// int& b = c; 错误
return 0;
}
2.声明引用时,必须初始化。
3.声明引用时,初始化的值不能为NULL
4.声明引用时,初始化的值可以是纯数值,但是此时引用需要使用const修饰,表示常引用,不允许修改引用值。
#include <iostream>
using namespace std;
int main()
{
// int& b = 123; 错误
const int& b = 123; // 常引用
cout << b << endl; // 123
// b++; 错误
return 0;
}
5.可以将变量引用的地址赋值给一个指针,此时指针指向的还是原来的变量。
#include <iostream>
using namespace std;
int main()
{
int a = 1;
int &b = a;
int* c = &b;
cout << *c << endl; // 1
cout << c << " " << &a << endl; // 0x61fe84 0x61fe84
return 0;
}
6.可以建立指针变量的引用。
#include <iostream>
using namespace std;
int main()
{
int a = 1;
int* b = &a;
int*& c = b; // c是b的引用
cout << c << " " << b << " " << endl; // 0x61fe88 0x61fe88
cout << *c << endl; // 1
return 0;
}
7.可以使用const修饰引用,表示常引用,此时不允许改变引用的值,但是可以改变原变量。
#include <iostream>
using namespace std;
int main()
{
int a = 1;
const int& b = a; // b是a的常引用
// b++; 错误
a++;
cout << b << endl; // 2
return 0;
}
3》引用参数
思考题:写一个函数,功能为交换两个int变量的值。
#include <iostream>
using namespace std;
/**
* @brief swap1 错误的交换方式
*/
void swap1(int a,int b)
{
a = a ^ b;
b = a ^ b;
a = a ^ b;
cout << a << endl; // 2
cout << b << endl; // 1
}
/**
* @brief swap2 C的交换方式
* 指针比较复杂繁琐
*/
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);
cout << a << endl; // 1
cout << b << endl; // 2
swap2(&a,&b);
cout << a << endl; // 2
cout << b << endl; // 1
swap3(a,b);
cout << a << endl; // 1
cout << b << endl; // 2
return 0;
}
使用引用作为函数参数时,不产生副本,可以使参数传递的效率提升,相比指针又更加简洁。
需要注意的是:引用参数应该在未被const修饰的情况下,尽量使用const修饰,以达到引用参数的安全性。
#include <iostream>
using namespace std;
void show(const int& a,const int& b)
{
cout << a << endl;
cout << b << endl;
}
int main()
{
int a = 1;
int b = 2;
show(a,b);
cout << a << endl;
return 0;
}
【2】C++窄化
#include <iostream>
using namespace std;
int main()
{
double a = 123234.8;
int b = (int)a; // C强制类型转换格式
int c(a); // C++格式
cout << b << endl;
cout << c << endl;
int d{a}; // C++11格式:可以给出数据窄化警告
cout << d << endl;
return 0;
}
【3】输入
在 C++中使用cin进行键盘输入,与cout一样支持连续操作。
#include <iostream>
using namespace std;
int main()
{
int a;
double b;
string c; // C++的字符串类型
cout << "依次输入整型、浮点型与字符串类型:" << endl;
// 连续输入三个数据,分别给abc三个变量
cin >> a >> b >> c;
cout << "您输入的数据是:" << endl;
cout << a << endl;
cout << b << endl;
cout << c << endl;
return 0;
}
如果想输入空格,可以使用getline函数,可以一次获得一行的数据
#include <iostream>
using namespace std;
int main()
{
string s;
cout << "请输入一行字符串,可以包含空格:" << endl;
getline(cin,s);
cout << "您输入的数据是:" << endl;
cout << s << endl;
return 0;
}
【4】string字符串类
1》基础使用
string 不是C++本身的基本数据类型,而是在C++标准库std中的一个字符串类。
在使用时需要引入头文件<string>,而不是<string.h>,string用于在绝大多数情况下代替char* ,不必担心内存是否足够、字符长度等问题。
string内部集成的函数可以完成绝大多数情况下字符串的操作。
string支持多种遍历方式。
for循环
for-each循环(C++11)
#include <iostream>
using namespace std;
int main()
{
string s = "fsdhfjdhsjkf";
// size和length效果完全相同
cout << "字符串长度:" << s.size()
<< " " << s.length() << endl;
cout << "for循环遍历:" << endl;
for(int i = 0;i<s.size();i++)
{
cout << s[i] << " ";
}
cout << endl << "for-each遍历:" << endl;
for(char i:s)
cout << i << " ";
return 0;
}
2》取出元素
在C++中除了使用[]取出元素外,还可以使用 at 函数取出元素。
在绝大多数情况下,更推荐使用 at 函数取出元素,因为 at 函数更安全,但是[]性能更好。
#include <iostream>
using namespace std;
int main()
{
string s = "fsdhfjdhsjkf";
cout << s[1] << endl; // 's'
// at函数
cout << s.at(1) << endl; // 's'
// cout << s[-100] << endl; // '\0'或乱码
cout << s.at(-100) << endl; // 检测到越界则终止运行
cout << "主函数结束" << endl;
return 0;
}
3》字符串与数字转换
在此处使用字符串流的方式举例:
整数---->字符串
#include <iostream>
#include <sstream> // 头文件
using namespace std;
int main()
{
int i = 2323;
stringstream ss;
ss << i;
string s = ss.str();
cout << s << endl;
return 0;
}
字符串---->整数
#include <iostream>
#include <sstream> // 头文件
using namespace std;
int main()
{
string s = "12345";
istringstream iss(s);
int i;
iss >> i;
cout << i << endl;
return 0;
}
今天的分享就到这里结束啦,如果有哪里写的不好的地方,请指正。
如果觉得不错并且对你有帮助的话点个关注支持一下吧!
1482

被折叠的 条评论
为什么被折叠?



