如何用凡科做自己的网站,换物网站为什么做不起来,钦州市建设工程质量监督站网站,企业网站怎么优化文章目录 C 引用的应用1. 修改函数中传递的参数2. 避免复制大型结构3. for 循环中修改所有对象4. for 循环中避免复制对象 References vs Pointers引用的限制使用引用的优点练习Quesition 1Question 2Question 3Question 4Question 5Question 6 如果一个变量被声明为引用#… 文章目录 C 引用的应用1. 修改函数中传递的参数2. 避免复制大型结构3. for 循环中修改所有对象4. for 循环中避免复制对象 References vs Pointers引用的限制使用引用的优点练习Quesition 1Question 2Question 3Question 4Question 5Question 6 如果一个变量被声明为引用那它就成了一个已有变量的别名。一个变量可以通过在声明的时候加 而成为引用。 此外还可以将引用变量定义为一种类型它可以作为另一个变量的引用。 用于表示变量或任何内存的地址。与引用变量关联的变量既可以通过变量名访问也可以通过与之关联的引用变量访问。
语法
data_type ref variable;例子:
// C Program to demonstrate
// use of references
#include iostream
using namespace std;int main()
{int x 10;// ref is a reference to x.int ref x;// Value of x is now changed to 20ref 20;cout x x \n;// Value of x is now changed to 30x 30;cout ref ref \n;return 0;
}输出
x 20
ref 30C 引用的应用
C中的引用有很多的应用如下是其中的一些
修改函数中传递的参数避免复制大型结构for 循环中修改所有对象for 循环中避免复制对象
1. 修改函数中传递的参数
如果一个函数接收到了一个变量的引用它可以修改这个变量的值。如下程序变量是使用引用交换的。
// C Program to demonstrate
// Passing of references as parameters
#include iostream
using namespace std;// Function having parameters as
// references
void swap(int first, int second)
{int temp first;first second;second temp;
}// Driver function
int main()
{// Variables declaredint a 2, b 3;// function calledswap(a, b);// changes can be seen// printing both variablescout a b;return 0;
}输出
3 22. 避免复制大型结构
想象一下一个函数必须接收一个大对象。如果我们没有使用引用来传递那么它的一个新副本就会被创建这回导致CPU时间和内存的浪费。可以使用引用来避免这种情况。
struct Student {string name;string address;int rollNo;
}// If we remove in below function, a new
// copy of the student object is created.
// We use const to avoid accidental updates
// in the function as the purpose of the function
// is to print s only.
void print(const Student s)
{cout s.name s.address s.rollNo \n;
}3. for 循环中修改所有对象
我们可以在每个循环中使用引用来修改所有元素。
// C Program for changing the
// values of elements while traversing
// using references
#include iostream
#include vectorusing namespace std;// Driver code
int main()
{vectorint vect{ 10, 20, 30, 40 };// We can modify elements if we// use referencefor (int x : vect) {x x 5;}// Printing elementsfor (int x : vect) {cout x ;}cout \n;return 0;
}输出
15 25 35 45 4. for 循环中避免复制对象
我们可以在每个循环中使用引用以避免在对象很大时复制单个对象。
// C Program to use references
// For Each Loop to avoid the
// copy of objects
#include iostream
#include vectorusing namespace std;// Driver code
int main()
{// Declaring vectorvectorstring vect{ geeksforgeeks practice,geeksforgeeks write,geeksforgeeks ide };// We avoid copy of the whole string// object by using reference.for (const auto x : vect) {cout x \n;}return 0;
}输出
geeksforgeeks practice
geeksforgeeks write
geeksforgeeks ideReferences vs Pointers
引用和指针都可以用于在一个函数中修改另一个函数的局部变量。当作为参数传递给函数或从函数返回时它们都可以用来避免复制大对象从而提高效率。尽管有上述相似之处引用和指针之间还是有以下区别。
指针可以被声明为 void但引用不可以int a 10;
void* aa a; // it is valid
void ar a; // it is not valid指针变量有n层/多层的间接即单指针、双指针、三指针。然而引用变量只有一个间接层次。下面的代码揭示了上述几点:// C Program to demonstrate
// references and pointers
#include iostream
using namespace std;// Driver Code
int main()
{// simple or ordinary variable.int i 10;// single pointerint* p i;// double pointerint** pt p;// triple pointerint*** ptr pt;// All the above pointers differ in the value they store// or point to.cout i i \t p p \t pt pt \t ptr ptr \n;// simple or ordinary variableint a 5;int S a;int S0 S;int S1 S0;// All the references do not differ in their// values as they all refer to the same variable.cout a a \t S S \t S0 S0 \t S1 S1 \n;return 0;
}输出i 10 p 0x7ffecfe7c07c pt 0x7ffecfe7c080 ptr 0x7ffecfe7c088
a 5 S 5 S0 5 S1 5引用变量不能更新引用变量是一个内部指针引用变量的声明前面有符号(但不要将其读作“address of”)。
引用的限制
一个引用一旦被创建它就不能再引用另一个对象它不能被重置。这通常使用指针完成。引用不能是NULL。指针经常用 NULL 来表示它没有指向任何有效的东西。引用必须在声明的时候初始化而指针没有该限制。
由于上面的限制C中的引用不能用于实现如链表、树等数据结构。Java 中引用没有上述限制可以用于实现所有的数据结构。Java不需要指针的主要原因是引用功能更强大。
使用引用的优点
更安全由于引用必须初始化所以像野指针这样的野引用不太可能存在。但仍然有可能存在不指向有效位置的引用见下面练习中的第5和6题使用更方便引用不需要解引用运算符来访问值。它们可以像普通变量一样使用。 运算符只有在声明的时候需要。此外对象引用的成员可以通过点运算符. 访问而不像指针需要箭头运算符- 才能访问成员。
除了上述原因还有一些地方如拷贝构造函数不能使用指针。在拷贝构造函数中必须使用引用传递实参类似地重载某些运算符如时必须使用引用。
练习
Quesition 1
#include iostream
using namespace std;int fun()
{static int x 10;return x;
}int main()
{fun() 30;cout fun();return 0;
}输出
30Question 2
#include iostream
using namespace std;int fun(int x) { return x; }int main()
{cout fun(10);return 0;
}输出
./3337ee98-ae6e-4792-8128-7c879288221f.cpp: In function int main():
./3337ee98-ae6e-4792-8128-7c879288221f.cpp:8:19: error: invalid initialization of non-const reference of type int from an rvalue of type intcout fun(10);^
./3337ee98-ae6e-4792-8128-7c879288221f.cpp:4:5: note: in passing argument 1 of int fun(int)int fun(int x) { return x; }Question 3
#include iostream
using namespace std;void swap(char* str1, char* str2)
{char* temp str1;str1 str2;str2 temp;
}int main()
{char* str1 GEEKS;char* str2 FOR GEEKS;swap(str1, str2);cout str1 is str1 \n;cout str2 is str2 \n;return 0;
}输出
str1 is FOR GEEKS
str2 is GEEKSQuestion 4
#include iostream
using namespace std;int main()
{int x 10;int* ptr x;int* ptr1 ptr;
}输出
./18074365-ebdc-4b13-81f2-cfc42bb4b035.cpp: In function int main():
./18074365-ebdc-4b13-81f2-cfc42bb4b035.cpp:8:11: error: cannot declare pointer to intint* ptr1 ptr;Question 5
#include iostream
using namespace std;int main()
{int* ptr NULL;int ref *ptr;cout ref \n;
}输出
timeout: the monitored command dumped core
/bin/bash: line 1: 34 Segmentation fault timeout 15s ./372da97e-346c-4594-990f-14edda1f5021 372da97e-346c-4594-990f-14edda1f5021.inQuestion 6
#include iostream
using namespace std;int fun()
{int x 10;return x;
}int main()
{fun() 30;cout fun();return 0;
}输出
0