标签: iterator

麻烦在对象列表上使用迭代器

所以我有一个名为symbol的类,它由4个字符串组成,这些字符串都是公共的.我创建了这些列表,我想在此列表中查看.这就是我到目前为止所拥有的.我查找了迭代器方法,它说它支持+运算符,但是我得到了一个错误.

    bool parser::parse(list<symbol> myList){
    //Will read tokens by type to make sure that they pass the parse
    std::list<symbol>::const_iterator lookAhead = myList.begin();
    if ((lookAhead + 1) != myList.end)
        lookAhead++;
    for (std::list<symbol>::const_iterator it = myList.begin(); it != myList.end(); ++it){
        if (it->type == "") {

        }
    }

    return true;
}
Run Code Online (Sandbox Code Playgroud)

尝试向lookAhead添加1时出错.为列表创建前瞻的有哪些好方法?

谢谢,宾克斯

c++ iterator list

0
推荐指数
1
解决办法
43
查看次数

将结构的迭代器传递给接受Rust中引用的函数?

我有一个函数,它接受结构的引用迭代器.有时我正在迭代一个向量,它工作正常,但有时我创建一个迭代器,产生新的结构,我很难找到一个.当我在闭包中创建一个值时,我会得到它,当闭包时它会消失.当我不想要它时,Rust总是试图将价值从事物中移出; 为什么不在这里?

struct Thing {
    value: u32
}

fn consume<'a, I: IntoIterator<Item=&'a Thing>>(things: I) {
    for thing in things {
        println!("{}", thing.value);
    }
}

fn main () {
    let solid = vec![Thing { value: 0 }];
    let ephemeral = (1..5).map(|i| &Thing { value: i });    // Boxing Thing does not work either
    consume(solid.iter());
    consume(ephemeral);
}
Run Code Online (Sandbox Code Playgroud)

error: borrowed value does not live long enough
Run Code Online (Sandbox Code Playgroud)

我有一种感觉,我要么需要将结构移出闭包和迭代器,要么将它存储在某个地方.但Box结构不起作用并返回结构而不是指针不会键入检查(我找不到相反的.cloned()).这是什么方法?

iterator rust

0
推荐指数
1
解决办法
496
查看次数

LinkedList时间遍历的迭代器

我写了一个程序,它存储了一些整数linkedlist,它还使用迭代器测试了它遍历列表的时间get(index),我对我的程序100%肯定,但是当我运行程序时,它给了我这个错误:

线程"main"java.lang.IndexOutOfBoundsException中的异常:索引:20000,大小:20000,位于java.util.LinkedList.get(LinkedList.java:476)的java.util.LinkedList.checkElementIndex(LinkedList.java:555)中Homework2.MyLinkedList.main(MyLinkedList.java:72)

您可以在下面找到我的代码.我需要帮助

import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Scanner;

public class MyLinkedList {
  public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    // System.out.print("Enter the size of your list: ");
    // int size = in.nextInt();

    int n1 = 20000;
    int n2 = 200000;
    int n3 = 2000000;

    LinkedList < Integer > list1 = new LinkedList();

    for (int i = 1; i <= n1; i++) {
      list1.add(i);
    }

    long t1Start = System.currentTimeMillis();
    ListIterator < Integer …
Run Code Online (Sandbox Code Playgroud)

java iterator traversal linked-list

0
推荐指数
1
解决办法
96
查看次数

在STL容器中释放内存

我有一个对象列表:

struct reply_t {
    unsigned int xid;
    char *buf;     
};

std::list<reply_t> > replies;
Run Code Online (Sandbox Code Playgroud)

我想删除该列表中的一些元素(这些元素xid <= confirmed_id).我喜欢简明扼要std :: remove_if.

remove_if(replies.begin(), replies.end(), 
   [&confirmed_id](const reply_t &arg) { return arg.xid <= confirmed_id; }); 
Run Code Online (Sandbox Code Playgroud)

问题是这不会释放bufs(在其他地方分配).有没有一种优雅的方式来处理这个?它是否像在对象析构函数中添加free(buf)一样简单?

c++ lambda iterator memory-management

0
推荐指数
1
解决办法
120
查看次数

如何查看迭代器的索引?

所以我有一个字符串source,我用迭代器循环:

iterator = iter(source)
for char in iterator:
  do stuff
Run Code Online (Sandbox Code Playgroud)

但是,现在说我有一个签入do stuff,我将迭代器的值与'h'进行比较.然后我想以某种方式看看'h'是否后跟"ello",然后将前十个字符添加到列表中.
为此,我自己的想法是找出哪个索引与迭代器的当前位置相对应,以便我可以说:

indIt = index(char)
if source[indIt + 1: indIt + 6] == "ello ":
  someList.append(source[indIt + 7:indIt + 16])
  indIt += 17
  char = indIt #which may also be fun to know how it can be done, if
Run Code Online (Sandbox Code Playgroud)

这意味着对于给定的输入hello Sandra, oh and hello Oscar, i welcome you both!,someList将包含["Sandra,oh","Oscar,i w"].

那么,我可以通过某种方式确定迭代器当前位置对应的索引吗?

python iterator

0
推荐指数
1
解决办法
65
查看次数

颠倒`IEnumerator`的行为

我在C#中编写一个Iterator接口,其行为与a相同,IEnumerator但它具有不同的签名

public interface Iterator
{
    /// <summary>
    /// Check if there iterator has another value.
    /// </summary>
    /// <returns><c>true</c>, if there is another value <c>false</c> otherwise.</returns>
    bool hasNext();

    /// <summary>
    /// Move the iterator to the next object and return that object.
    /// </summary>
    /// <returns>The next object.</returns>
    object next();
}
Run Code Online (Sandbox Code Playgroud)

这是向后从IEnumerator行为MoveNextCurrent.如果我有一个实现只是包装一个IEnumerator作为我的Iterable

class EnumeratorWrap: Iterator
{
    private IEnumerator<object> _iter;
    private bool _hasNext = true;

    public Enumerator(IEnumerator<object> iter)
    {
        _iter …
Run Code Online (Sandbox Code Playgroud)

c# iterator

0
推荐指数
1
解决办法
352
查看次数

Python CSV阅读器:没有获取线功能?

我一直在这里阅读CSV模块上的Python文档:https://docs.python.org/2/library/csv.html

我注意到它没有提到任何方式让读者对象返回它当前正在读取的行数,或者它当前处于哪个迭代.这似乎是一个广泛需要的功能.所以,我想知道如何做到这一点:

reader = csv.DictReader(readFile)
    for row in reader:
        # do something 
        # now want to find the line number that row corresponds to
Run Code Online (Sandbox Code Playgroud)

或者,如果不可能,为什么会这样.

python csv iterator generator

0
推荐指数
1
解决办法
111
查看次数

如何调用每个迭代两个列表的zip函数?

我有两个列表: a=[1,2,3],b=[a,b,c]

我希望zip这两个中的每一个都调用一个函数,但不要在for循环中以微不足道的方式执行它.有pythonic方式吗?我尝试过map:

map(func(i,v) for i,v in zip(a,b))

但它不起作用

python dictionary iterator

0
推荐指数
1
解决办法
40
查看次数

__iter__中__getitem__的等效代码

我试图__iter__在Python 3中更多地了解.出于某些原因__getitem__,我更好地理解__iter__.我想我不知道怎么没有得到相应的下一个实现__iter__.

我有以下代码:

class Item:
    def __getitem__(self,pos):
        return range(0,30,10)[pos]

item1= Item()
print (f[1]) # 10
for i in item1:
   print (i) # 0 10 20
Run Code Online (Sandbox Code Playgroud)

我理解上面的代码,但是又如何使用__iter__和编写等效代码__next__()

class Item:
   def __iter__(self):
      return self
      #Lost here
   def __next__(self,pos):
      #Lost here
Run Code Online (Sandbox Code Playgroud)

我理解当python看到一个__getitem__方法时,它会尝试通过调用带有整数索引的方法来迭代该对象0.

python iterator python-3.x

0
推荐指数
1
解决办法
413
查看次数

继续遍历数组迭代

我意识到我的沟通技巧并不那么出色(我甚至在试图查找它时遇到问题),所以我让代码说话:

easy_as_123 = ("a".."c").to_a
10.times do |j|
  if j >= easy_as_123.length
    puts "j is #{j}, letter is #{easy_as_123[j % easy_as_123.length]}"
  else 
    puts "j is #{j}, letter is #{easy_as_123[j]}"
  end
end
Run Code Online (Sandbox Code Playgroud)

是否有一个更优雅和简洁的解决方案,以不断迭代我的[a,b,c]数组?

编辑,我发布的代码作品,这正是我正在寻找的结果,但它不简洁也不好看,有没有方法能够以更优雅的方式实现同​​样的结果?

ruby arrays iterator range

0
推荐指数
1
解决办法
85
查看次数