从 C# 到 C++,处理容器
其实我这里有一个设计问题。它非常简单,但要点是:
我有一个 C++ 类,其中有一个声明为私有成员的 STL 向量。但该类的客户端需要迭代该向量。
在 C# 中,我们有一个非常方便的语句,即 Yield,在这种情况下,您编写一个返回 IEnumerable 的函数,它会“产生”您一种迭代该类内的私有容器的好方法。
我只是想为 C++ 找到一个优雅的解决方案,而不是使用像 GetValue(int idx) 这样的方法。
有什么建议吗?
示例:
class Fat
{
public:
Fat();
// some code here ...
private:
void LoadSectors(SECT startPoint);
std::vector<SECT>sectors;
};
class Storage
{
public:
Storage(string CompoundFile);
//For example, this method will receive a ref to my fat system and iterate over
//the fat array in order to read every sector.
LoadStrem(Fat& fat);
};
这是一个非常简单的示例。
Actually, I have a design question here. Its very simple but the point is:
I have one C++ class that has a STL vector declared as a private member. But the clients of that class need to iterate over this vector.
In C# we have a very handy statement, the Yield, that in cases like that, you write a function returning an IEnumerable and it "yields" you a nice way to iterate over a private container inside that class.
I'm just trying to find an elegant solution for C++, instead of using methods like GetValue(int idx).
Any suggestions?
Example:
class Fat
{
public:
Fat();
// some code here ...
private:
void LoadSectors(SECT startPoint);
std::vector<SECT>sectors;
};
class Storage
{
public:
Storage(string CompoundFile);
//For example, this method will receive a ref to my fat system and iterate over
//the fat array in order to read every sector.
LoadStrem(Fat& fat);
};
This is far simple example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C++ 中没有类似于 C# 中的
yield
的语法糖。如果你想创建一个类,其实例应该像普通 STL 集合一样可迭代,那么你必须为你的类实现一个迭代器,在你的类型上将其公开为::iterator
,并提供begin()
和end()
成员函数。There's no syntactic sugar in C++ analogous to
yield
in C#. If you want to create a class, instances of which should be iterable in the same way stock STL collections are, then you have to implement an iterator for your class, expose it as::iterator
on your type, and providebegin()
andend()
member functions.您可以创建一个返回向量引用(或最好是常量引用)的访问器函数,也可以创建返回向量的
begin()
和end()
访问器函数适当的向量迭代器。You can either create an accessor function which returns a reference (or preferably const reference) to the vector, or you can create
begin()
andend()
accessor functions which return the appropriate vector iterators.当您需要发布类的内部结构时,它总是很痛苦...
您可以通过提供像 stl 那样的算法来解决它:在对象的接口上提供一个
foreach
函数。这样,课程保持“封闭”状态,但您拥有所需的灵活性。您还可以添加一个
copy
函数,也许还可以添加一个transform
;这些是我最经常需要的。It always hurts when you need to publish the innards of your class...
You may be able to solve it by providing algorithms as the stl does: provide a
foreach
function on the object's interface.This way, the class remains 'closed', but you have the flexibility you need. You can also add a
copy
function, and maybe atransform
; these are the ones I most frequently need.让你的类公开迭代器。
然后周围的代码可以通过一对迭代器自由遍历向量的元素。
Let your class expose iterators.
Then the surrounding code can traverse the elements of the vector freely, through just a pair of iterators.