返回STL列表作为参数 [英] Returning STL lists as argument

查看:113
本文介绍了返回STL列表作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数从日志文件中读取行,将这些行转换为某个类,并返回此类的实例的STL列表。

I have a function that reads lines from a log file, converts these lines to a certain class and returns a STL list of instances of this class.

如何

在不失一般性的情况下,假设:

Without loss of generality, assume:

list<Request> requests = log_manipulator.getAsRequestList();

如何声明 getAsRequestList()?我应该返回一个列表的引用还是只返回一个列表?

How should I declare getAsRequestList()? Should I return a reference to a list or just return a list?

这是一个严重的问题,因为在这个特定的赋值中,列表将包含大约1.5M的元素,因此

This is a serious issue because in this particular assignment the lists will contain circa 1.5M elements, and thus a mistake like that can screw up memory usage.

推荐答案

返回引用是不可取的,返回列表对象会导致复制。最好是将方法的签名更改为接受并填充列表引用:

Returning a reference is not advisable, and returning the list object would cause copying. Best would be to change the method's signature to accept and populate a list reference:

list<Request> requests;
log_manipulator.getRequestListByRef(requests);

void getRequestListByRef(list< Request>&)作为方法的签名。

这篇关于返回STL列表作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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