返回的函数向量被归零

编程入门 行业动态 更新时间:2024-10-18 14:15:45
本文介绍了返回的函数向量被归零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下代码是从main调用的函数。 Sence是为了b $ b初始化维度为a的向量。 " B"是相应的值(所有 在向量中相同)。 问题是初始化是以正确的方式完成的(由 输出)但是返回的向量是空的 - 意味着指针是 显示为零。 vector< int> * initialize(int a,vector< int> * vec,int b) { vector< int> ve; vec =& ve; ve.reserve(a); for(int i = 0; i< = a-1; i ++) ve [i] = b; for( i = 0; i< = a-1; i ++) cout<< i<< " ;.价值: << ve [i]<< endl; 返回vec; ve.clear(); } 出了什么问题??? 我之前没有使用指针试过它 - 结果相同!

The following code is a function called from the main. Sence is to initialize a vector of the dimension of "a". "b" is the according value (all the same in a vector). The problem is that the initialization is done the right way (controled by the output) but the returned vector is empty - meaning the pointer is showing zero. vector<int>* initialize (int a, vector<int>* vec, int b) { vector<int> ve; vec=&ve; ve.reserve(a); for (int i=0; i<=a-1; i++) ve[i]=b; for (i=0; i<=a-1; i++) cout << i << ". value : " << ve[i] << endl; return vec; ve.clear(); } What is wrong??? I have tried it before without using a pointer - same result!

推荐答案

Huibuh写道: 以下代码是从main调用的函数。 Sence将初始化a维度的向量。 " B"是相应的值(在向量中都是一样的)。 问题是初始化是以正确的方式完成的(由输出控制)但是返回的向量是空的 - 意思是指针显示为零。 vector< int> * initialize(int a,vector< int> * vec,int b) { vector< int> ve; 对于(int i = 0; i< = a -1; i ++) ve [i] = b; for(i = 0; i< = a-1; i ++) > cout<< i<< " ;.值: << ve [i]<< endl; 返回vec; ve.clear(); } 什么是错了??? 我之前没有使用指针试过它 - 结果相同! The following code is a function called from the main. Sence is to initialize a vector of the dimension of "a". "b" is the according value (all the same in a vector). The problem is that the initialization is done the right way (controled by the output) but the returned vector is empty - meaning the pointer is showing zero. vector<int>* initialize (int a, vector<int>* vec, int b) { vector<int> ve; vec=&ve; ve.reserve(a); for (int i=0; i<=a-1; i++) ve[i]=b; for (i=0; i<=a-1; i++) cout << i << ". value : " << ve[i] << endl; return vec; ve.clear(); } What is wrong??? I have tried it before without using a pointer - same result!

首先,你要返回一个地址当地的。另外,我不确定你用vec做什么,但我几乎可以肯定这不是你想要的。 你想做什么? - Pete

First, you''re returning a address of a local. Also, I''m not sure what you''re trying to do with "vec", but I''m almost sure it isn''t what you want. What are you trying to do? - Pete

Huibuh写道: 以下代码是从main调用的函数。 Sence将初始化a维度的向量。 " B"是相应的值(在向量中都是一样的)。 问题是初始化是以正确的方式完成的(由输出控制)但是返回的向量是空的 - 意思是指针显示为零。 vector< int> * initialize(int a,vector< int> * vec,int b) { vector< int> ve; 对于(int i = 0; i< = a -1; i ++) ve [i] = b; for(i = 0; i< = a-1; i ++) > cout<< i<< " ;.值: << ve [i]<< endl; 返回vec; ve.clear(); } 什么是错了??? 我之前没有使用指针试过它 - 结果相同! The following code is a function called from the main. Sence is to initialize a vector of the dimension of "a". "b" is the according value (all the same in a vector). The problem is that the initialization is done the right way (controled by the output) but the returned vector is empty - meaning the pointer is showing zero. vector<int>* initialize (int a, vector<int>* vec, int b) { vector<int> ve; vec=&ve; ve.reserve(a); for (int i=0; i<=a-1; i++) ve[i]=b; for (i=0; i<=a-1; i++) cout << i << ". value : " << ve[i] << endl; return vec; ve.clear(); } What is wrong??? I have tried it before without using a pointer - same result!

首先,你要返回一个地址当地的。另外,我不确定你用vec做什么,但我几乎可以肯定这不是你想要的。 你想做什么? - Pete

First, you''re returning a address of a local. Also, I''m not sure what you''re trying to do with "vec", but I''m almost sure it isn''t what you want. What are you trying to do? - Pete

2004年4月5日星期一23:07:44 +0200,Huibuh <无***** @ t-online.de>写道: On Mon, 5 Apr 2004 23:07:44 +0200, "Huibuh" <no*****@t-online.de> wrote: 以下代码是从main调用的函数。 Sence将初始化a维度的向量。 " B"是相应的值(所有在矢量中都相同)。 问题是初始化是以正确的方式完成的(由输出控制)但是返回的向量是空的 - 意味着指针显示为零。 vector< int> * initialize(int a,vector< int> * vec,int b) { vector< int> ve; 对于(int i = 0; i< = a -1; i ++) ve [i] = b; for(i = 0; i< = a-1; i ++) > cout<< i<< " ;.值: << ve [i]<< endl; 返回vec; ve.clear(); } 什么是错误??? 你正在你的函数中创建一个完整的局部向量,设置它,然后它会在退出函数时被销毁。怎么样只需 配置本地矢量并使用指针你的功能是 给出: #include< iostream> ; #include< vector> using namespace std; vector< int> * initialize(int a,vector< int> * vec,int b) { // vector< int> ve; // vec =& ve; vec-> reserve(a); > for(int i = 0; i< = a-1; i ++) (* vec)[i] = b; for(i = 0; i< = a-1; i ++) cout<< i<< " ;.价值: << (* vec)[i]<<结束; 返回vec; } int main() { vector< int> x; vector< int> * vecp = initialize(10,& x,1000); cout<< x [0]<< " " << x [1]<< " " << x [2]<< endl; 返回0; } 我之前没有使用指针尝试过 - 结果相同! 不确定你的意思,但不确定是否重要。你/可以/有$ 向量按值返回,但这样效率会相当低。 -leor The following code is a function called from the main. Sence is toinitialize a vector of the dimension of "a". "b" is the according value (allthe same in a vector).The problem is that the initialization is done the right way (controled bythe output) but the returned vector is empty - meaning the pointer isshowing zero. vector<int>* initialize (int a, vector<int>* vec, int b){ vector<int> ve; vec=&ve; ve.reserve(a); for (int i=0; i<=a-1; i++) ve[i]=b; for (i=0; i<=a-1; i++) cout << i << ". value : " << ve[i] << endl; return vec; ve.clear();} What is wrong??? You''re creating an entire local vector in your function, setting it up, and then it gets destroyed upon exit from the function. How about just dispensing with the local vector and using the pointer your function is given: #include <iostream> #include <vector> using namespace std; vector<int>* initialize (int a, vector<int>* vec, int b) { //vector<int> ve; // vec=&ve; vec->reserve(a); for (int i=0; i<=a-1; i++) (*vec)[i]=b; for (i=0; i<=a-1; i++) cout << i << ". value : " << (*vec)[i] << endl; return vec; } int main() { vector<int> x; vector<int> *vecp = initialize(10, &x, 1000); cout << x[0] << " " << x[1] << " " << x[2] << endl; return 0; } I have tried it before without using a pointer - same result! Not sure what you mean, but not sure it matters. You /could/ have the vector be returned by value, but that would be rather inefficient. -leor

- Leor Zolman --- BD软件--- www.bdsoft C / C ++,Java,Perl和Unix的现场培训 C ++用户:下载BD软件''免费的STL错误消息解密器: www.bdsoft/ tools / stlfilt.html

更多推荐

返回的函数向量被归零

本文发布于:2023-11-13 02:12:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583130.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:向量   函数   被归零

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!