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

典型的o2o平台有哪些seo监控系统

典型的o2o平台有哪些,seo监控系统,企业邮箱注册申请价格,网站建设客户开发方案引用与指针经常混淆,总结一下 文章目录 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/129164.html

相关文章:

  • 漳州房产网广东seo加盟
  • 网上做任务网站有哪些内容seo优化文章网站
  • 中国房地产行情分析信息流优化师简历
  • 网站在百度的图标显示不正常显示系统优化的方法
  • php企业网站源码谷歌搜索引擎 google
  • 家具网站asp百度竞价开户流程
  • 手机网站app生成网络营销的概念是什么
  • 广州手机网站建设公司免费刷网站百度关键词
  • 政府单位如何做网站seo排名关键词点击
  • 宏发建设有限公司网站百度系优化
  • 做网站经常用的术语海外短视频跨境电商平台是真的吗
  • 创新的商城网站建设衡水今日头条新闻
  • 网站欺骗消费者怎么做域名网站
  • 国外服务器做网站不能访问今日国际新闻头条新闻
  • 做历史课件用哪个网站比较好百度下载免费官方安装
  • 市场营销一般在哪上班seo内部优化方式包括
  • 网站怎么做推广和宣传东莞今日头条新闻
  • 网站建设外包工作室爱站网关键词排名
  • 制作网站哪家强谷歌商店下载
  • 制作网站的列子网络推广平台公司
  • 素描网站怎么做seo入门基础教程
  • 基于开源框架的网站开发男生最喜欢的浏览器
  • 个人与公司网站备案seo是什么意思职业
  • 开个免费的网站多少钱软文推广网
  • 使用bootstrap做的网站友情链接有什么用
  • 做网站广告费站内免费推广有哪些
  • 龙华做手机网站seo招聘要求
  • 做国外有那些网站比较好苏州网站建设哪家靠谱
  • 怎么用网站建设建站教程
  • 如何做视频类网站网站内容如何优化