标签: iterator

如何访问第二个map迭代器?

我们是两个学生,现在我们有一个我们无法解决的史诗般的大问题.我们向老师请了一些帮助,但他无法帮助我们,所以我们最后一次机会就是这个论坛!

我们正在做一个项目:NPI文件的命令解释器.

map<string,void(Interpreteur::*)()>::iterator trouve = interpreteur.myMap.find(saisie);
if(trouve == interpreteur.myMap.end()) 
    cerr<<"command not found"<<endl; 
else 
    (trouve->*second)();
Run Code Online (Sandbox Code Playgroud)

我们必须使用名为"map"的对象,但是我们不能得到名为"Second"的第二个参数.为什么?Code Blocks告诉我们错误是在"else"中,这是错误:

在此范围内未声明"秒".

我们也尝试过:

map<string,void(Interpreteur::*)()>::iterator trouve = interpreteur.myMap.find(saisie);
if(trouve == interpreteur.myMap.end()) 
    cerr<<"command not found"<<endl; 
else 
    (trouve.second)();
Run Code Online (Sandbox Code Playgroud)

代码块回答:

错误:'std :: map,void(Interpreteur ::*)()> :: iterator'没有名为'second'的成员

如果有人可以帮助我们,它将拯救我们的项目,我们必须在明天结束它.我们将非常感激.

非常感谢您的帮助,我们可以回答问题,如果有的话:)

c++ iterator stl map

6
推荐指数
1
解决办法
1880
查看次数

与at()或索引相比,为什么使用C++迭代器会大大增加代码大小?

我一直在寻找使用更新的C++语言功能,例如嵌入式系统上的迭代器(16KB的SRAM和64 KB的闪存,Cortex M4),并遇到了令人惊讶的障碍.为什么地球上的迭代器如此庞大?我的印象是他们基本上是一些指针算术或索引.STL是否引入了一些意想不到的代码?

这些都是从GCC-臂无- EABI-4_9工具链窗口使用的Kinetis设计工作室在这里使用了以下标志.

arm-none-eabi-g++ -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Os -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fsingle-precision-constant -flto  -g3 -I"../Sources" -I"../Includes" -std=gnu++11 -fabi-version=0 -std=c++11 -MMD -MP -MF"Sources/System.d" -MT"Sources/System.o" -c -o "Sources/System.o" "../Sources/System.cpp"
Run Code Online (Sandbox Code Playgroud)

ITM_SendChar只需要一个字符并将其放入寄存器中.

std::string input = "Oh hai there! :D\n";

#ifdef char_array
    // .text              7352
    // .data               376
    // .bss                236
    for(int i = 0; i < input.size(); i++)
            ITM_SendChar(input[i]);
#endif

#ifdef char_at
    // .text              7392
    // .data               376
    // .bss                236
    for(int i = 0; i < input.size(); …
Run Code Online (Sandbox Code Playgroud)

c++ embedded gcc iterator stl

6
推荐指数
1
解决办法
426
查看次数

迭代从索引开始的python字符串

如果我有一个非常长的字符串(比如说1亿个字符),有没有办法使用类似for c in str:但是开始一定数量的字符来迭代字符?我不希望切片并使用子集,因为我知道切割字符串会产生副本(在我的情况下非常昂贵).换句话说,我可以为字符串指定迭代器的起始点吗?

python string iterator character

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

理解比较中的可迭代类型

最近我遇到了cosmologicon的pywats,现在尝试了解迭代器的乐趣:

>>> a = 2, 1, 3
>>> sorted(a) == sorted(a)
True
>>> reversed(a) == reversed(a)
False
Run Code Online (Sandbox Code Playgroud)

好的,sorted(a)返回a listsorted(a) == sorted(a)变成两个列表比较.但reversed(a)回报reversed object.那么为什么这些反转的物体不同呢?而且id的比较让我更加困惑:

>>> id(reversed(a)) == id(reversed(a))
True
Run Code Online (Sandbox Code Playgroud)

python iterator

6
推荐指数
2
解决办法
154
查看次数

将List Iterator传递给Java中的多个线程

我有一个包含大约200K元素的列表.

我能够将此列表的迭代器传递给多个线程并让它们遍历整个批次,而没有任何访问相同的元素吗?

这就是我现在想到的.

主要:

public static void main(String[] args)
{
    // Imagine this list has the 200,000 elements.
    ArrayList<Integer> list = new ArrayList<Integer>();

    // Get the iterator for the list.
    Iterator<Integer> i = list.iterator();

    // Create MyThread, passing in the iterator for the list.
    MyThread threadOne = new MyThread(i);
    MyThread threadTwo = new MyThread(i);
    MyThread threadThree = new MyThread(i);

    // Start the threads.
    threadOne.start();
    threadTwo.start();
    threadThree.start();
}
Run Code Online (Sandbox Code Playgroud)

MyThread的:

public class MyThread extends Thread
{

    Iterator<Integer> i;

    public MyThread(Iterator<Integer> i)
    { …
Run Code Online (Sandbox Code Playgroud)

java multithreading iterator listiterator

6
推荐指数
1
解决办法
5958
查看次数

通过在C++中直接访问其迭代器来删除容器的元素

我已经std::vector<int>在我的main函数中声明了一个并且想要从中删除所有偶数元素,但只是将它的迭代器传递给一个remove_even接受容器的开始和结束迭代器的函数.

#include <iostream>
#include <algorithm>
#include <vector>

void remove_even(auto start, auto end) {
    while(start != end) {
        if(*start % 2 == 0)
        // Remove element from container
    }
}

int main() {
    std::vector<int> vec = {2, 4, 5, 6, 7};
    remove_even(vec.begin(), vec.end());
}
Run Code Online (Sandbox Code Playgroud)

有没有办法在C++中这样做或者我必须直接将我的向量传递给函数?

c++ containers iterator vector

6
推荐指数
1
解决办法
152
查看次数

std :: end(myVector)和myVector.end()之间的区别

我注意到有两种方法可以获得向量(或其他容器类)的结束迭代器:

std::end(myVector)
Run Code Online (Sandbox Code Playgroud)

myVector.end()
Run Code Online (Sandbox Code Playgroud)

这同样适用于其他各种容器迭代器功能begin,cend,cbegin,rend,rbegin,crend,crbegin,find,等,如果有这些之间的任何功能上的区别是什么我不知道是什么?如果没有,是否有一些历史原因让他们两个?

(如果这是重复的道歉,我已经搜索了全部,并且发现了这些方法中的一个或另一个的大量来源,但没有提到两者或比较两者.)

c++ containers iterator stl

6
推荐指数
1
解决办法
320
查看次数

Java 8 - 在地图值中过滤列表

我正在编写一个方法,它接受在这里定义a Map的形式的输入.Map<Term, List<Integer>>Term

方法:

  1. 翻过它的键Map并使用Term属性过滤它们.
  2. 对于每个剩余的键,获取相应列表的大小,将其限制为5(min(List.size(), 5))并将输出添加到全局变量(例如totalSum)
  3. 返回 totalSum

这是我到目前为止所写的:

 inputMap
    .entrySet()
    .stream()
    .filter(entry -> entry.getKey().field().equals(fieldName))    // Keep only terms with fieldName
    .forEach(entry -> entry.getValue()
        .map(size -> Math.min(entry.getValue().size(), 5)))   // These 2 lines do not work
        .sum();
Run Code Online (Sandbox Code Playgroud)

我无法将列表流作为输入,为每个列表输出一个整数并返回所有输出的总和.

我显然可以使用for循环来编写它,但我正在尝试学习Java 8,并且好奇如果使用它可以解决这个问题.

java iterator java-8 java-stream collectors

6
推荐指数
1
解决办法
1416
查看次数

Python如何将方法的结果转换为生成器

我有以下继承:

class Processor(object):
    def get_listings(self):
        """
        returns a list of data
        """
        raise NotImplemented()

    def run(self):
        for listing in get_listings():
           do_stuff(listing)

class DBProcessor(Processor):
    def get_listings(self):
        """
        return a large set of paginated data
        """
        ...
        for page in pages:
            for data in db.fetch_from_query(...):
                yield data
Run Code Online (Sandbox Code Playgroud)

虽然这有效,但是这会失败len(self.get_listings())或任何其他列表操作.

我的问题是如何重构我的代码DBProcessor.get_listings可以处理列表操作,但是当它的迭代器调用它将返回一个生成器?

python iterator generator

6
推荐指数
1
解决办法
93
查看次数

为什么javac错误"(x)不能应用于(y)",当参数和参数都匹配时会发生?(内部类调用外类方法)

通过家庭作业了解Java迭代器和一般数据结构.

我已经构建了一个双链表(LinkedList),它使用Nodes(LinkedList $ Node)并有一个Iterator(LinkedList $ LinkedListIterator)所有类都使用泛型.

在LinkedListIterator的@Overridden remove()方法中,我正在使用外部类的方法,即LinkedList类.

我得到以下编译时错误:

./LinkedList.java:170: deleteNode(LinkedList<T>.Node<T>,LinkedList<T>.Node<T>,LinkedList<T>.Node<T>) in LinkedList<T> cannot be applied to (LinkedList<T>.Node<T>,LinkedList<T>.Node<T>,LinkedList<T>.Node<T>)
        deleteNode(nodeToBeRemoved, next, prev);
Run Code Online (Sandbox Code Playgroud)

我的(原始)理解是类型不匹配,但我不明白这是怎么回事.

这是我完整的类代码:

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.ConcurrentModificationException;


public class LinkedList<T> implements Iterable<T> { 
    private Node<T> sentinel;
    private long modCount;  //apparently no unsigned int's in Java.

    public LinkedList() {
        modCount = 0;
        sentinel = new Node<T>(null);
        sentinel.setNext(sentinel);
        sentinel.setPrev(sentinel);
    }

    public void append(T t) {
        /*
                    APPEND:
                      ...
                    [-----]
                    |     |
                    [-1st-]
                    |     |
        inFront->   [SENTL]
                    |     | <-- [newNode] …
Run Code Online (Sandbox Code Playgroud)

java iterator iterable compiler-errors inner-classes

6
推荐指数
1
解决办法
128
查看次数