vector装变量地址作为函数引用参数的问题

函数引用参数会改变原始变量。

比如:

#include <iostream>
#include <vector>
using namespace std;

void change(int &ve){
    ve=2;
}

int main(){
    int ve=0;
    change(ve);
    cout<<ve<<endl;
}

输出:

2

使用vector装变量地址(vector<type*> x)作为函数引用参数时不能改变原始vector,可能是局部变量造成的原因

#include <iostream>
#include <vector>
using namespace std;
void change(vector<int*> &ve){
    int a=1,b=2,c=3,d=4;
    ve.push_back(&a);
    ve.push_back(&b);
    ve.push_back(&c);
    ve.push_back(&d);
}

int main(){
    vector<int*> ve;
    change(ve);
    for(int i;i<ve.size();i++){
        cout<<*ve[i]<<endl; 
    }
    return 0;
}

输出:

1
32585
2
0

这是因为函数change中的局部变量a,b,c,d是局部变量,经过只有在定义的函数正在执行时,局部变量才存在,这称为局部变量的生存期。当函数开始时,它的形参变量和它定义的任何局部变量都将在内存中创建,当函数结束时,它们被销毁。这意味着存储在函数形参或局部变量中的任何值在调用不同函数之后都会丢失。若使用全局变量就不会有这样的问题;

#include <iostream>
#include <vector>
using namespace std;
int a=1,b=2,c=3,d=4;
void change(vector<int*> &ve){
    ve.push_back(&a);
    ve.push_back(&b);
    ve.push_back(&c);
    ve.push_back(&d);
}

int main(){
    vector<int*> ve;
    change(ve);
    for(int i;i<ve.size();i++){
        cout<<*ve[i]<<endl;
    }
    return 0;
}

输出:

1
2
3
4

指针传递也是一样的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如qsort 等函数需要函数指针才能回调 用此函数库可以将成员函数指针转为普通函数指针 测试代码如下 #include <stdio.h> #include <algorithm> #include <vector> #include <string> #include <iostream> #include <math.h> using cmpfunc = int(__cdecl*)(const void*, const void*); using DebugArrayFunc = void(__stdcall *)(std::string &out;); #include "thunk.h" class MySort { public: int Rule; MySort(int a):Rule(a){} // 回调函数 template<typename T> int __cdecl sort(const void* a, const void* b); }; class Test { public: std::vector<int> mm; void Sort(int (*comp)(const void *,const void *)) { return qsort(mm._Myfirst,mm.size(),sizeof(int),comp); } void Entry(DebugArrayFunc func) { std::string string; cmpfunc comp; TemplateThunk athunk; // 正序 comp = (cmpfunc)athunk.GetCall(&MySort;::sort<int>, &MySort;(0)); Sort(comp); func(string); std::cout << string << std::endl; // 逆序 comp = (cmpfunc)athunk.GetCall(&MySort;::sort<int>, &MySort;(1)); Sort(comp); func(string); std::cout << string << std::endl; } }; class CallBack { public: std::vector<int> *pthis; CallBack(std::vector<int> *ff):pthis(ff){} void __stdcall DebugArray(std::string &out;) { char buff[100]; char *aa = buff; for each (auto a in *pthis) { aa += sprintf(aa, "%d ", a); } out.assign(buff); } }; void main() { TemplateThunk athunk; Test tt; tt.mm = { 1, 3, 7, 8, 5, 6, 4, 2, 3, 10 }; tt.Entry(athunk.GetCall(&CallBack;::DebugArray,&CallBack;(&tt;.mm))); } template <typename T> int __cdecl MySort::sort(const void* a, const void* b) { return Rule ? *static_cast<const T*>(a)-*static_cast<const T*>(b) : *static_cast<const T*>(b)-*static_cast<const T*>(a); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值