Speed accessing a std::vector by iterator vs by operator[]/index?

The performance difference is likely negligable or none (the compiler might optimise them to be identical); you should worry about other things, like whether your program is correct (a slow but correct program is better than a fast and incorrect program). There are other advantages to using iterators though, such as being able to change … Read more

How to create a generator/iterator with the Python C API?

Below is a simple implementation of module spam with one function myiter(int) returning iterator: import spam for i in spam.myiter(10): print i prints numbers from 0 to 9. It is simpler then your case but shows main points: defining object with standard __iter__() and next() methods, and implementing iterator behaviour including raising StopIteration when appropriate. … Read more

C#: yield return within a foreach fails – body cannot be an iterator block

You can only use yield return in a function that returns an IEnumerable or an IEnumerator, not a List<T>. You need to change your function to return an IEnumerable<DesktopComputer>. Alternatively, you can rewrite the function to use List<T>.ConvertAll: return GetComputerIdTags().ConvertAll(pcTag => new DesktopComputer() { AssetTag = pcTag, Description = “PC ” + pcTag, AcquireDate = … Read more

C++ iterators & loop optimization

The two versions are not the same though. In the second version it compares the iterator against list.end() every time, and what list.end() evaluates to might change over the course of the loop. Now of course, you cannot modify list through the const_iterator it; but nothing prevents code inside the loop from just calling methods … Read more

What’s the most idiomatic way of working with an Iterator of Results? [duplicate]

There are lots of ways you could mean this. If you just want to panic, use .map(|x| x.unwrap()). If you want all results or a single error, collect into a Result<X<T>>: let results: Result<Vec<i32>, _> = result_i32_iter.collect(); If you want everything except the errors, use .filter_map(|x| x.ok()) or .flat_map(|x| x). If you want everything up … Read more

How to retrieve value type from iterator in C++?

typename std::iterator_traits<Iter>::value_type This will work if your template is instantiated with Iter as a pointer type. By the way, typename isn’t part of the type itself. It tells the compiler that value_type really is a type. If it were the name of a function or a static data member, then that affects the syntax. The … Read more