如何从函数返回std :: vector? [英] how to return std::vector from function?

查看:170
本文介绍了如何从函数返回std :: vector?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c ++用户


假设我构建了一个大数组并将它放在

函数中的std :: vector中,现在我想要将它返回到调用函数的位置。

我可以这样做:


std :: vector< int> fun(){

//构建向量v;

返回v;

}


int main(){

std :: vector< int> a = fun();

返回0;

}


工作正常。但是,我相信fun()中有一个深层副本,所以当数组很大时,

的成本很高。现在我试着返回没有

深拷贝的指针。


std :: vector< int> * fun(){

//构建一个指向vector的指针;

return * v;

}


int main(){

std :: vector< int> * a = fun();

返回0;

}


这会在fun()中出现内存泄漏问题,因为v将在返回后被删除

因此返回的指针将无效。


无论如何我都有可以从fun()获得正确的指针?我也认为
想要使用智能指针,例如boost :: shared_ptr:


boost :: shared_ptr<的std ::矢量< INT> > getV(){

boost :: shared_ptr<的std ::矢量< INT> > v;

v-> push_back(1);

v-> push_back(2);

return v;

}


int main(){

boost :: shared_ptr<的std ::矢量< INT> > v = getV();

std :: cout<< v-> at(0);

返回0;

}


运行程序时收到以下消息:

/usr/include/boost/shared_ptr.hpp:253:T * boost :: shared_ptr< T> :: operator->()const [with T = std :: vector< int ,std :: allocator< int> >]:断言'px!= 0''失败。

中止


我怎样才能正确?当getV()返回

shared_ptr时是否有任何深拷贝?


非常感谢。


zl2k

解决方案

zl2k写道:

c ++用户

假设我构建了一个大型数组并将它放在一个
函数中的std :: vector中,现在我想将它返回到调用函数的位置。
我可以这样做:

std :: vector< int> fun(){
//构建向量v;
返回v;
}
int main(){
std :: vector< int> ; a = fun();
返回0;
}

它工作正常。但是,我相信有一个很深的副本在fun(),


为什么?

所以当阵列很大时成本很高。现在我试着返回一个指针
而没有深层复制。

std :: vector< int> * fun(){
//构建一个指向vector的指针;
return * v;
}
int main(){
std :: vector< int> * a = fun();
返回0;
}

这会在fun()中出现内存泄漏问题,因为v将在返回后被删除
因此返回的指针将无效。


您的描述是正确的,虽然这不是内存泄漏。

无论如何我可以从fun()获得正确的指针?我还想过要使用智能指针,比如boost :: shared_ptr:

boost :: shared_ptr<的std ::矢量< INT> > getV(){
boost :: shared_ptr<的std ::矢量< INT> > v;
v-> push_back(1);
v-> push_back(2);
返回v;
}

int main (){
boost :: shared_ptr<的std ::矢量< INT> > v = getV();
std :: cout<< v-> at(0);
返回0;
}

运行程序时收到以下消息:
/ usr / include / boost / shared_ptr.hpp:253:T *
boost :: shared_ptr< T> :: operator->()const [with T = std :: vector< int,
std :: allocator< int> >]:断言`px!= 0''失败。中止

我怎样才能正确理解?当getV()返回shared_ptr时是否有任何深拷贝?




而不是返回一个向量,你的函数可以简单地取一个

参考。


void fun(std :: vector< int>& v){

//填充向量v; < br $>
}


int main(){

std :: vector< int> a;

fun(a);

返回0;

}

> std :: vector< int> * fun(){

//构建一个指向vector的指针;
return * v;


你的意思是


返回& v;




}

int main(){
std :: vector< int> * a = fun();
返回0;
}

这会在fun()中出现内存泄漏问题,因为v将在返回后被删除
因此返回的指针将无效。


这不是内存泄漏,你有一个指向堆栈中对象的指针

'不再存在了。

无论如何,我可以从fun()获得正确的指针吗?




你可以做这样的事情......


std :: vector< int> * fun()

{

std :: vector< int> * v = new std :: vector< int>();

// build * v;

return v;

}


然后确保调用者删除返回的值。


或者你可以做这样的事情......

void fun(std :: vector< int>& v)

{

// build v;

}


int main()

{

std :: vector< int> x;

fun(x);

}


-Brian


zl2k写道:

假设我构建了一个大数组并将它放在st / :: vector中的
函数中,现在我想返回它回到调用函数的位置。
我可以这样做:

std :: vector< int> fun(){
//构建向量v;
返回v;
}
int main(){
std :: vector< int> ; a = fun();
返回0;
}

它工作正常。


这确实是正确的方法。


但是,我相信fun()中有一个深层副本,所以当阵列很大时,成本很高。


如果编译器足够智能,就不会有任何副本。

有众所周知的优化返回值以防止

不必要的副本。


现在我试着返回没有
深拷贝的指针。




使用这里的指针只会让它变得混乱。


hi, c++ user

Suppose I constructed a large array and put it in the std::vector in a
function and now I want to return it back to where the function is called.
I can do like this:

std::vector<int> fun(){
//build the vector v;
return v;
}

int main(){
std::vector<int> a = fun();
return 0;
}

It works fine. However, I believe there is a deep copy in fun(), so the
cost is big when the array is big. Now I tried to return a pointer without
deep copy.

std::vector<int>* fun(){
//build a pointer to vector;
return *v;
}

int main(){
std::vector<int>* a = fun();
return 0;
}

This got memory leak trouble in fun() since v will be deleted after return
thus the pointer returned will be invalid.

Is there anyway that I can get a correct pointer from fun()? I also
thought to use a smart pointer, such as boost::shared_ptr:

boost::shared_ptr< std::vector<int> > getV(){
boost::shared_ptr< std::vector<int> > v;
v->push_back(1);
v->push_back(2);
return v;
}

int main(){
boost::shared_ptr< std::vector<int> > v = getV();
std::cout << v->at(0);
return 0;
}

I got the following message when run the program:
/usr/include/boost/shared_ptr.hpp:253: T* boost::shared_ptr<T>::operator->() const [with T = std::vector<int, std::allocator<int> >]: Assertion `px != 0'' failed.
Aborted

How can I get it correct? Is there any deep copy when the getV() return
the shared_ptr?

Thanks a lot.

zl2k

解决方案

zl2k wrote:

hi, c++ user

Suppose I constructed a large array and put it in the std::vector in a
function and now I want to return it back to where the function is called.
I can do like this:

std::vector<int> fun(){
//build the vector v;
return v;
}

int main(){
std::vector<int> a = fun();
return 0;
}

It works fine. However, I believe there is a deep copy in fun(),
Why?
so the cost is big when the array is big. Now I tried to return a pointer
without deep copy.

std::vector<int>* fun(){
//build a pointer to vector;
return *v;
}

int main(){
std::vector<int>* a = fun();
return 0;
}

This got memory leak trouble in fun() since v will be deleted after return
thus the pointer returned will be invalid.
Your description is correct, though that is not a memory leak.
Is there anyway that I can get a correct pointer from fun()? I also
thought to use a smart pointer, such as boost::shared_ptr:

boost::shared_ptr< std::vector<int> > getV(){
boost::shared_ptr< std::vector<int> > v;
v->push_back(1);
v->push_back(2);
return v;
}

int main(){
boost::shared_ptr< std::vector<int> > v = getV();
std::cout << v->at(0);
return 0;
}

I got the following message when run the program:
/usr/include/boost/shared_ptr.hpp:253: T*
boost::shared_ptr<T>::operator->() const [with T = std::vector<int,
std::allocator<int> >]: Assertion `px != 0'' failed. Aborted

How can I get it correct? Is there any deep copy when the getV() return
the shared_ptr?



Instead of returning a vector, your function could simply take one by
reference.

void fun(std::vector<int>& v){
//fill the vector v;
}

int main(){
std::vector<int> a;
fun(a);
return 0;
}


> std::vector<int>* fun(){

//build a pointer to vector;
return *v;
do you mean

return &v;

?
}

int main(){
std::vector<int>* a = fun();
return 0;
}

This got memory leak trouble in fun() since v will be deleted after return
thus the pointer returned will be invalid.
It''s not a memory leak, you have a pointer to an object on the stack
that''s not there anymore.
Is there anyway that I can get a correct pointer from fun()?



you could do something like this...

std::vector<int>* fun()
{
std::vector<int> * v = new std::vector<int>();
//build *v;
return v;
}

and then make sure the caller deletes the returned value.

or you could do something like this...

void fun( std::vector<int> & v )
{
// build v;
}

int main()
{
std::vector<int> x;
fun(x);
}

-Brian


zl2k wrote :

Suppose I constructed a large array and put it in the std::vector in a
function and now I want to return it back to where the function is called.
I can do like this:

std::vector<int> fun(){
//build the vector v;
return v;
}

int main(){
std::vector<int> a = fun();
return 0;
}

It works fine.
This is indeed the correct way to do it.

However, I believe there is a deep copy in fun(), so the
cost is big when the array is big.
There won''t be any copy if the compiler is smart enough.
There are well known optimizations for return values to prevent
unnecessary copies.

Now I tried to return a pointer without
deep copy.



Using pointers here will only make it messy.


这篇关于如何从函数返回std :: vector?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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