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

怎么申请网站域名好的推广平台

怎么申请网站域名,好的推广平台,网站设计策划书 模板,咋样做网站引用与指针经常混淆,总结一下 文章目录 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/65772.html

相关文章:

  • 手机做ppt的免费模板下载网站中国网站排名查询
  • 做网站买一个域名多少钱沧州网站优化
  • 如何在旅游网站上做攻略信息流优化师职业规划
  • 网站建设的6个基本步骤制作网页的步骤
  • 滁州做网站第三方营销平台有哪些
  • 在北京做网站制作一个月多少钱广告投放网
  • 青秀区网站建设新网店怎么免费推广
  • 网站建设公司行业现状哪里可以学seo课程
  • 兰州新区规划建设局网站凡科网免费建站
  • 如何制作自己的视频网站3步打造seo推广方案
  • 静安网站建设内容营销平台有哪些
  • 手机版网站设计案例营销软文怎么写
  • 价目表海报app制作站群seo技巧
  • 杭州市滨江区建设局网站深企在线
  • 网站 黑白推广方案格式模板范文
  • 电子元器件网站怎么做营销软文500字范文
  • 移动端网站推广万网域名注册流程
  • 宣传片策划拍摄制作公司关键词查询优化
  • 常州专业网站建设公司咨询武汉seo优化代理
  • 框架布局技术制作一个网站网络网站
  • 营销型企业网站建设教案站长之家最新网站
  • 网站建设原创苏州整站优化
  • 百度糯米做网站多少钱个人网页在线制作
  • 网站开发与网站建设搜索 引擎优化
  • 网站商城建设需求表深圳龙岗区优化防控措施
  • 采集做网站中国软文网
  • 如何用服务器做网站百度提交网址多久才会收录
  • 建设银行信用卡中心网站首页seo文章
  • wordpress.appseo搜索引擎优化
  • 网站建设和优化排名seo岗位培训