从函数中分配对返回值的引用。 [英] assigning reference to a returned value from function.

查看:51
本文介绍了从函数中分配对返回值的引用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,


我需要从函数返回一个向量。向量是int并且它

可以包含多达1000个元素。早些时候我在做什么

//功能定义

vector< intretIntVector()

{

vector< intvecInt ;

/ *

//用int值填充向量。

* /

... < br $> b $ b ....

返回vecInt;


}

//结束函数定义

int main()

{

vector< intvect_int = retIntVector();

...

...

返回1;

}

//程序结束

但是我诱惑使用函数返回的向量引用

增强性能

类似


vector< int& vect_int = retIntVector();


但不知怎的,我无法说服自己使用这种方法,因为

这种情况​​下没有vect_int的具体对象请参考。

我想问你后面的方法是否正确?

我在msvc 6上运行它并且它给了我很好的结果..

谢谢和问候,

Yogesh Joshi

解决方案

yp ********* @ indiatimes.com 写道:


你好所有,


我需要从函数返回一个向量。向量是int,它

可以包含多达1000个元素。早些时候我正在做

//函数定义

vector< intretIntVector()

{

vector< intvecInt;

/ *

//用int值填充向量。

* /

..

...

返回vecInt;
< br $>
}

//结束函数定义

int main()

{

vector< intvect_int = retIntVector();

..

..

返回1;

}

//程序结束

但我试图使用函数返回的向量引用来

增强性能

喜欢


vector< int& vect_int = retIntVector();



您引用的向量在哪里?如果它是

retIntVector中的局部变量,你就是在寻找麻烦。


为什么不去简单高效的东西并传入一个引用

vector?


void retIntVector(vector< int>&);


-

Ian Collins。


你引用的矢量在哪里?如果它是
中的局部变量


retIntVector你要求麻烦。



如代码所示...函数返回的向量是

函数的本地。
< blockquote class =post_quotes>
为什么不去简单有效的东西并传入对

向量的引用?


void retIntVector (vector< int>&);


-



此功能已经构建和测试,情况如下好的

因为我没有它的源代码..


谢谢和问候,

Yogesh Joshi


yp ********* @ indiatimes.com 写道:


Hello All,


我需要从函数返回一个向量这个向量是int的,它可以包含多达1000个元素。早些时候我正在做

//函数定义

vector< intretIntVector()

{

vector< intvecInt;

/ *

//用int值填充向量。

* /

..

...

返回vecInt;


}

//结束函数定义

int main()

{

vector< intvect_int = retIntVector();

..

..

返回1;

}

//程序结束

但我试图使用函数返回的向量引用来提升性能

就像


vector< int& vect_int = retIntVector();


但不知怎的,我无法说服自己使用这种方法,因为

这种情况​​下没有vect_int的具体对象请参考。

我想问你后面的方法是否正确?

我在msvc 6上运行它并且它给了我很好的结果..


谢谢和问候,

Yogesh Joshi



你的vect_int会指向垃圾!

也许你可以创建一个类?


class MyVecInt:public vector< int>

{

public:

MyVecInt()

{

//用值填充我

}

};



main()

{

MyVecInt vec_int;

}


Hello All,

I need to return a vector from a function.The vector is of int and it
can contain as many as 1000 elements.Earlier I was doing
//function definition
vector<intretIntVector()
{
vector<intvecInt;
/*
//Populate the vector with int values.
*/
...
....
return vecInt;

}
//end function Definition
int main()
{
vector<intvect_int = retIntVector();
...
...
return 1;
}
//Program End
But I tempt to use a reference to vector returned by the function to
enhance the performance
like

vector<int& vect_int =retIntVector();

But somehow I could not convince myself over using this approach as in
this case there is no concrete object to which the vect_int refer.
I want to ask you whether the later approach is correct or not?
I ran it on msvc 6 and it gave me fine results..
Thanks and Regards,
Yogesh Joshi

解决方案

yp*********@indiatimes.com wrote:

Hello All,

I need to return a vector from a function.The vector is of int and it
can contain as many as 1000 elements.Earlier I was doing
//function definition
vector<intretIntVector()
{
vector<intvecInt;
/*
//Populate the vector with int values.
*/
..
...
return vecInt;

}
//end function Definition
int main()
{
vector<intvect_int = retIntVector();
..
..
return 1;
}
//Program End
But I tempt to use a reference to vector returned by the function to
enhance the performance
like

vector<int& vect_int =retIntVector();

Where is the vector you reference? If it''s a local variable in
retIntVector you are asking for trouble.

Why not to the simple and efficient thing and pass in a reference to the
vector?

void retIntVector( vector<int>& );

--
Ian Collins.


Where is the vector you reference? If it''s a local variable in

retIntVector you are asking for trouble.

As shown in the code..the vector which is returned by the function is
local to the function.

Why not to the simple and efficient thing and pass in a reference to the
vector?

void retIntVector( vector<int>& );

--

This function is already built and tested and the situation is as good
as i don''t have its source code..

Thanks and Regards,
Yogesh Joshi


yp*********@indiatimes.com wrote:

Hello All,

I need to return a vector from a function.The vector is of int and it
can contain as many as 1000 elements.Earlier I was doing
//function definition
vector<intretIntVector()
{
vector<intvecInt;
/*
//Populate the vector with int values.
*/
..
...
return vecInt;

}
//end function Definition
int main()
{
vector<intvect_int = retIntVector();
..
..
return 1;
}
//Program End
But I tempt to use a reference to vector returned by the function to
enhance the performance
like

vector<int& vect_int =retIntVector();

But somehow I could not convince myself over using this approach as in
this case there is no concrete object to which the vect_int refer.
I want to ask you whether the later approach is correct or not?
I ran it on msvc 6 and it gave me fine results..
Thanks and Regards,
Yogesh Joshi

Your vect_int will point to garbage!
Maybe you could create a class?

class MyVecInt : public vector<int>
{
public:
MyVecInt()
{
// Populate me with values
}
};
and
main()
{
MyVecInt vec_int;
}


这篇关于从函数中分配对返回值的引用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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