标签: iterator

启用S​​TL迭代器调试真正做了什么?

我通过定义在应用程序中启用了迭代器调试

_HAS_ITERATOR_DEBUGGING = 1
Run Code Online (Sandbox Code Playgroud)

我期待这真的只是检查矢量边界,但我感觉它做的远不止于此.实际上正在执行哪些检查等?

顺便提一下Dinkumware STL.

c++ iterator stl

10
推荐指数
1
解决办法
2124
查看次数

访谈:为集合集合设计迭代器

为java中的集合集合设计迭代器.迭代器应隐藏嵌套,允许您迭代属于所有集合的所有元素,就像使用单个集合一样

java collections iterator

10
推荐指数
1
解决办法
1万
查看次数

为什么STL中没有"Iterable"接口?

C++ STL似乎不经常使用纯粹的抽象基类(也就是接口).我知道大多数事情都可以通过STL算法或聪明的模板元编程来实现.

但是,对于某些用例(例如,在API中,如果我不想具体说明我得到的容器类型,只是它包含的元素),以下形式的接口会很好:

template<typename T> struct forward_iterable {
    struct iterator {
        typedef T  value_type;
        typedef T& reference;
        typedef T* pointer;
        virtual reference operator*() const = 0;
        virtual pointer operator->() const = 0;
        virtual bool operator==(const iterator&) const = 0;
        virtual bool operator!=(const iterator&) const = 0;
        virtual operator const_iterator() const = 0;
        virtual iterator& operator++() = 0;
        virtual iterator  operator++(int) = 0;
    };
    struct const_iterator { ... };  // similar, but with const references

    virtual iterator begin() = 0;
    virtual …
Run Code Online (Sandbox Code Playgroud)

abstract-class iterator design-patterns stl

10
推荐指数
1
解决办法
4781
查看次数

在TreeSet上使用迭代器

情况:我有一个自定义对象TreeSet,我也使用了自定义比较器.我已经创建了一个在这个TreeSet上使用的迭代器.

TreeSet<Custom> ts=new TreeSet<Custom>();
Iterator<Custom> itr=ts.iterator();
while(itr.hasNext()){
    Custom c=itr.next();
    //Code to add a new element to the TreeSet ts
}
Run Code Online (Sandbox Code Playgroud)

问题:我想知道如果我在while循环中向TreeSet添加一个新元素,那么新元素会立即被排序.换句话说,如果我在while循环中添加一个新元素并且它小于我当前在c中保存的元素,那么在下一次迭代中我将获得与上一次迭代中相同的元素吗?(因为在排序之后,新添加的元素将占据当前元素之前的某个位置.

java collections iterator treeset concurrentmodification

10
推荐指数
2
解决办法
5万
查看次数

Iterator null集合

在不确定集合引用是否为null时,我必须在迭代之前检查null是很常见的.样品:

Collection<Object> collection = ...
...
if(collection != null)//troublesome
    for(Object o : collection)
Run Code Online (Sandbox Code Playgroud)

当然,我知道空集合比null要好得多,但在某些情况下,客户端代码无法控制来自其他模块的可空集合(例如,从第三方代码返回值).所以我写了一个实用工具方法:

public static <T> Iterable<T> nullableIterable(Iterable<T> it){
    return it != null ? it : Collections.<T>emptySet();
}
Run Code Online (Sandbox Code Playgroud)

在客户端代码中,不再需要检查null:

for(Object o : nullableIterable(collection))
...
Run Code Online (Sandbox Code Playgroud)

你认为nullableIterable()合理吗?有什么建议?有顾虑吗?谢谢!

java collections iterator

10
推荐指数
1
解决办法
1万
查看次数

将枚举转换为迭代器

我在工作表上遇到问题,即创建一个将Enumeration转换为Iterator的适配器.当我尝试运行以下代码时,我得到一个空指针异常.

import java.util.Vector;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Iterator;

public class ConvertEnumeration {

    public static void main(String [] args) {

        int [] ourArray = {0,1,2,3,4,5,6,7,8,9};
        Vector vector = new Vector(Arrays.asList(ourArray));

        //Get Enumerator 
        Enumeration enumerator = vector.elements();

        EnumerationToIterator enumToIt = new EnumerationToIterator(enumerator);
        while(enumToIt.hasNext()) {
            System.out.println(enumToIt.next());
        }
    }

}


//Convert our enumeration to Iterator!
class EnumerationToIterator implements Iterator {

    //Our enumeration
    Enumeration enmueration;

    //Constructor
    public EnumerationToIterator(Enumeration enmueration){
        enmueration = this.enmueration;
    }

    //Our Methods
    public boolean hasNext(){
        return enmueration.hasMoreElements();
    }

    public Object next(){ …
Run Code Online (Sandbox Code Playgroud)

java iterator enumeration nullpointerexception

10
推荐指数
4
解决办法
7658
查看次数

通过二叉搜索树迭代查找所有叶子

我对树很新,我正在尝试创建一种"叶子迭代器".我认为它应该将没有.left.right值的所有节点放在堆栈上,但我不确定它是怎么做的,甚至是不对的.我已经尝试过搜索它,但是我遇到的每个例子都是从最左边的叶子开始,然后继续p = node.parent,我避免链接到节点的父节点.

我不明白我怎么能重复从根开始并经过葡萄藤而不会一遍又一遍地访问相同的葡萄藤.

编辑

我看到人们建议使用递归方法来解决这个问题,我现在同意了.但是我一直试图找到迭代器级方法来解决这个问题,但是我仍然想知道这是否可能,以及如何做到这一点!

java algorithm iterator nodes binary-search-tree

10
推荐指数
1
解决办法
3万
查看次数

用"纯"C++ 11替换替换BGL迭代顶点?

我想用纯C++ 11等效替换顶点或边上的BGL迭代.BGL代码(来自:http://www.boost.org/doc/libs/1_52_0/libs/graph/doc/quick_tour.html)是:

typename boost::graph_traits<Graph>::out_edge_iterator out_i, out_end;
typename boost::graph_traits<Graph>::edge_descriptor e;
for (std::tie(out_i, out_end) = out_edges(v, g);
     out_i != out_end; ++out_i)
{
  e = *out_i;
  Vertex src = source(e, g), targ = target(e, g);
  std::cout << "(" << name[get(vertex_id, src)]
            << "," << name[get(vertex_id, targ)] << ") ";
}
Run Code Online (Sandbox Code Playgroud)

我从这里尝试了几个建议:用"纯"C++ 11替换替换BOOST_FOREACH?但没有运气.

我希望能够写出如下内容:

for (auto &e : out_edges(v, g))
{ ... }
Run Code Online (Sandbox Code Playgroud)

或类似的东西:

for (std::tie(auto out_i, auto out_end) = out_edges(v, g);
     out_i != out_end; ++out_i)
{...}
Run Code Online (Sandbox Code Playgroud)

可能吗?

c++ boost iterator boost-graph c++11

10
推荐指数
1
解决办法
1378
查看次数

使用泛型迭代器而不是特定的列表类型

我是Rust的新手,来自C#/ Java /类似.

在C#中,我们IEnumerable<T>可以使用它来迭代几乎任何类型的数组或列表.C#还有一个yield关键字,可用于返回惰性列表.这是一个例子......

// Lazily returns the even numbers out of an enumerable
IEnumerable<int> Evens(IEnumerable<int> input)
{
    foreach (var x in input)
    {
        if (x % 2 == 0)
        {
            yield return x;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这当然是一个愚蠢的例子.我知道我可以用Rust的map函数做到这一点,但我想知道如何创建自己的接受和返回泛型迭代器的方法.

从我可以收集到的内容,Rust具有可以类似使用的泛型迭代器,但它们超出了我的理解.我看到Iter,IntoIterator,Iterator类型,以及可能更多的文档,但没有很好地理解他们.

任何人都可以提供如何创建上述内容的明确示例吗?谢谢!

PS懒惰的方面是可选的.我更关心远离特定列表和数组类型的抽象.

arrays iterator vector rust

10
推荐指数
2
解决办法
2736
查看次数

如何解压缩迭代器?

给定一对对列表xys,将其解压缩为两个列表的Python成语是:

xs, ys = zip(*xys)
Run Code Online (Sandbox Code Playgroud)

如果xys是迭代器,我如何将其解压缩为两个迭代器,而不将所有内容存储在内存中?

python iterator generator python-itertools

10
推荐指数
2
解决办法
3362
查看次数