vector传参

豆子DXS 2018-12-06 02:29:29
vector作为函数参数返回,在函数体把vector作数组,在主函数中调用此函数,发现vector的值没有传过来。有大神可以举例分析一下吗?第一次写代码,很多不懂,网上找资料没找到想要的回答。
...全文
1108 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
penghuahuijuan 2018-12-11
  • 打赏
  • 举报
回复
直接传递参数为形参,会另外分配内存空间,相当于一个单独的变量。
要改变原始参数的值,需要引用或者指针访问原始参数。
豆子DXS 2018-12-11
  • 打赏
  • 举报
回复
@啃馒头
我是属于看书看了忘,就只能逼自己写程序改bug学习,但是就是很多基础知识不懂,到处碰壁,都不知道怎么学编程,请前辈给些学编程的意见
啃馒头 2018-12-10
  • 打赏
  • 举报
回复
形参 ,是单向传递啊,编程的基本原理啊 ,不能返回参数的,我建议你,设置个全局变量,在函数中返回。
或者设置非安全模式,用指针的方式,这样也可以。
返回参数变量 还一个很不好的编程习惯
要改掉
豆子DXS 2018-12-09
  • 打赏
  • 举报
回复
谢谢大家帮我,就是vector使用错误造成,成局部变量后就传不出去了,我再改改代码
_ZGq 2018-12-09
  • 打赏
  • 举报
回复

vector<int> func();

这样不行吗?
(如果想快一点,用
vector<int>& func();
传引用的方式)
qq_33316323 2018-12-08
  • 打赏
  • 举报
回复
引用 8 楼 豆子DXS 的回复:
void defect( Mat cable, float gradient_size, float pixel_num, float mindet_area, int sec_graythreshold, int *defect_num, vector<vector<double>>&detect_matrix)
{。。。。。。。。。。。。。。。。。。
if (*defect_num == 0)
{
vector<vector<double>> detect_matrix(1, vector<double>(1,0.0));
}
else
//if (*defect_num != 0.0)
{
vector<vector<double>> detect_matrix(*defect_num, vector<double>(5,0.0));
for (cable_idx_0 = 0; cable_idx_0 <*defect_num; cable_idx_0++)
{
。。。。。。。。。。。。。。。。。。
}
}
detect_matrix.clear();
}

这样用是不是导致我的detect_matrix数据在其他.cpp文件中根本调用不到

vector<vector<double>> detect_matrix(1, vector<double>(1,0.0));
这样你的detect_matrix就被局部变量detect_matrix覆盖了。不能多次使用构造函数,你可以自己往里面装,或者查一下vector的api。
豆子DXS 2018-12-08
  • 打赏
  • 举报
回复
void defect( Mat cable, float gradient_size, float pixel_num, float mindet_area, int sec_graythreshold, int *defect_num, vector<vector<double>>&detect_matrix)
{。。。。。。。。。。。。。。。。。。
if (*defect_num == 0)
{
vector<vector<double>> detect_matrix(1, vector<double>(1,0.0));
}
else
//if (*defect_num != 0.0)
{
vector<vector<double>> detect_matrix(*defect_num, vector<double>(5,0.0));
for (cable_idx_0 = 0; cable_idx_0 <*defect_num; cable_idx_0++)
{
。。。。。。。。。。。。。。。。。。
}
}
detect_matrix.clear();
}

这样用是不是导致我的detect_matrix数据在其他.cpp文件中根本调用不到
  • 打赏
  • 举报
回复
引用 6 楼 豆子DXS 的回复:
@zjq9931 我就是跟踪的数据,最底层的detect_matrix中的数据就是没有传出来,vector<vector<double>> detect_matrix(*defect_num, vector<double>(5)); 可以这样多次声明类型并改变容器大小吗?
多次声明的是不同的变量。是局部变量。不是传进来的参数。问题就在这里了。
豆子DXS 2018-12-06
  • 打赏
  • 举报
回复
@zjq9931
我就是跟踪的数据,最底层的detect_matrix中的数据就是没有传出来,vector<vector<double>> detect_matrix(*defect_num, vector<double>(5)); 可以这样多次声明类型并改变容器大小吗?
@风轻云淡_ 2018-12-06
  • 打赏
  • 举报
回复
引用 1 楼 zjq9931 的回复:
函数的参数要用引用
比如:
声明:
void foo(vector<int> &abc);

实现:
void foo(vector<int> &abc)
{
abc[0]=1;
}
这样就可以了。
莫要误人子弟啊
  • 打赏
  • 举报
回复
引用 1 楼 豆子DXS 的回复:
@zjq9931 void defect( ,,,,,,,,,,,vector<vector<double>>&detect_matrix)//最底层函数 { .................................... if (epoint_num < 3 && abnormal_point_num < 3) { vector<vector<double>> detect_matrix(1, vector<double>(1)); ............................................ } else {......................... vector<vector<double>> detect_matrix(*defect_num, vector<double>(5)); ........................................... } } void cable_get(,,,,,, vector<vector<double>>&detect_matrix)//第2层 { ................................. defect(,,,,,,, detect_matrix); .............................. } int main()主函数 {............................. cable_get( ,,,,detect_matrix); ............................... } 我这样使用vector是不是有问题,我第2层函数cable_get读取不到最底层defect函数传过来的detect_matrix
单从这里看是没有问题的,你要跟踪看看
豆子DXS 2018-12-06
  • 打赏
  • 举报
回复
@zjq9931
void defect( ,,,,,,,,,,,vector<vector<double>>&detect_matrix)//最底层函数
{ ....................................
if (epoint_num < 3 && abnormal_point_num < 3)
{
vector<vector<double>> detect_matrix(1, vector<double>(1));
............................................
}
else
{.........................
vector<vector<double>> detect_matrix(*defect_num, vector<double>(5));
...........................................
}
}
void cable_get(,,,,,, vector<vector<double>>&detect_matrix)//第2层
{ .................................
defect(,,,,,,, detect_matrix);
..............................
}
int main()主函数
{.............................
cable_get( ,,,,detect_matrix);
...............................
}
我这样使用vector是不是有问题,我第2层函数cable_get读取不到最底层defect函数传过来的detect_matrix
AlbertS 2018-12-06
  • 打赏
  • 举报
回复
引用 1 楼 zjq9931 的回复:
函数的参数要用引用
比如:
声明:
void foo(vector<int> &abc);

实现:
void foo(vector<int> &abc)
{
abc[0]=1;
}
这样就可以了。
  • 打赏
  • 举报
回复
函数的参数要用引用 比如: 声明: void foo(vector<int> &abc); 实现: void foo(vector<int> &abc) { abc[0]=1; } 这样就可以了。

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧