C++ 中的 Java 迭代器等价物? (带代码)

标签 java c++

大家好。一位 friend 为我编写了一些 Java 代码,我很容易将其转换为 C++,但我很好奇 C++ 中 Java 迭代器的等价物。这是代码,我很可能希望将数据返回到 vector 中。感谢任何帮助

public class RLEIterator
extends RegionIterator
{
    public int reg = 0;
    public int mode = 0;
    public int skip = 0;
        // mode is the number of IDs that are valid still (count down)
        // skip is used after we run out of mode IDs, and move forward
        //   The goal is, to always have a valid 'hasNext' state, after
        // an ID is read via 'next'. Thus, the initial search, and then
        // the reading forward if mode == 0, after the ID is found.
    public int i;

    public RLEIterator()
    {
        // Need to set up the skip of an initial part, so we can
        // correctly handle there not being anything, despite there
        // being data encoded.

        int comp;
        i = 0;

        while ((mode == 0) && (i<nearRLE.length))
        {
            // set up the next code
            comp = ((int)nearRLE[i]) & 0xff;

            if ((comp > 0) && (comp <= 0x3e))
            {
                // skip forward by comp;
                reg += comp;
                i++;
                mode = 0; // have to keep on reading
            }
            else if (comp == 0x3f)
            {
                // skip forward by the following 16 bit word;
                // second byte is hi-byte of the word
                reg += ((int)nearRLE[i+1]) & 0xff;
                reg += (((int)nearRLE[i+2]) & 0xff) << 8;

                i+=3;
            }
            else if (comp == 0xff)
            {
                // include the following WORD of regions
                mode = ((int)nearRLE[i+1]) & 0xff;
                mode += (((int)nearRLE[i+2]) & 0xff) << 8;
                i += 3;
            }
            else if ((comp >= 0xc0) && (comp <= 0xfe))
            {
                // comp - 0xc0 regions are nearby
                mode = comp - 0xc0;  // +1 perhaps?
                i++;
            }
            else if ((comp >= 0x40) && (comp <= 0x7f))
            {
                // skip bits 3-5 IDs and then include bits 0-2
                reg += (comp & 0x38) >> 3;
                mode = (comp & 0x7);
                i++;
            }
            else if ((comp >= 0x80) && (comp <= 0xbf))
            {
                // include IDs bits 3-5, then skip bits 0-2
                mode = (comp & 0x38) >> 3;
                skip = (comp & 0x7);
                i++;
            }
        }
    }

    public boolean hasNext()
    {
        // not at the end of the RLE, and not currently processing a
        // directive. (mode)
        return (mode > 0);
    }

    public int next()
    {
        int ret = -1;
        int comp;

        // sanity check first. Shouldn't truthfully get called if mode
        // isn't >0
        if (mode <= 0)
            return -1;

        ret = reg;
        reg++;
        mode--;

        if (mode == 0)
        {
            // skip forward
            reg += skip;
            skip = 0;

            while ((mode == 0) && (i<nearRLE.length))
            {
                // set up the next code
                comp = ((int)nearRLE[i]) & 0xff;

                if ((comp > 0) && (comp <= 0x3e))
                {
                    // skip forward by comp;
                    reg += comp;
                    i++;
                    mode = 0; // have to keep on reading
                }
                else if (comp == 0x3f)
                {
                    // skip forward by the following 16 bit word;
                    // second byte is hi-byte of the word
                    reg += ((int)nearRLE[i+1]) & 0xff;
                    reg += (((int)nearRLE[i+2]) & 0xff) << 8;

                    i+=3;
                }
                else if (comp == 0xff)
                {
                    // include the following WORD of regions
                    mode = ((int)nearRLE[i+1]) & 0xff;
                    mode += (((int)nearRLE[i+2]) & 0xff) << 8;
                    i += 3;
                }
                else if ((comp >= 0xc0) && (comp <= 0xfe))
                {
                    // comp - 0xc0 regions are nearby
                    mode = comp - 0xc0;  // +1 perhaps?
                    i++;
                }
                else if ((comp >= 0x40) && (comp <= 0x7f))
                {
                    // skip bits 3-5 IDs and then include bits 0-2
                    reg += (comp & 0x38) >> 3;
                    mode = (comp & 0x7);
                    i++;
                }
                else if ((comp >= 0x80) && (comp <= 0xbf))
                {
                    // include IDs bits 3-5, then skip bits 0-2
                    mode = (comp & 0x38) >> 3;
                    skip = (comp & 0x7);
                    i++;
                }
            }
        }

        return ret;
    }
}

同样,任何有关如何将其融入 C++ 中单个函数(如果可能)的方案的帮助都将非常棒。谢谢!

最佳答案

您对 C++ 迭代器的熟悉程度如何?它们被设计成看起来非常像指针,所以给定一个迭代器 it:

++it

将递增迭代器(跳到下一个元素)

*it

将解引用迭代器(返回一个引用到它指向的元素)

--it

将(如果它被定义的话)递减迭代器(回到前一个元素)

C++ 迭代器通常对它们操作的容器一无所知。特别是,检查序列中是否还有更多元素的方法不是调用迭代器上的 HasNext,而是将其与已知指向序列末尾的迭代器进行比较。 (或者,严格来说,将其与指向序列末尾的元素进行比较。)

除此之外,迭代器需要定义一些类型定义和其他辅助内容,以帮助编译器分类和理解迭代器的功能。

定义迭代器的最简单方法是使用 Boost.Iterator 库的 iterator_facade 类,它为您实现了几乎所有的管道。该库具有出色的文档,包括描述如何定义您自己的迭代器类型的教程。

如果使用 Boost 是一种选择,那么您绝对应该这样做。否则,标准库有 std::iterator 这也有一点帮助(但是,值得注意的是,它不是强制性的 - 迭代器可以完美有效地定义而无需派生自此类)

很抱歉,我没有写出完整的示例,但我现在有点赶时间。 (而且,如果您能够使用 Boost,那么从头开始定义示例迭代器的麻烦将是浪费时间)。希望以上内容足以让您入门。

关于C++ 中的 Java 迭代器等价物? (带代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6242353/

相关文章:

java - 如何使用 OJDBC 取回生成的 ID?

java - 在 volley 加载数据时向应用程序添加进度微调器

c++ - QDialog - 设置背景图片

.net - 在 win32 API 和 .NET 框架之间进行选择

C++ 向下转换(使用 dynamic_cast )返回 NULL?

java - 使用 IntelliJ IDEA 配置 Spring MVC

java - 公交线路-Google Maps Android API v2

java - 我的 fxgl 遇到非法状态异常 : EntityFactory was not set!

c++ - 我可以在没有 tputs 或 putp 的情况下使用 tparm()

c++ - 自定义消息Dlg