返回的函数向量被归零 [英] returned vector of function is zeroized

查看:84
本文介绍了返回的函数向量被归零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码是从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 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???
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.com

C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD软件''免费的STL错误消息解密器:
www.bdsoft.com/ tools / stlfilt.html


这篇关于返回的函数向量被归零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆