如何使C ++中的每个循环函数使用自定义类 [英] How to make the for each loop function in C++ work with a custom class

查看:261
本文介绍了如何使C ++中的每个循环函数使用自定义类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚接触C / C ++编程。我在C ++中做了一个自定义类,在C#中作为一个列表。我用这种方式:

  List< int> ls; 
int whatever = 123;
ls.Add(1);
ls.Add(235445);
ls.Add(whatever);

它具有动态分配。我有一个 T *向量变量,我把我的数组,当列表即将完全填充,我调整为这样的大小: 8 *(pow(++ realloc_count,4)* 4)。它的工作很好,不知道这个计算是否是最正确的实现这一点。但是无论如何。



我喜欢重新发明一个更低级别的语言,当我可以,这就是为什么我不使用 T> 类。我这样做只是为了好玩。



现在我想循环遍历我的列表的项目。我不喜欢使用好的旧 for(int i = 0; i 循环,所以我想:如果我可以使用一个漂亮漂亮的 foreach-like 循环,使用如下语法:

  foreach(int item in list){Work(item);} 

code> 在视觉工作室,等待Intellisense,它建议我这:

  for each(object var in collection_to_loop)
{

}

NICE!有一个foreach循环的集合在C + +我想,但我的类OBVIOUSLY将不工作,我的列表是非常基本的。经过一些研究,我注意到,这将只适用于STL库...



我可以用UGLIEST KLUDGE我可以THINK,我想摆脱它:

  #define foreach(type,var,list)\ 
int _i_ = 0; \
## type var; \
for = 0,var = list [_i_]; _i_

我使用它像这样:

  foreach(int,i,ls){
doWork(i);
}

请不要杀我。

解决方案

那么,有没有办法写一个甜美美丽的foreach样循环? div>

首先, C ++ 中的 for-each 循环的语法不同于 C#(它也被称为以循环为基础的范围),形式如下:

  for(< type> < name> :< collection>){...} 

c> std :: vector< int> vec ,它会是这样:

  for(int i:vec){... } 

覆盖下,这有效地使用 begin c $ c>和 end()成员函数,它返回迭代器。因此,要允许您的自定义类使用 for-each 循环,您需要提供 begin() end()函数。这些通常是重载的,返回一个迭代器或者一个 const_iterator 。实现迭代器可能很棘手,虽然有一个向量类的类不是太难。

  template< typename T& 
struct List
{
T * store;
std :: size_t size;
typedef T * iterator;
typedef const T * const_iterator;

....

迭代器begin(){return& store [0]; }
const_iterator begin()const {return& store [0]; }
iterator end(){return& store [size]; }
const_iterator end()const {return& store [size]; }

...
};

使用这些实现,您可以使用上述基于范围的循环。


I'm new to C/C++ programming. I made a custom class in C++ that works as a List in C#. I use it this way:

List<int> ls;
int whatever = 123;
ls.Add(1);
ls.Add(235445);
ls.Add(whatever);

It has dynamic allocation. I have a T* vector variable where I put my array, and when the list is about to be completely filled, I resize it to this size: 8 * (pow(++realloc_count,4) * 4). It's working good, not sure if this calculation is the most correct to achieve this. But whatever.

I like to reinvent the wheel in a lower-level language when I can, that's why I'm not using the vector<T> class. I'm doing this just for fun.

Now I want to loop through the items of my list. I don't like to use the good old for(int i=0;i<n; i==) loop, so I thought: "What if I could use a nice and beautiful foreach-like loop, using a syntax like this:

foreach (int item in list) { Work(item); }

And then I typed for in the visual studio, awaited for Intellisense, and it suggested me this:

for each (object var in collection_to_loop)
{

}        

"NICE! There is a foreach-loop for collections in C++" I thought. But my class OBVIOUSLY won't work with that. My List is very basic. And after some research I noticed that this will only work with the STL Libraries...

I can do that with the UGLIEST KLUDGE I COULD THINK, and I want to get rid of it:

#define foreach(type, var, list)\
int _i_ = 0;\
##type var;\
for (_i_ = 0, var=list[_i_]; _i_<list.Length();_i_++,var=list[_i_]) 

And I use it like this:

foreach(int,i,ls){
    doWork(i);
}

Please don't kill me.

So, is there a way to write a sweet and beautiful foreach-like loop?

解决方案

Firstly, the syntax of a for-each loop in C++ is different from C# (it's also called a range based for loop. It has the form:

for(<type> <name> : <collection>) { ... }

So for example, with an std::vector<int> vec, it would be something like:

for(int i : vec) { ... }

Under the covers, this effectively uses the begin() and end() member functions, which return iterators. Hence, to allow your custom class to utilize a for-each loop, you need to provide a begin() and an end() function. These are generally overloaded, returning either an iterator or a const_iterator. Implementing iterators can be tricky, although with a vector-like class it's not too hard.

template <typename T>
struct List
{
    T* store;
    std::size_t size;
    typedef T* iterator;
    typedef const T* const_iterator;

    ....

    iterator begin() { return &store[0]; }
    const_iterator begin() const { return &store[0]; }
    iterator end() { return &store[size]; }
    const_iterator end() const { return &store[size]; }

    ...
 };

With these implemented, you can utilize a range based loop as above.

这篇关于如何使C ++中的每个循环函数使用自定义类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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