首页 > 编程语言 >C++快速入门 第十二讲:传值、传址和传引用

C++快速入门 第十二讲:传值、传址和传引用

时间:2023-08-18 14:22:47浏览次数:58  
标签:std 传址 cout int age C++ swap main 传值

实例1:值传递

 1 #include<iostream>
 2 
 3 void changeAge(int age,int newAge); 
 4 int main()
 5 {
 6     int age = 24;//定义一个age,占一处地址 
 7     std::cout << "My age is " << age <<"\n";
 8     
 9     changeAge(age,age + 1);
10     
11     std::cout << "Now my age is " << age << "\n";
12     
13     return 0;
14 }
15 
16 void changeAge(int age,int newAge)//再定义一个age,占另一处地址 
17 {
18     age = newAge;
19     std::cout << "In this, my age is " << age << "\n"; 
20 }

绕开“值传递”问题的第一种方法是向函数传递变量的地址取代他的值。

实例2:指针地址传递

 1 #include<iostream>
 2 
 3 void changeAge(int *age,int newAge); 
 4 int main()
 5 {
 6     int age = 24;//定义一个age,占一处地址 
 7     std::cout << "My age is " << age <<"\n";
 8     
 9     changeAge(&age,age + 1);
10     
11     std::cout << "Now my age is " << age << "\n";
12     
13     return 0;
14 }
15 
16 void changeAge(int *age,int newAge)//再定义一个age,占另一处地址 
17 {
18     *age = newAge;
19     std::cout << "In this, my age is " << *age << "\n"; 
20 }

实例3:两值互换

 1 #include<iostream>
 2 
 3 void swap(int *x,int *y);
 4 int main()
 5 {
 6     int x,y;
 7     
 8     std::cout << "请输入两个不同的值:";
 9     std::cin >> x >> y;
10     
11     swap(&x,&y);
12     
13     std::cout << "调换后输出:" << x << ' ' << y <<"\n\n"; 
14     
15 }
16 
17 void swap(int *x,int *y)
18 {
19     int temp;
20     temp = *x;
21     *x = *y;
22     *y = temp;
23 }

实例4:两值互换2

 1 #include<iostream>
 2 
 3 void swap(int *x,int *y);
 4 int main()
 5 {
 6     int x,y;
 7     
 8     std::cout << "请输入两个不同的值:";
 9     std::cin >> x >> y;
10     
11     swap(&x,&y);
12     
13     std::cout << "调换后输出:" << x << ' ' << y <<"\n\n"; 
14     
15 }
16 
17 void swap(int *x,int *y)
18 {
19     *x ^= *y;
20     *y = *x;
21     *x ^= *y;
22 }

实例5:不用指针的两值交换

 1 #include <iostream> 
 2 
 3 void swap(int &x,int &y);
 4 
 5 int main()
 6 {
 7     int x,y;
 8     std::cout << "请输入两个不同的值:";
 9     std::cin >> x >> y;
10     
11     swap(x,y);
12     std::cout << "调换后输出:" << x << ' ' << y << "\n\n";
13     
14     return 0;
15 }
16 
17 void swap(int &x,int &y)
18 {
19     int temp;
20     temp = x;
21     x = y;
22     y = temp;
23 }

标签:std,传址,cout,int,age,C++,swap,main,传值
From: https://www.cnblogs.com/ybqjymy/p/17640371.html

相关文章

  • C++快速入门 第二讲:从一个小程序说起
    cout(cout<<i表示变量i流向屏幕显示)是一个输出流对象,属于basic_ostream类的对象。ostream类在iostream头文件中定义。同理cin(回车后,键盘输入缓冲区内容流向cin流的内部缓冲区,cin>>xx操作便从这个缓冲区提取数据,即键盘输入流向程序)为输入流对象,C++标准库所使用的所有标识符(即类......
  • C++快速入门 第三讲:输入输出方法
    实例1:忽略输入字符串的前面部分字符输出1#include<iostream>//23usingnamespacestd;//名字空间45intmain()6{7charbuf[20];//只能存放19个字符,因为字符串以0结尾89cin.ignore(7);//忽略输入的前七个字符10cin.g......
  • C++快速入门 第四讲:文件操作
    ifream与ofream分别为文件读取类和文件写入类实例1:文件读取(读取同一文件夹的test.txt文件内容)1#include<fstream>//涉及到了文件流操作2#include<iostream>34usingnamespacestd;56intmain()//in输入:读out输出:写7{8ifstreamin;//......
  • C++快速入门 第五讲:输入输出小结
    实例1:根据输入内容输出1#include<iostream>2usingnamespacestd;//名字空间3intmain()4{5charanswer;67cout<<"请问可以格式化您的硬盘吗?!【Y/N】"<<"\n";8cin>>answer;910switch(answer......
  • C++ save vector or float to bin
    voidsave_bin(std::vector<float>&data_vector,std::stringname="mnn.bin"){std::ofstreamoutFile(name,std::ios::out|std::ios::binary);......
  • C++里std::enable_shared_from_this是干什么用的?
    std::enable_shared_from_this使用场景在很多场合,经常会遇到一种情况,如何安全的获取对象的this指针,一般来说我们不建议直接返回this指针,可以想象下有这么一种情况,返回的this指针保存在外部一个局部/全局变量,当对象已经被析构了,但是外部变量并不知道指针指向的对象已经被析构了,如......
  • 1. C++入门及简单程序结构
    1.C++入门及简单程序结构一,编写一个简单的C++程序#include<iostream>usingnamespacestd;intmain(){ return0;}二,基础语法变量1.变量的概念变量本质上是一个装东西的盒子,并且只能存放一个值。2.变量的定义变量必须先定义,才可以使用。inta=5;3.变量......
  • 1.C++入门以及简单顺序结构
    C++入门以及简单顺序结构一、编写一个简单的C++程序#include<iostream>#include<cstdio>usingnamespacestd;intmain(){return0;}二、基础语法变量1.变量的概念变量本质上是一个装东西的盒子,并且只能存放一个值。2.变量的定义变量必须先定义,才可以......
  • 1.C++入门以及简单顺序结构
    C++入门以及简单顺序结构一、编写一个简单的C++程序#include<iostream>usingnamespacestd;intmain(){ return0;}二、基础语法变量1.变量的概念变量本质上是一个装东西的盒子,并且只能存放一个值。2.变量的定义变量必须先定义,才可以使用。inta=5;......
  • 各省数字贸易指数数据计算(peek获取与next传值的使用)
    需求:工作中需要计算各省数字贸易指数数据,需要首先利用peek获取栈顶元素,然后通过n.next进行顶端元素传值,最后利用综合指数法来进行合一计算和存储,用于后续的深度数据挖掘。解决:classNode(object):definit(self,val):self.val=val#指向元素的值self.next=NoneclassSt......