当前位置: 首页 > wzjs >正文

装饰公司加盟手机优化大师怎么退款

装饰公司加盟,手机优化大师怎么退款,wordpress定时发布插件,百度智能小程序怎么优化排名引用与指针经常混淆,总结一下 文章目录 1. 引用与指针的区别2. 引用传递数组3. 通过引用传递容器和类4. 多线程传递容器时用 std:: ref 替代引用传递 1. 引用与指针的区别 引用(Reference):引用是变量的别名,本质上不…

引用与指针经常混淆,总结一下

文章目录

    • 1. 引用与指针的区别
    • 2. 引用传递数组
    • 3. 通过引用传递容器和类
    • 4. 多线程传递容器时用 std:: ref 替代引用传递

1. 引用与指针的区别

  • 引用(Reference):引用是变量的别名,本质上不是一个变量,而是给一个已经存在的变量起的一个别名。
    • 本质:引用就是变量的别名,不占用新的内存空间。
  • 指针(Pointer):指针是一个变量,存放的是另一个变量的内存地址,需要通过解引用 * 操作符访问该地址的内容。
    • 本质:指针就是存放地址的变量。
int a = 10;
int* p = &a; // p存储的是a的地址
cout << *p;   // 解引用,输出a的值
int a = 10;
int& b = a; // b是a的别名
b = 20;
cout << a; // 输出20

内存结构:

变量地址内容
a0x100020
b0x100020
  • b 只是 a 的别名,它们共享同一个内存空间,所以修改 b 就是修改 a。

  • 引用一旦绑定,就不能再改,而指针则不是

int a = 10;
int b = 20;
int& r = a; // r是a的别名
r = b;      // ❌ 不是修改引用,而是给a赋值
  • 引用不占用额外内存,它就是变量的别名。

2. 引用传递数组

  • 通过引用可以传递整个数组,避免数组退化成指针。

数组的数组名(array name)本质上是数组首元素的地址,所以当数组传参时,传递的实际上是数组首元素的地址,而不是整个数组。

void func(int* arr);
void func(int arr[]); // 与前面等价

二者等价,因为数组会自动退化为指针。

例子:通过指针传递一维数组

#include <iostream>
using namespace std;void modifyArray(int* arr, int size) {for(int i = 0; i < size; i++) {arr[i] += 10;}
}int main() {int numbers[] = {1, 2, 3, 4, 5};int size = sizeof(numbers) / sizeof(numbers[0]);modifyArray(numbers, size);for(int i = 0; i < size; i++) {cout << numbers[i] << " ";}return 0;
}
  • 传入的是数组首元素的地址,即 int* arr 接收的是 &numbers[0] 的地址

通过引用传递一维数组

#include <iostream>
using namespace std;void modifyArray(int (&arr)[5]) {for(int i = 0; i < 5; i++) {arr[i] += 10;}
}int main() {int numbers[] = {1, 2, 3, 4, 5};modifyArray(numbers);for(int i = 0; i < 5; i++) {cout << numbers[i] << " ";}return 0;
}
  • int (&arr)[5] 表示引用一个长度为5的数组。
  • 传入时,整个数组的内存地址传递过来,并且保留数组大小。

通过指针传递二维数组的例子:

#include <iostream>
using namespace std;void printArray(int (*arr)[4], int rows) {for(int i = 0; i < rows; i++) {for(int j = 0; j < 4; j++) {cout << arr[i][j] << " ";}cout << endl;}
}int main() {int numbers[3][4] = {{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12}};printArray(numbers, 3);return 0;
}

通过引用传递二维数组的例子:

#include <iostream>
using namespace std;void printArray(int (&arr)[3][4]) {for(int i = 0; i < 3; i++) {for(int j = 0; j < 4; j++) {cout << arr[i][j] << " ";}cout << endl;}
}int main() {int numbers[3][4] = {{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12}};printArray(numbers);return 0;
}

3. 通过引用传递容器和类

如果传递类对象(class),也应该使用引用传递,否则会拷贝整个对象,非常消耗资源。

#include <iostream>
using namespace std;// ✅ 定义一个 Person 类
class Person {
public:string name;int age;Person(string name, int age) {this->name = name;this->age = age;}void display() {cout << "Name: " << name << ", Age: " << age << endl;}
};// ✅ 通过引用传递类对象
void modifyPerson(Person& p) {p.age += 10;p.name = "Mr. " + p.name;
}int main() {Person person("John", 25);// 传递引用,避免拷贝对象modifyPerson(person);// 输出修改后的信息person.display();return 0;
}

对于一些容器,可以通过引用传递,避免拷贝且可以修改容器中内容。

#include <iostream>
#include <vector>
using namespace std;// ✅ 通过引用传递 vector
void modifyVector(vector<int>& vec) {for(int& val : vec) {val *= 2;}
}int main() {vector<int> numbers = {1, 2, 3, 4, 5};// 传递 vector 的引用modifyVector(numbers);// 打印修改后的 vectorfor(int val : numbers) {cout << val << " ";}return 0;
}

4. 多线程传递容器时用 std:: ref 替代引用传递

C++ STL 容器 (如 vector、list) 在多线程或函数包装中,默认是按值传递。

#include <iostream>
#include <vector>
#include <thread>
using namespace std;void modifyVector(vector<int>& vec) {for (auto& v : vec) {v *= 2;}
}int main() {vector<int> nums = {1, 2, 3, 4, 5};// 将函数和容器传入线程thread t(modifyVector, nums);  // ❌ 出问题!t.join();// 输出结果for (auto v : nums) {cout << v << " ";}return 0;
}
  • 虽然 modifyVector 的参数是 vector&,但是 std::thread 默认是按值传递!
  • thread 会拷贝一份 nums,导致无法修改原容器。

C++ 提供了一个工具 std::ref,专门用于“强制引用传递”,防止容器被拷贝。

#include <iostream>
#include <vector>
#include <thread>
#include <functional>  // 包含 std::ref
using namespace std;void modifyVector(vector<int>& vec) {for (auto& v : vec) {v *= 2;}
}int main() {vector<int> nums = {1, 2, 3, 4, 5};// ✅ 使用 std::ref,强制引用传递thread t(modifyVector, std::ref(nums));t.join();// 输出结果for (auto v : nums) {cout << v << " ";}return 0;
}

在 C++ 中,以下场景:

  • std::thread
  • std::function
  • std::bind
  • std::async
  • std::packaged_task

都默认按值传递参数。如果你传入容器、类、数组、函数等复杂对象,会直接拷贝副本,而不是传引用,需要用 std::ref

http://www.dtcms.com/wzjs/137893.html

相关文章:

  • 做网站带微好吗如何快速推广一个新产品
  • 淄博市疫情风险等级北京seo公司公司
  • 上海搬家公司报价厦门seo
  • wordpress企业网站模板巢湖seo推广
  • 成都网站建设与网站推广培训接推广app任务的平台
  • 汉中北京网站建设seo关键词排名优化制作
  • wordpress插件vpn湖南seo优化服务
  • 广州公共资源建设工程交易中心网站深圳全网营销型网站
  • 在易语言里面做网站行业关键词分类
  • 网站建设需要用到哪些技术网络优化的意义
  • 有创意的婚纱网站模板网站域名怎么注册
  • 深圳快速seo女生seo专员很难吗为什么
  • 武汉云优化网站建设百度在线问答
  • 温岭做网站的公司有哪些网站联盟推广
  • 从什么网站找做app的代码seo推广哪家好
  • 旅游网站开发意义百度推广价格表
  • 设计对网站的重要性服务器ip域名解析
  • 想开一个做网站的公司seo排名查询
  • 网站建设运营成本苏州seo排名公司
  • 3c渠道网宁波网站快速优化
  • 制作一个视频网站网络推广平台有哪些公司
  • 网站建设年终总结怎么写seo任务
  • 做营销网站哪家好谷歌浏览器app下载
  • 网站做强制解析推广哪个app最挣钱
  • 值得做的网站推广优化网站排名教程
  • 学做甜点的网站今天特大军事新闻
  • 网站编辑的栏目怎么做跨境电商培训机构哪个靠谱
  • 做好中心网站建设工作总结海淀区seo多少钱
  • 花茶网站模板媒介
  • 网站产品二级分类百度平台营销收费标准