C++:引用的简单理解

那里用到了呢?
1022. Digital Library (30)-PAT甲级真题(map映射)
A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID’s.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=10000) which is the total number of books. Then N blocks follow, each contains the information of a book in 6 lines:

Line #1: the 7-digit ID number;
Line #2: the book title — a string of no more than 80 characters;
Line #3: the author — a string of no more than 80 characters;
Line #4: the key words — each word is a string of no more than 10 characters without any white space, and the keywords are separated by exactly one space;
Line #5: the publisher — a string of no more than 80 characters;
Line #6: the published year — a 4-digit number which is in the range [1000, 3000].
It is assumed that each book belongs to one author only, and contains no more than 5 key words; there are no more than 1000 distinct key words in total; and there are no more than 1000 distinct publishers.

After the book information, there is a line containing a positive integer M (<=1000) which is the number of user’s search queries. Then M lines follow, each in one of the formats shown below:

1: a book title
2: name of an author
3: a key word
4: name of a publisher
5: a 4-digit number representing the year
Output Specification:

For each query, first print the original query in a line, then output the resulting book ID’s in increasing order, each occupying a line. If no book is found, print “Not Found” instead.

Sample Input:
3
1111111
The Testing Book
Yue Chen
test code debug sort keywords
ZUCS Print
2011
3333333
Another Testing Book
Yue Chen
test code sort keywords
ZUCS Print2
2012
2222222
The Testing Book
CYLL
keywords debug book
ZUCS Print2
2011
6
1: The Testing Book
2: Yue Chen
3: keywords
4: ZUCS Print
5: 2011
3: blablabla
Sample Output:
1: The Testing Book
1111111
2222222
2: Yue Chen
1111111
3333333
3: keywords
1111111
2222222
3333333
4: ZUCS Print
1111111
5: 2011
1111111
2222222
3: blablabla
Not Found

题目大意:模拟数字图书馆的查询功能。会给出n本书的信息,以及m个需要查询的命令,数字标号对应相应的命令,数字编号后面的字符串是查询的搜索词,要求输出这行命令以及输出满足条件的书的id,如果一个都没有找到,输出Not Found
分析:
– 对除了id之外的其他信息都建立一个map<string, set>,把相应的id插入对应搜索词的map的集合里面,形成一个信息对应一个集合,集合里面是复合条件的书的id。
– 因为对于输入的关键词(可以重复,算是书本对应的tag标签吧~)没有给定关键词的个数,可以使用while(cin >> s)并且判断c = getchar(),c是否等于\n,如果是再退出循环~
– 建立query,通过传参的形式可以将不同的map名称统一化,先要判断map.find()和m.end()是否相等,如果不等再去遍历整个map,输出所有满足条件的id,如果相等就说明不存在这个搜索词对应的id,那么就要输出Not Found~

– 传参一定要用引用,否则最后一组数据可能会超时

#include <iostream>
#include <map>
#include <set>
using namespace std;
map<string, set<int> > title, author, key, pub, year;
void query(map<string, set<int> > &m, string &str) {  //使用了引用,提高效率
    if(m.find(str) != m.end()) {
        for(auto it = m[str].begin(); it != m[str].end(); it++)
            printf("%07d\n", *it);
    } else
        cout << "Not Found\n";
}
int main() {
    int n, m, id, num;
    scanf("%d", &n);
    string ttitle, tauthor, tkey, tpub, tyear;
    for(int i = 0; i < n; i++) {
        scanf("%d\n", &id);
        getline(cin, ttitle);
        title[ttitle].insert(id);
        getline(cin, tauthor);
        author[tauthor].insert(id);
        while(cin >> tkey) {
            key[tkey].insert(id);
            char c = getchar();
            if(c == '\n') break;
        }
        getline(cin, tpub);
        pub[tpub].insert(id);
        getline(cin, tyear);
        year[tyear].insert(id);
    }
    scanf("%d", &m);
    for(int i = 0; i < m; i++) {
        scanf("%d: ", &num);
        string temp;
        getline(cin, temp);
        cout << num << ": " << temp << "\n";
        if(num == 1) query(title, temp);
        else if(num == 2) query(author, temp);
        else if(num == 3) query(key, temp);
        else if(num == 4) query(pub,temp);
        else if(num ==5) query(year, temp);
    }
    return 0;
}

前言:引用是C++一个很重要的特性,最近看了很多有关引用的资料和博客,故在此对引用的相关知识进行总结

一、什么是引用
引用,顾名思义是某一个变量或对象的别名,对引用的操作与对其所绑定的变量或对象的操作完全等价

语法:类型 &引用名=目标变量名;
特别注意:

1.&不是求地址运算符,而是起标志作用

2.引用的类型必须和其所绑定的变量的类型相同

复制代码
1 #include< iostream>
2 using namespace std;
3 int main(){
4 double a=10.3;
5 int &b=a; //错误,引用的类型必须和其所绑定的变量的类型相同
6 cout<<b<<endl;
7 }
复制代码

3.声明引用的同时必须对其初始化,否则系统会报错

复制代码
1 #include
2 using namespace std;
3 int main(){
4 int &a; //错误!声明引用的同时必须对其初始化
5 return 0;
6 }
复制代码

4.引用相当于变量或对象的别名,因此不能再将已有的引用名作为其他变量或对象的名字或别名

5.引用不是定义一个新的变量或对象,因此内存不会为引用开辟新的空间存储这个引用

复制代码
1 #include
2 using namespace std;
3 int main(){
4 int value=10;
5 int &new_value=value;
6 cout<<“value在内存中的地址为:”<<&value<<endl;
7 cout<<“new_value在内存中的地址为:”<<&new_value<<endl;
8 return 0;
9 }
复制代码

6.对数组的引用

语法:类型 (&引用名)[数组中元素数量]=数组名;
复制代码
1 #include
2 using namespace std;
3 int main(){
4 int a[3]={1,2,3};
5 int (&b)[3]=a;//对数组的引用
6 cout<<&a[0]<<" “<<&b[0]<<endl;
7 cout<<&a[1]<<” “<<&b[1]<<endl;
8 cout<<&a[2]<<” "<<&b[2]<<endl;
9 return 0;
10 }
复制代码

7.对指针的引用

语法:类型 &引用名=指针名;//可以理解为:(类型) &引用名=指针名,即将指针的类型当成类型*
复制代码
1 #include
2 using namespace std;
3 int main(){
4 int a=10;
5 int *ptr=&a;
6 int *&new_ptr=ptr;
7 cout<<&ptr<<" "<<&new_ptr<<endl;
8 return 0;
9 }
复制代码

二、引用的应用
A.引用作为函数的参数
复制代码
1 #include
2 using namespace std;
3 void swap(int &a,int &b){//引用作为函数的参数
4 int temp=a;
5 a=b;
6 b=temp;
7 }
8 int main(){
9 int value1=10,value2=20;
10 cout<<"----------------------交换前----------------------------"<<endl;
11 cout<<“value1的值为:”<<value1<<endl;
12 cout<<“value2的值为:”<<value2<<endl;
13 swap(value1,value2);
14 cout<<"----------------------交换后----------------------------"<<endl;
15 cout<<“value1的值为:”<<value1<<endl;
16 cout<<“value2的值为:”<<value2<<endl;
17 return 0;
18 }
复制代码

特别注意:

1.当用引用作为函数的参数时,其效果和用指针作为函数参数的效果相当。当调用函数时,函数中的形参就会被当成实参变量或对象的一个别名来使用,也就是说此时函数中对形参的各种操作实际上是对实参本身进行操作,而非简单的将实参变量或对象的值拷贝给形参。

2.通常函数调用时,系统采用值传递的方式将实参变量的值传递给函数的形参变量。此时,系统会在内存中开辟空间用来存储形参变量,并将实参变量的值拷贝给形参变量,也就是说形参变量只是实参变量的副本而已;并且如果函数传递的是类的对象,系统还会调用类中的拷贝构造函数来构造形参对象。而使用引用作为函数的形参时,由于此时形参只是要传递给函数的实参变量或对象的别名而非副本,故系统不会耗费时间来在内存中开辟空间来存储形参。因此如果参数传递的数据较大时,建议使用引用作为函数的形参,这样会提高函数的时间效率,并节省内存空间。

3.使用指针作为函数的形参虽然达到的效果和使用引用一样,但当调用函数时仍需要为形参指针变量在内存中分配空间,而引用则不需要这样,故在C++中推荐使用引用而非指针作为函数的参数

4.如果在编程过程中既希望通过让引用作为函数的参数来提高函数的编程效率,又希望保护传递的参数使其在函数中不被改变,则此时应当使用对常量的引用作为函数的参数。

5.数组的引用作为函数的参数:C++的数组类型是带有长度信息的,引用传递时如果指明的是数组则必须指定数组的长度

复制代码
1 #include
2 using namespace std;
3 void func(int(&a)[5]){//数组引用作为函数的参数,必须指明数组的长度
4 //函数体
5 }
6 int main(){
7 int number[5]={0,1,2,3,4};
8 func(number);
9 return 0;
10 }
复制代码
B.常引用
语法:const 类型 &引用名=目标变量名;
常引用不允许通过该引用对其所绑定的变量或对象进行修改

复制代码
1 #include
2 using namespace std;
3 int main(){
4 int a=10;
5 const int &new_a=a;
6 new_a=11;//错误!不允许通过常引用对其所绑定的变量或对象进行修改
7 return 0;
8 }
复制代码

特别注意:

先看下面的例子

复制代码
1 #include
2 #include
3 using namespace std;
4 string func1(){
5 string temp=“This is func1”;
6 return temp;
7 }
8 void func2(string &str){
9 cout<<str<<endl;
10 }
11 int main(){
12 func2(func1());
13 func2(“Tomwenxing”);
14 return 0;
15 }
复制代码
运行上面的程序编译器会报错

这是由于func1()和“Tomwenxing”都会在系统中产生一个临时对象(string对象)来存储它们,而在C++中所有的临时对象都是const类型的,而上面的程序试图将const对象赋值给非const对象,这必然会使程序报错。如果在函数func2的参数前添加const,则程序便可正常运行了

复制代码
1 #include
2 #include
3 using namespace std;
4 string func1(){
5 string temp=“This is func1”;
6 return temp;
7 }
8 void func2(const string &str){
9 cout<<str<<endl;
10 }
11 int main(){
12 func2(func1());
13 func2(“Tomwenxing”);
14 return 0;
15 }
复制代码

C.引用作为函数的返回值
语法:类型 &函数名(形参列表){ 函数体 }
特别注意:

1.引用作为函数的返回值时,必须在定义函数时在函数名前将&

2.用引用作函数的返回值的最大的好处是在内存中不产生返回值的副本

复制代码
1 //代码来源:RUNOOB
2 #include
3 using namespace std;
4 float temp;
5 float fn1(float r){
6 temp=rr3.14;
7 return temp;
8 }
9 float &fn2(float r){ //&说明返回的是temp的引用,换句话说就是返回temp本身
10 temp=rr3.14;
11 return temp;
12 }
13 int main(){
14 float a=fn1(5.0); //case 1:返回值
15 //float &b=fn1(5.0); //case 2:用函数的返回值作为引用的初始化值 [Error] invalid initialization of non-const reference of type ‘float&’ from an rvalue of type ‘float’
//(有些编译器可以成功编译该语句,但会给出一个warning)
16 float c=fn2(5.0);//case 3:返回引用
17 float &d=fn2(5.0);//case 4:用函数返回的引用作为新引用的初始化值
18 cout<<a<<endl;//78.5
19 //cout<<b<<endl;//78.5
20 cout<<c<<endl;//78.5
21 cout<<d<<endl;//78.5
22 return 0;
23 }
复制代码
上例中4个case的说明解释:

case 1:用返回值方式调用函数(如下图,图片来源:伯乐在线):

返回全局变量temp的值时,C++会在内存中创建临时变量并将temp的值拷贝给该临时变量。当返回到主函数main后,赋值语句a=fn1(5.0)会把临时变量的值再拷贝给变量a

case 2:用函数的返回值初始化引用的方式调用函数(如下图,图片来源:伯乐在线)

这种情况下,函数fn1()是以值方式返回到,返回时,首先拷贝temp的值给临时变量。返回到主函数后,用临时变量来初始化引用变量b,使得b成为该临时变量到的别名。由于临时变量的作用域短暂(在C++标准中,临时变量或对象的生命周期在一个完整的语句表达式结束后便宣告结束,也就是在语句float &b=fn1(5.0);之后) ,所以b面临无效的危险,很有可能以后的值是个无法确定的值。

如果真的希望用函数的返回值来初始化一个引用,应当先创建一个变量,将函数的返回值赋给这个变量,然后再用该变量来初始化引用:

1 int x=fn1(5.0);
2 int &b=x;
case 3:用返回引用的方式调用函数(如下图,图片来源:伯乐在线)

这种情况下,函数fn2()的返回值不产生副本,而是直接将变量temp返回给主函数,即主函数的赋值语句中的左值是直接从变量temp中拷贝而来(也就是说c只是变量temp的一个拷贝而非别名) ,这样就避免了临时变量的产生。尤其当变量temp是一个用户自定义的类的对象时,这样还避免了调用类中的拷贝构造函数在内存中创建临时对象的过程,提高了程序的时间和空间的使用效率。

case 4:用函数返回的引用作为新引用的初始化值的方式来调用函数(如下图,图片来源:伯乐在线)

这种情况下,函数fn2()的返回值不产生副本,而是直接将变量temp返回给主函数。在主函数中,一个引用声明d用该返回值初始化,也就是说此时d成为变量temp的别名。由于temp是全局变量,所以在d的有效期内temp始终保持有效,故这种做法是安全的。

3.不能返回局部变量的引用。如上面的例子,如果temp是局部变量,那么它会在函数返回后被销毁,此时对temp的引用就会成为“无所指”的引用,程序会进入未知状态。

4.不能返回函数内部通过new分配的内存的引用。虽然不存在局部变量的被动销毁问题,但如果被返回的函数的引用只是作为一个临时变量出现,而没有将其赋值给一个实际的变量,那么就可能造成这个引用所指向的空间(有new分配)无法释放的情况(由于没有具体的变量名,故无法用delete手动释放该内存),从而造成内存泄漏。因此应当避免这种情况的发生

5当返回类成员的引用时,最好是const引用。这样可以避免在无意的情况下破坏该类的成员。

6.可以用函数返回的引用作为赋值表达式中的左值

复制代码
1 #include
2 using namespace std;
3 int value[10];
4 int error=-1;
5 int &func(int n){
6 if(n>=0&&n<=9)
7 return value[n];//返回的引用所绑定的变量一定是全局变量,不能是函数中定义的局部变量
8 else
9 return error;
10 }
11
12 int main(){
13 func(0)=10;
14 func(4)=12;
15 cout<<value[0]<<endl;
16 cout<<value[4]<<endl;
17 return 0;
18 }
复制代码

D.用引用实现多态
在C++中,引用是除了指针外另一个可以产生多态效果的手段。也就是说一个基类的引用可以用来绑定其派生类的实例

class Father;//基类(父类)
class Son:public Father{…}//Son是Father的派生类
Son son;//son是类Son的一个实例
Father &ptr=son;//用派生类的对象初始化基类对象的使用
特别注意:

ptr只能用来访问派生类对象中从基类继承下来的成员。如果基类(类Father)中定义的有虚函数,那么就可以通过在派生类(类Son)中重写这个虚函数来实现类的多态。

三、总结
1.在引用的使用中,单纯给某个变量去别名是毫无意义的,引用的目的主要用于在函数参数的传递中,解决大块数据或对象的传递效率和空间不如意的问题

2.用引用传递函数的参数,能保证参数在传递的过程中不产生副本,从而提高传递效率,同时通过const的使用,还可以保证参数在传递过程中的安全性

3.引用本身是目标变量或对象的别名,对引用的操作本质上就是对目标变量或对象的操作。因此能使用引用时尽量使用引用而非指针

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值