你如何归还清单?最佳做法 [英] How do you return a list? Best practices

查看:47
本文介绍了你如何归还清单?最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这可能听起来很奇怪,但我想知道如何从函数中返回

数据列表。这些是我知道的一些方法,而且我想知道你通常使用哪种方法。这是一个最佳实践问题而不是技术问题。


1)返回一个列表实例,即

std :: list myFunction(){

std :: list list();

返回列表;

}


然而,这会进行大量复制并且效率不高,但似乎概念上最简单的是



2)返回列表指针(或引用)ie

std :: list * my Function(){

std :: list * list = malloc(sizeof(std :: list)) ;

返回列表;

}


这只需要复制指针,但现在必须删除此列表。如果

我们传递了一个成员变量(或全局变量)的地址,那么我们

不需要删除,但要确保它不会改变在调用者之前

完成使用它。


3)返回一个迭代器,即

std :: list :: iterator myFunction(){

std :: list list;

return list.begin();

}


这几乎没有复制,但我们也需要知道list.end()或者

来自另一种方法。


4)通行证

中的列表void myFunction(std :: list& list){

//填充列表中的内容

}


再一次复制,听起来不错。

每个都有利弊但这可能听起来像一个愚蠢的问题,但是它们是b
方法是最可取的。从API的角度来看?例如,如果我为其他开发人员编写API,那么这将是最好的吗?


非常感谢

安德鲁


PS这个问题的答案是否有任何不同,如果它是一套,

矢量或地图?

Hi,
This may sound a odd question, but I wanted to know how you return a list of
data from a function. These are some of the ways I know how, and I was
wondering which method you normally use. This is more of a best practices
question rather than a technical one.

1) Return a list instance ie
std::list myFunction() {
std::list list();
return list;
}

This however does a lot of copying and isn''t very efficient, but seems
conceptually the easiest.

2) Return a list pointer (or reference) ie
std::list * my Function() {
std::list *list = malloc(sizeof(std::list));
return list;
}

This only needs to copy a pointer, however this list must now be deleted. If
we passed the address of say a member variable (or global variable) then we
don''t need to delete, but make sure it doesn''t change before the caller is
finished using it.

3) Return a iterator ie,
std::list::iterator myFunction() {
std::list list;
return list.begin();
}

This has little copying again, but we also need to know the list.end() maybe
from another method.

4) Pass the list in
void myFunction(std::list &list) {
// Fill the list with stuff
}

Again little copying, sounds a good idea.
Each has pros and cons but this may sound like a silly question, but which
method is most "advisable" from a API point of view? For example if I was
writing a API for other developers to use, which would be the best?

Thanks very much
Andrew

P.S Would the answer to this question be any different if it was a set,
vector, or map?

推荐答案

* Andrew Brampton:
* Andrew Brampton:

这可能听起来很奇怪,但我想知道你是如何返回一个列表的
来自函数的数据。这些是我知道的一些方法,我想知道你通常使用哪种方法。这是一个最佳实践问题而不是技术问题。

1)返回一个列表实例,即
std :: list myFunction(){


假设你的意思是,例如std :: list< int> ;.

std :: list list();


这是一个函数''list''的声明,返回''std :: list''。


返回列表;
}



使用编译器进行NRVO优化(上面提到的错误

固定)是最好的。许多编译器现在都在做NRVO。


然而这需要很多复制而且不是很有效


使用NRVO它是'实际上效率最高。


,但在概念上看似最简单。


那也是。

2)返回一个列表指针(或引用),即
std :: list * my Function(){
std :: list * list = malloc(sizeof(std :: list));
返回列表;
}


不要使用malloc在C ++中,使用''new''。


这只需要复制指针,但现在必须删除此列表。如果我们通过说成员变量(或全局变量)的地址,那么我们不需要删除,但要确保在调用者之前没有更改
完成使用它。


使用std :: auto_ptr<的std ::列表< T> > ;.


3)返回一个迭代器,即
std :: list :: iterator myFunction(){
std :: list list;
return list.begin();
}


那是非常糟糕的:你正在返回一个指向局部变量的指针,并且有

未定义的行为。

这几乎没有复制,但我们还需要知道list.end()或者
来自另一种方法。

4)将列表传递给
void myFunction(std :: list& list){
//填写列表中的内容
}

再次进行小复制,听起来不错。


这实际上是NRVO优化所做的,除了优化

避免原始初始化。

每个都有专业人士但这可能听起来像一个愚蠢的问题,但哪种方法最可取。从API的角度来看?例如,如果我为其他开发人员编写API,那将是最好的?


第一个,或第一个加上最后一个(第一个作为

的包装器,最后都提供给客户端代码)。 />
PS如果是一个集合,
向量或地图,这个问题的答案是否会有所不同?
Hi,
This may sound a odd question, but I wanted to know how you return a list of
data from a function. These are some of the ways I know how, and I was
wondering which method you normally use. This is more of a best practices
question rather than a technical one.

1) Return a list instance ie
std::list myFunction() {
Assuming you mean e.g. std::list<int>.

std::list list();
That''s a declaration of a function ''list'' returning a ''std::list''.

return list;
}

With a compiler that does NRVO optimization this (with errors mentioned above
fixed) is the best. Many compilers now do NRVO.

This however does a lot of copying and isn''t very efficient
With NRVO it''s actually the most efficient.

, but seems conceptually the easiest.
That also.
2) Return a list pointer (or reference) ie
std::list * my Function() {
std::list *list = malloc(sizeof(std::list));
return list;
}
Don''t use malloc in C++, use ''new''.

This only needs to copy a pointer, however this list must now be deleted. If
we passed the address of say a member variable (or global variable) then we
don''t need to delete, but make sure it doesn''t change before the caller is
finished using it.
Use a std::auto_ptr< std::list<T> >.

3) Return a iterator ie,
std::list::iterator myFunction() {
std::list list;
return list.begin();
}
That''s VERY BAD: you''re returning a pointer to a local variable, and have
Undefined Behavior.
This has little copying again, but we also need to know the list.end() maybe
from another method.

4) Pass the list in
void myFunction(std::list &list) {
// Fill the list with stuff
}

Again little copying, sounds a good idea.
That''s essentially what an NRVO optimization does, except the optimization
avoid the original initialization.
Each has pros and cons but this may sound like a silly question, but which
method is most "advisable" from a API point of view? For example if I was
writing a API for other developers to use, which would be the best?
The first, or the first plus the last (with the first as a wrapper for the
last, both offered to the client code).
P.S Would the answer to this question be any different if it was a set,
vector, or map?




No.


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么会这样是坏事吗?

A:热门发布。

问:usenet和电子邮件中最烦人的是什么?



No.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?




Andrew Brampton写道:

Andrew Brampton wrote:

这可能听起来很奇怪,但我想知道你如何返回一个清单
来自函数的数据。这些是我知道的一些方法,我想知道你通常使用哪种方法。这是一个最佳实践问题,而不是技术问题。
Hi,
This may sound a odd question, but I wanted to know how you return a list of
data from a function. These are some of the ways I know how, and I was
wondering which method you normally use. This is more of a best practices
question rather than a technical one.




一种方法是使用侵入式列表。草图:


模板< class T>

struct list_node

{

list_node * next;

T值;

};


模板< class T>

void delete_list(list_node< ; T> *节点)

{

if(node)

{

delete_list(node->下一步);

删除节点;

}

}


list_node< whatever> * create_my_list ()

{

list_node< whatever> * head = new list_node< whatever>();

//将其他节点附加到头部 - >下一个

返回头;

}



One way to do this is to use intrusive lists. Sketch:

template<class T>
struct list_node
{
list_node* next;
T value;
};

template<class T>
void delete_list(list_node<T>* node)
{
if(node)
{
delete_list(node->next);
delete node;
}
}

list_node<whatever>* create_my_list()
{
list_node<whatever>* head = new list_node<whatever>();
// attach other nodes to head->next
return head;
}


当我想要返回一个集合时,我通常这样做:


模板< typename InIterator>

void GetCollection(InIterator it)

{

//返回1,2,3

* it ++ = 1;

* it ++ = 2;

* it ++ = 3;

}


并将其称为:

std :: list< int> myList;

GetCollection(std :: back_inserter(myList));


-

Dag Henriksson

When I want to return a collection, I usually do it like this:

template<typename InIterator>
void GetCollection(InIterator it)
{
// return 1, 2, 3
*it++ = 1;
*it++ = 2;
* it++ = 3;
}

And call it like this:
std::list<int> myList;
GetCollection(std::back_inserter(myList));

--
Dag Henriksson


这篇关于你如何归还清单?最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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