从 C# 到 C++,处理容器

发布于 2024-08-12 23:00:01 字数 713 浏览 1 评论 0原文

其实我这里有一个设计问题。它非常简单,但要点是:

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤寂小茶 2024-08-19 23:00:01

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 provide begin() and end() member functions.

你另情深 2024-08-19 23:00:01

您可以创建一个返回向量引用(或最好是常量引用)的访问器函数,也可以创建返回向量的 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() and end() accessor functions which return the appropriate vector iterators.

爱本泡沫多脆弱 2024-08-19 23:00:01

当您需要发布类的内部结构时,它总是很痛苦...

您可以通过提供像 stl 那样的算法来解决它:在对象的接口上提供一个 foreach 函数。

class S { 
   std::vector<int> v;

public:
   //... and some methods to populate the vector

   template< typename F > F& foreach( F& f ) { 
      return std::for_each( v.begin(), v.end(), f );
   }

};

这样,课程保持“封闭”状态,但您拥有所需的灵活性。您还可以添加一个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.

class S { 
   std::vector<int> v;

public:
   //... and some methods to populate the vector

   template< typename F > F& foreach( F& f ) { 
      return std::for_each( v.begin(), v.end(), f );
   }

};

This way, the class remains 'closed', but you have the flexibility you need. You can also add a copy function, and maybe a transform; these are the ones I most frequently need.

花心好男孩 2024-08-19 23:00:01

让你的类公开迭代器。

class Fat
{
   public:
    typedef std::vector<SECT>::iterator iterator;

    iterator begin() { return sectors.begin(); }
    iterator end() { return sectors.end(); }

      Fat();
   // some code here ...

   private:
      void LoadSectors(SECT startPoint);
      std::vector<SECT>sectors;

然后周围的代码可以通过一对迭代器自由遍历向量的元素。

Let your class expose iterators.

class Fat
{
   public:
    typedef std::vector<SECT>::iterator iterator;

    iterator begin() { return sectors.begin(); }
    iterator end() { return sectors.end(); }

      Fat();
   // some code here ...

   private:
      void LoadSectors(SECT startPoint);
      std::vector<SECT>sectors;

Then the surrounding code can traverse the elements of the vector freely, through just a pair of iterators.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文