Questions tagged [iterator]

An iterator is an object-oriented programming pattern that allows traversal through a collection, agnostic of the actual implementation or object addresses in physical memory. It is one of the Gang of Four's behavioral design patterns.

Filter by
Sorted by
Tagged with
-1 votes
0 answers
73 views

For an iterator method of IEnumerable<string> as a return type , what is the actual return type of it?

After searching I knew the role of the (IEnumerator) and the (GetEnumerator()) method in the foreach loop and how the compiler searches for that method by the duck typing way when using the foreach ...
Hossam Zaki's user avatar
1 vote
0 answers
61 views

Implementation of a custom zip iterator that works with std::ranges::transform as well as std:::transform [duplicate]

I have implemented a zip iterator, and it works well when used with algorithm functions like std::for_each or std::transform. However, when I use it with std::ranges::for_each or std::ranges::...
Kyoungyoon PARK's user avatar
0 votes
1 answer
35 views

What happens with memory for list literals with range when converted to iterator object?

I'm wondering if CODE 2 requires 41880+ of memory space anyways, since I "created" a list... but perhaps not because I didn't store it in a variable? In other words, are CODE 1 and CODE 2 ...
dungarian's user avatar
  • 203
3 votes
2 answers
86 views

AbstractList guarantees that spill into other methods

I can't think of a better name for the question, but I hope that an example will make this simpler. Suppose I create my own custom List (it is irrelevant what it does): static class MyList<E>...
Eugene's user avatar
  • 118k
1 vote
1 answer
88 views

What happens when I call .by_ref().take().rev() on a IntoIter

Let's say I have a IntoIter and I want to collect chunks of n elements from right to left (equivalent to rchunks on slices). I can .rev() on the iterator and then call .by_ref().take(n).rev() for each ...
Son Kaos999's user avatar
1 vote
1 answer
33 views

How can I make a mutating iterator that locks inner data until dropped?

My goal is to make a NewType(Arc<RwLock<Vec<T>>>) with a many_iter_mut() method that returns an iterator that holds a write-lock (RwLockWriteGuard) over the inner Vec<T>. Thus ...
danda's user avatar
  • 629
0 votes
0 answers
19 views

Access a custom C# Dictionary Iterator in Scriban

I got a Dictionary<string, object> where I want to format the object on the fly. This works in the console, the problem is that I need to access those values in my Scriban template. That's how I ...
Standard's user avatar
  • 1,490
1 vote
1 answer
44 views

Trait to optionally dereference a reference to a Copy object?

I'm implementing a function that takes a list of elements by value, but I'll need to perform two passes over the list. The first pass computes some value over the input data (without modifying the ...
Bernard's user avatar
  • 5,299
0 votes
1 answer
104 views

c++ returning const reverse iterator from a class method, but method cannot be const?

I am trying to return a const reverse iterator from a class method. But my code doesn't compile when I mark my method as const. Without const the code compiles with no troubles. Any idea why? #include ...
Miroslav Krajcir's user avatar
-1 votes
0 answers
56 views

Is there any way to remove the need for const_cast in this case?

I am writing a data structure for a prefix iterator template class to perform some DFS. Iterator& operator++() { TTreeNode lPopped = const_cast<TTreeNode>(fStack.top()); ...
Simplylmk's user avatar
  • 265
0 votes
0 answers
59 views

C++ std::copy in forward method is not working [duplicate]

Visual study 2013 An error occurred after migrating the developing project to 2019, Perhaps operator= is no longer supported It's my code, the source that concatenates the list of std::list to ';' ...
김주환's user avatar
3 votes
1 answer
37 views

How to pass an arbitrary iterator as a function argument?

Question If I write a generic walk routine, why can I pass in my own iterators but not pairs? Example Here's a simple walk that does something for each item in src. function walk(src) for n,i in src ...
Tim Menzies's user avatar
1 vote
1 answer
69 views

Can I split an iterator with Item=(key, val) into separate key iter and val iter without collecting or cloning?

I have a function that looks like this: fn set_many(&mut self, key_vals: impl IntoIterator<Item = (Index, T)>) { let (keys, vals): (Vec<_>, Vec<_>) = key_vals....
danda's user avatar
  • 629
0 votes
1 answer
111 views

apply std::transform to every nth element

Is there a way to construct an iterator from another one which will always advance (in the sense of std::advance) by a number n? I would like to pass an iterator to std::transform but the ...
0xbadf00d's user avatar
  • 17.7k
3 votes
2 answers
121 views

In C++20, is the sentinel type of a forward range guaranteed to be the same type as its iterator?

A forward range r requires that its iterator is in turn a forward iterator, which in turns requires that the iterator works as its own sentinel. But, does it imply that the actual sentinel type (e.g., ...
ABu's user avatar
  • 10.7k
4 votes
1 answer
50 views

Iteration in Scala vs. Kotlin

I'm migrating some code from Scala to Kotlin, I observe different behaviour: Scala: var i = 0 Iterator.continually{ println(s"i=$i") i += 1 i }.takeWhile { _ < 3 }.foreach { i =&...
david.perez's user avatar
  • 6,314
0 votes
1 answer
67 views

How to register a template type as a valid value_type in STL algorithms

I am trying to write a custom container imitating the STL list. I have provided a Link class (not shown), some basic functionality and a custom bidirectional iterator. Trying to conform to STL ...
berkeozgur's user avatar
3 votes
1 answer
50 views

Is it possible to get iterator value without switch to the next position:

In C++ I can get current value of iterator many times int main() { std::string s = "abcd"; auto iter = s.begin(); std::cout << *iter << std::endl; // a std::cout &...
mascai's user avatar
  • 1,529
0 votes
2 answers
39 views

Are there a C# equivalents of Python's iter() and next() functions?

Python's iter(.) and next(.) built-in functions allow iterating a list (or other objects that implement __iter__(self) and __next__(self)) without a for loop and without an index. Does C# have ...
joseville's user avatar
  • 743
0 votes
2 answers
47 views

Loop through list of iframes, check for element in iframe and do action with it

Here is the situation: On https://www.globalsqa.com/samplepagetest/ I am trying to access the page "Alertbox" through page dropdown menu Tester's Hub/Demo Testing Site/Alertbox. Before the ...
nerull7's user avatar
0 votes
2 answers
42 views

How do we just select lists from the imported module?

Let's say I did the following: from X import A, B, C, D What I want to do is that combine all lists in A, B, C, D to make a single big list: all_list=[] for each_variable in (X.A, X.B, X.C, X.D ...
user98235's user avatar
  • 840
0 votes
1 answer
37 views

Flatten and combine field from nested struct

I have a struct body struct that contains planet info such as position velocity etc pub struct Body { pub id: u8, pub pos: Vec<f64>, pub vel: Vec<f64>, } pub struct Data { ...
thefrollickingnerd's user avatar
1 vote
1 answer
69 views

How to get rid of extraneous (0..) in iteration when its not used?

I came up with the following function to transpose a Vec<Vec<T>>: fn transpose<T>(v: Vec<Vec<T>>) -> Vec<Vec<T>> { let mut xs = v.into_iter().map(|x| x....
Anopt's user avatar
  • 182
-1 votes
1 answer
41 views

Python generator missing a value during iteration

Hello while using generators in my code, I noticed this strange behaviour where after breaking out from the loop, the next() call on a generator skips one value. Example code: from itertools import ...
knightcool's user avatar
-2 votes
2 answers
144 views

What does the "!= data + arraySize" mean in C++? [closed]

I was looking for a way to find a given int in my array and i found this solution #include <algorithm> #include <iostream> using namespace std; int main() { int data[] = {23, 45, 56, 12,...
Pryhas's user avatar
  • 19
2 votes
1 answer
130 views

find() does not recognize my custom iterator

I wrote a custom iterator named Text_Iterator for a simple text editor. However, STL algorithms such as find() would not recognize it as a legitimate iterator. My code is: using Line = std::vector<...
berkeozgur's user avatar
0 votes
2 answers
75 views

Is there a way to implement a cartesian product over iterators that are *not cloneable*?

The idea is that I want the cartesian product of iterators that are all Box<dyn...>, because I have a function higher up that returns different types of iterators, some of which must be run ...
Julia Benginow's user avatar
0 votes
1 answer
58 views

What is the difference between `return iter()` and `yield from` in Python?

What is the difference between return iter() and yield from in my case? Or is my code correct? I want my class can be iterated multiple times. See the code below: from typing import Iterable class ...
Huaijun Jiang's user avatar
0 votes
1 answer
46 views

Don't understand how std::iterator works in list initialization [duplicate]

I have trouble with using std::vector::iterator. First I defined a struct: template <typename T> struct VecIter { VecIter(std::vector<T>::iterator &&it_, size_t remain_): it(...
oronr's user avatar
  • 13
2 votes
3 answers
119 views

Looping through a HashMap using iterator does not stop for an if condition

A HashMap<Integer, Integer> has 10 entries but I only want to print 3 entries. Code: HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>(); hm.put(2, 1); hm.put(5, 3); hm.put(...
user2817235's user avatar
1 vote
1 answer
62 views

Mistery of Iterators

I'm going crazy for the strange behaviour of the Iterators in Google Apps Script. My script picks a random folder (corresponding to a year between 2012 and the current year), then pick a random image ...
Gabriele's user avatar
0 votes
2 answers
59 views

jump out of Vec::retain()?

I want to iterate through a Vec doing various things to the objects in it (which would be more complex than usizes), and, in the event of an order to stop iterating, retain only the objects which ...
mike rodent's user avatar
  • 14.4k
0 votes
0 answers
23 views

How to send data from a list loaded by <s:iterator> [duplicate]

I was wondering that how could I get one of my data from a list loaded by <s:iterator>. iπ can see all of them on the website but I want to modify one of them. Below is my JSP page code. I want ...
馮永定's user avatar
0 votes
1 answer
58 views

Iterator's predicate returns Result<bool, E>, how do I stop iteration and return the error

type E = String; fn helper() -> Result<char, E> { let pred = |x| -> Result<bool, E> { if x % 3 { Ok(x % 2) } else { Err("error"....
Tom Huntington's user avatar
0 votes
1 answer
75 views

Binary Tree Iterator in C++ works for T=int but Fails for T=std::string

I have implemented a binary tree with an iterator in C++: #include <iostream> #include <memory> #include <iterator> #include <iostream> namespace binary_search_tree { ...
Marlon's user avatar
  • 1
0 votes
1 answer
67 views

How can I use Rayon to parallelize iteration over iter::from_fn?

I have a function that I'm using to generate an iterator using iter::from_fn, as described in this answer. fn generate_iterator<'a>( some_arg: Box<dyn SomeTrait>, some_other_arg: &...
Miguel Guthridge's user avatar
0 votes
1 answer
81 views

How to Pass Value to Iterator

In JavaScript, we have the yield operator, which is able to return a value optionally provided to the generator via the next() method. From next: Parameters value Optional The value to send to the ...
Bondolin's user avatar
  • 2,881
1 vote
2 answers
53 views

Issue with summing over map closure

I'm working on AoC to learn Rust and have been trying to cobble together a solution based on what I've seen more experienced rust programmers write. Below seems to compile entirely except I have an ...
thefrollickingnerd's user avatar
0 votes
1 answer
49 views

How to iterate backwards with a custom iterator in Java using permutations

I am trying to create a a class of Permutation that extends Iterator and adds two methods: previous() and hasPrevious(). It must offer a constructor with one argument: a string containing the ordered ...
Kate Johnson's user avatar
-2 votes
0 answers
164 views

Cannot decrement arr.end() for a C++ array [duplicate]

For a C++ std::array a2, if I try to decrement a2.end() I get compilation error error: lvalue required as decrement operand. Why is that? std::array::end presumably returns a random access iterator, ...
sancho.s ReinstateMonicaCellio's user avatar
-1 votes
1 answer
78 views

altering order of list c++

if I have a list of items defined, and I wanted to change or change the order one of the chosen items to go up in the list, how do i implement that? The definition of the function should be as follows:...
user avatar
1 vote
1 answer
64 views

Unable to bound lifetime in for each loop using str.lines() function

I'm new to rust and i'm building an implementation of the grep command to learn this language. I'm trying to create a function to match the search query in a case insensitive way, but i'm having ...
Alex Licata's user avatar
1 vote
1 answer
67 views

Iterating over different collections in an Enum

I have an enum with two variants. One of these variants contains a Vec and the other contains a BTreeMap. I'd like to be able to iterate over these two contains. Actually, I like to "massage"...
DHP's user avatar
  • 39
0 votes
1 answer
35 views

Rust collect iterator to new vector when randomly selecting elements between two vectors

Given two same length Vec<u8>, trying to select an element from each index with a given probability p: f32, as follows, fn select(xs: &Vec<u8>, ys: &Vec<u8>, p: f32) -> ...
iris's user avatar
  • 381
0 votes
1 answer
69 views

kdb/Q How to iterate on a function x number of times?

I'm new to kdb/Q, what is the equivalent of iteration, where I need to run a function 10 times? In Python, it would be something like creating some kind of iterator, using a while loop to run the ...
mmv456's user avatar
  • 25
0 votes
2 answers
66 views

call a new function without closing the file

I have a method that opens a csv file and another method that I want to use to process this file -but I also need it to process different types of objects, so I want to keep it as an independent ...
invalid syntax's user avatar
0 votes
1 answer
86 views

Trying to implement begin/end with static_cast in C++

I'm trying to implement an object to iterate easily an array, without static_cast each element separately (I have to do it hundreds of times). It works when I use the classic for loop, but when I ...
Juan JuezSarmiento's user avatar
1 vote
2 answers
52 views

Implement a trait for Iterator over values or ref

I am trying to implement a trait that works for Iterator and Iterator<&u32> seamlessly, and while I did manage to find a solution I am not sure that is the best one, or even a good one. I ...
Eily's user avatar
  • 121
4 votes
2 answers
89 views

Cost of cloning iterator originating from into_iter() in Rust?

I'm trying to figure out what is the cost of cloning iterators that originate from into_iter() in Rust, but can't find anything meaningful. Consider the code like this: let v = vec![1,2,3,4,...]; // ...
yesint's user avatar
  • 187
0 votes
2 answers
78 views

In nested iterators accessing vector by index does not work

I have a strange behavior that I cannot explain to myself. If want to create new Strings by iterating over two Vec<String>'s, but somehow access by index doesn't work, because I borrow stuff. My ...
exocortex's user avatar
  • 389

1
2 3 4 5
291