如何在c ++中执行此操作 [英] How to do this in c++

查看:74
本文介绍了如何在c ++中执行此操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我过去经常使用c ++,但最近我一直在使用java和pythons

而且我发现要想到最好的方法很难了

C ++中的东西


想象一下,我想要一个返回list的函数。所选用户来自

a的大用户列表,或基于某些

选择的数据库或其他东西。


在python我可能会使用一个生成器函数,每次调用它时都会产生下一个

相关用户。在java中,我可能会返回一个

迭代器,它迭代通过所选用户可能使用内部类的

实例来实现迭代的迭代器

通过我想要的项目。


我不太清楚如何用C ++最好地做到这一点?


C ++迭代器与java迭代器是完全不同的东西

似乎根本没有定义一种新类型的迭代器,只有

迭代虽然选中项目。


我可以创建一个新列表并按值返回,但这似乎有点低效,所以至少可以说。将任何类型的引用

返回到现有列表似乎充满了内存管理问题

如何创建和删除内存。


在C ++中执行此操作的最佳方法是什么?我希望这有点意义......

I used to use c++ a lot but recently I''ve been using java and pythons
and I''m finding it a lot harder to think of the best way to do
something in C++

Imagine I want a function that returns a "list" of selected users from
a big list of users, or a database or something based on some
selection.

In python I might use a generator function which yielded the next
relevent user each time it was called. In java I''d probably return an
Iterator which iterated through the selected users probably using an
instance of an inner class to implement the iterator that just iterated
through the items I wanted.

It''s not so clear how I''d best do this in C++?

C++ iterators are totally different things to java iterators and it
doesn''t seem at all elegent to define a new type of iterator which only
iterates though selected items.

I could create a new list and return that by value but that seems
somewhat inefficient so say the least. Returning any kind of reference
to an existing list seems fraught with memory management issues over
how to create and delete the memory.

What''s the best way to do this in C++? I hope this makes some sense...

推荐答案



jo ********* @ jbmail.com 写道:
我以前经常使用c ++但是最近我一直在使用java和pythons
而且我发现想要用C ++做最好的方法更难了

想象一下想要一个返回列表的函数。所选用户来自
一个大的用户列表,或数据库或基于某些选择的东西。

在python中我可能会使用一个生成器函数,它产生了下一个
每次调用时相关用户。在java中我可能会返回一个
迭代器,它迭代通过所选用户可能使用内部类的实例来实现迭代器,它只是通过我想要的项目迭代。 br />
我不太清楚如何用C ++最好地做到这一点?

C ++迭代器与java迭代器完全不同,它不会似乎总是要定义一种新类型的迭代器,它只会迭代选择的项目。

我可以创建一个新的列表并按值返回但是这似乎是
有点低效,所以至少说。将任何类型的引用返回到现有列表似乎充满了关于如何创建和删除内存的内存管理问题。

执行此操作的最佳方法是什么在C ++中?我希望这有点道理......
I used to use c++ a lot but recently I''ve been using java and pythons
and I''m finding it a lot harder to think of the best way to do
something in C++

Imagine I want a function that returns a "list" of selected users from
a big list of users, or a database or something based on some
selection.

In python I might use a generator function which yielded the next
relevent user each time it was called. In java I''d probably return an
Iterator which iterated through the selected users probably using an
instance of an inner class to implement the iterator that just iterated
through the items I wanted.

It''s not so clear how I''d best do this in C++?

C++ iterators are totally different things to java iterators and it
doesn''t seem at all elegent to define a new type of iterator which only
iterates though selected items.

I could create a new list and return that by value but that seems
somewhat inefficient so say the least. Returning any kind of reference
to an existing list seems fraught with memory management issues over
how to create and delete the memory.

What''s the best way to do this in C++? I hope this makes some sense...




你有什么大清单?作为?如何返回指向相关对象的

指针列表?


Mike



What do you have the "big list" as ? how about returning a list of
pointers to the relevant objects?

Mike


29 2005年9月03:17:50 -0700, jo ********* @ jbmail.com 写道:
On 29 Sep 2005 03:17:50 -0700, jo*********@jbmail.com wrote:
我可以创建一个新的列表并按值返回,但这似乎有点低效,所以至少说。将任何类型的引用返回到现有列表似乎充满了内存管理问题,而不是如何创建和删除内存。
I could create a new list and return that by value but that seems
somewhat inefficient so say the least. Returning any kind of reference
to an existing list seems fraught with memory management issues over
how to create and delete the memory.




使用std ::矢量<>让它为你管理记忆。创建一个

空列表或向量,并将一个非const引用传递给你的

函数,该函数从数据库或文件中选择名称等,或者

无论如何,让函数填满向量。你是对的

按值返回向量或列表很可能太低了

效率低。


std :: list如果你需要

来插入和删除大量对象,那么std :: map可能比vector更有效。 std :: vector更适合

顺序或随机访问。


并返回对本地分配的任何对象的引用

函数(即不是用new创建,而是基于堆栈或自动的

变量,无论你想要什么,都是一个很大的禁忌,因为
$ b当函数返回时,$ b内存不再存在...因此无效

参考。


-

Bob Hairgrove
No**********@Home.com


* jo*********@jbmail.com
我过去经常使用c ++,但最近我一直在使用java和pythons
而且我发现想到最好的东西要困难得多用C ++做的事情

想象一下,我想要一个返回list的函数。所选用户来自
一个大的用户列表,或数据库或基于某些选择的东西。

在python中我可能会使用一个生成器函数,它产生了下一个
每次调用时相关用户。在java中我可能会返回一个
迭代器,它迭代通过所选用户可能使用内部类的实例来实现迭代器,它只是通过我想要的项目迭代。 br />
我不太清楚如何用C ++最好地做到这一点?
I used to use c++ a lot but recently I''ve been using java and pythons
and I''m finding it a lot harder to think of the best way to do
something in C++

Imagine I want a function that returns a "list" of selected users from
a big list of users, or a database or something based on some
selection.

In python I might use a generator function which yielded the next
relevent user each time it was called. In java I''d probably return an
Iterator which iterated through the selected users probably using an
instance of an inner class to implement the iterator that just iterated
through the items I wanted.

It''s not so clear how I''d best do this in C++?




这个问题的根源不在于迭代器(是的,他们在C ++中很尴尬)

但是对于你返回的对象有生命周期和复制问题。


要解决这个问题,请使用智能指针一个动态分配的对象 -

基本上就是你在Java和Python中所拥有的。


最简单的智能指针是std :: auto_ptr,这只是转移

所有权,也许这对你来说已经足够了。否则,请考虑

,例如提高:: shared_ptr的。为此你必须安装升级库。


-

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

问:为什么这么糟糕?

A:热门发布。

问:usenet上最烦人的事情是什么并在电子邮件中?



The root of this problem is not with iterators (yes, they are awkward in C++)
but with lifetime and copying issues for the object you return.

To solve that use a smart-pointer to a dynamically allocated object -- which
is essentially what you have in Java and Python.

The simplest kind of smart-pointer is std::auto_ptr, which just transfers
ownership, and perhaps that will be sufficient for you. Otherwise, consider
e.g. boost::shared_ptr. For that you''ll have to install the boost library.

--
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?


这篇关于如何在c ++中执行此操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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