iterator

c++ uint , unsigned int , int

穿精又带淫゛_ 提交于 2020-08-22 07:15:21
问题 Hi I have a program that deals alot with vectors and indexes of the elements of these vectors, and I was wondering: is there a difference between uint and unsigned int which is better to use one of the above types or just use int as I read some people say compiler does handle int values more efficiently, but if I used int I will have to check always for negative idxs which is pain. do you think iterators to be better? is it more efficient than normal indexing vectorx[idx] ? p.s the software

c++ uint , unsigned int , int

江枫思渺然 提交于 2020-08-22 07:15:12
问题 Hi I have a program that deals alot with vectors and indexes of the elements of these vectors, and I was wondering: is there a difference between uint and unsigned int which is better to use one of the above types or just use int as I read some people say compiler does handle int values more efficiently, but if I used int I will have to check always for negative idxs which is pain. do you think iterators to be better? is it more efficient than normal indexing vectorx[idx] ? p.s the software

Why does a class need __iter__() to return an iterator?

為{幸葍}努か 提交于 2020-08-20 18:26:20
问题 Why does a class need to define __iter__() returning self, to get an iterator of the class? class MyClass: def __init__(self): self.state = 0 def __next__(self): self.state += 1 if self.state > 4: raise StopIteration return self.state myObj = MyClass() for i in myObj: print(i) Console log: Traceback (most recent call last): for i in myObj: TypeError: 'MyClass' object is not iterable the answer https://stackoverflow.com/a/9884259/4515198, says An iterator is an object with a next (Python 2) or

Difference between iter() and into_iter() on a shared, borrowed Vec?

依然范特西╮ 提交于 2020-08-19 11:41:45
问题 I am reading the Rust 101 tutorial, where the author talks about shared borrowing with the example of a Vec object passed to a function. Below is a slightly adapted MWE of what the the tutorial is teaching. The interesting part is v.iter() in vec_min . The author writes: This time, we explicitly request an iterator for the vector v . The method iter borrows the vector it works on, and provides shared borrows of the elements. But what happens if I use a for ... in ... construction on an object