std::list/vector 作为函数参数的传递方式
来源:互联网 发布:淘宝上哪家卖真皮皮带 编辑:程序博客网 时间:2024/05/03 06:40
项目中的一个小bug。主观性认为 std::list 作为函数参数,是按指针传递的,实际还是按值传递的(不解!!)。 举例如下
1 #include <iostream>
2 #include <list>
3
4 using namespace std;
7 /*version 1: transfer pointer of list */
8 /*
9 void update(list<int>* b){
10
11 for(list<int>::iterator piter = b->begin(); piter != b->end(); piter++)
12 {
13 (*piter)++;
14 }
15 }
16 */
17
18 /*version2: transfer list */
19 /*void update(list<int> b){
20
21 for(list<int>::iterator piter = b.begin(); piter != b.end(); piter++)
22 {
23 (*piter)++;
24 }
25 }
26 */
27
28 /*version 3:transfer reference of list*/
29void update(list<int>& b){
30
31 for(list<int>::iterator piter = b.begin(); piter != b.end(); piter++)
32 {
33 (*piter)++;
34 }
35 }
39
40int main()
41 {
42 list<int> a(4,100);
43 // list<int>* b = &a;
44 update(a);
45
46 for(list<int>::iterator piter=a.begin(); piter != a.end(); piter++)
47 {
48 cout << "updated list " << *piter << endl;
49 }
50 }
上述 update函数的参数设计为 list<Int>, 结果是 main()中 调用 update(a)之后, a 的值 并没有被修改;设计成 list<int>* ,才能修改main中的a值。
同理,将list换成vector,发现vector也是按值传递的。这跟潜意识里面想法,“数组名作为参数,按指针/引用传递” 全然不同。
设计修改main中变量的子函数,需要指针/引用传参;否则就用传值。另外,std list/vector类,确实同基本类型(int, float)规则一致。
- std::list/vector 作为函数参数的传递方式
- 函数作为参数的传递
- cocos2dx 函数作为参数进行传递和调用(std::function跟CC_CALLBACK_1的使用)
- vector作为参数的三种传参方式
- Vector作为参数的三种传参方式
- 将vector作为参数传递
- 数组作为参数传递的方式
- 函数作为参数传递
- vector作为参数传递的几种情况
- 数组作为函数参数的传递
- 数组作为函数的参数传递
- 数组作为函数参数的传递方法
- 数组作为函数的参数传递
- 指针作为函数参数传递的问题
- 将成员函数作为std::for_each的第三个参数
- 函数的参数传递方式
- 函数参数的传递方式
- 函数参数的传递方式
- http://weibo.com/u/5258668114/home?wvr=5&topnav=1
- 啊额头与热忱uibonecxxcyt
- 用RedHat的packstack快速安装OpenStack环境 [云计算 OpenStack]
- 华为云计算FusionSphere V100R003C10 解决方案描述 [云计算 华为云计算]
- [Leetcode] Binary Tree Zigzag Level Order Traversal
- std::list/vector 作为函数参数的传递方式
- 撒旦阿斯达看吧没收到否v表面上下次课现在变成V迷不v
- linux如何复制文件夹
- 啊实打实大阿斯顿发送到发给
- 个人网页中必不可少的十五大Web API
- PHP5下fckeditor的上传中文名出错的解决方法
- php技术生成静态页面的实现
- PHP中使用FCKeditor2.4.3配置
- freebsd下防止arp欺骗的软件