如何使 C++ 中的 for each 循环函数与自定义类一起使用

2021-12-26 00:00:00 list foreach c++ c++11

我是 C/C++ 编程的新手,但我已经使用 C# 编程 1.5 年了.我喜欢 C#,我喜欢 List 类,所以我想用 C++ 做一个 List 类作为练习.

I'm new to C/C++ programming, but I've been programming in C# for 1.5 years now. I like C# and I like the List class, so I thought about making a List class in C++ as an exercise.

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

该实现类似于现有的任何 Array List 类.我有一个 T* vector 成员用于存储项目,当这个存储空间即将被完全填满时,我调整了它的大小.

The implementation is similar to any Array List class out there. I have a T* vector member where I store the items, and when this storage is about to be completely filled, I resize it.

请注意,这不是在生产中使用的,这只是一个练习.我很了解 vector 和朋友们.

Please notice that this is not to be used in production, this is only an exercise. I'm well aware of vector<T> and friends.

现在我想遍历列表中的项目.我不喜欢使用 for(int i=0;i<n; i==).我在 Visual Studio 中输入 for,等待 Intellisense,它向我建议:

Now I want to loop through the items of my list. I don't like to use for(int i=0;i<n; i==). I typed for in the visual studio, awaited for Intellisense, and it suggested me this:

for each (object var in collection_to_loop)
{

}        

这显然不适用于我的 List 实现.我想我可以做一些宏观魔术,但这感觉就像一个巨大的黑客.实际上,最让我烦恼的是传递这样的类型:

This obviously won't work with my List implementation. I figured I could do some macro magic, but this feels like a huge hack. Actually, what bothers me the most is passing the type like that:

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

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

我的问题是:有没有办法让这个自定义 List 类与 foreach-like 循环一起工作?

My question is: is there a way to make this custom List class work with a foreach-like loop?

推荐答案

首先,C++for-each循环的语法与C#不同(也称为基于范围的for循环.它的形式为:

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>) { ... }

例如,使用 std::vector;vec,它会是这样的:

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

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

在幕后,这有效地使用了 begin()end() 成员函数,它们返回迭代器.因此,要允许您的自定义类使用 for-each 循环,您需要提供一个 begin() 和一个 end() 函数.这些通常是重载的,返回一个 iterator 或一个 const_iterator.实现迭代器可能很棘手,尽管使用类似向量的类并不太难.

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.

相关文章