iterator

Iterator-producing function in Nim: works when assigning the iterator, stuck when calling it directly

寵の児 提交于 2021-01-27 12:12:59
问题 I tried to make a procedure that creates an iterator, as follows: proc makeCDFrom(start: int): iterator(): int = result = iterator(): int = var i: int = start while i >= 0: echo "i:", i yield(i) dec(i) let cdFrom6 = makeCDFrom(6) for j in cdFrom6(): echo "j:", j This appears to work as expected: i:6 j:6 i:5 j:5 i:4 j:4 i:3 j:3 i:2 j:2 i:1 j:1 i:0 j:0 However, initially, I had tried with this slight variation: proc makeCDFrom(start: int): iterator(): int = result = iterator(): int = var i: int

Arithmetic on end() iterator

[亡魂溺海] 提交于 2021-01-26 09:15:35
问题 Let A be a std::vector<double> , Is this well-defined? if(!A.empty()) std::vector<double>::iterator myBack = A.end() - 1; Is the end iterator only good for equalities and inequalities checks? Or I can perform some pointer arithmetic as long as I remain in the container? On my platform this code works. I'm wondering if this is portable. 回答1: It is perfectly valid as vector::iterator is a random access iterator. You can perform arithmetic operations on it and it is not platform dependent. std:

Arithmetic on end() iterator

此生再无相见时 提交于 2021-01-26 09:11:50
问题 Let A be a std::vector<double> , Is this well-defined? if(!A.empty()) std::vector<double>::iterator myBack = A.end() - 1; Is the end iterator only good for equalities and inequalities checks? Or I can perform some pointer arithmetic as long as I remain in the container? On my platform this code works. I'm wondering if this is portable. 回答1: It is perfectly valid as vector::iterator is a random access iterator. You can perform arithmetic operations on it and it is not platform dependent. std:

How to use std::copy for printing a user defined type

为君一笑 提交于 2021-01-24 12:58:06
问题 Below is the code that works perfectly for print the values of type std::string std::vector<std::string> v; v.push_back("this"); v.push_back("is"); v.push_back("a"); v.push_back("test"); std::copy(v.begin(),v.end(),std::ostream_iterator<std::string>(std::cout,",")); But when I am trying to print a user defined type (a structure), code is not compiling: struct Rec { int name; int number; int result; }; int main() { Rec rec1 = {1,1,1}; Rec rec2 = {2,1,1}; Rec rec3 = {3,1,1}; Rec rec4 = {4,1,1};

Implementation of template of a << operator // C++

风格不统一 提交于 2021-01-24 07:00:35
问题 I would want to make a template of a << operator in C++, that would show a Object that is a "range" (by that i mean any object like : std::vector, std::set, std::map, std::deque). How can i achieve this? I've been googling and looking in docs for a few days now, but without any effect. I've been doing few templates and been overriding few operators before, but these were inside of a certain class that was representing a custom vector class. I cant seem to find a good way of implementing this,

Implementation of template of a << operator // C++

我与影子孤独终老i 提交于 2021-01-24 06:57:13
问题 I would want to make a template of a << operator in C++, that would show a Object that is a "range" (by that i mean any object like : std::vector, std::set, std::map, std::deque). How can i achieve this? I've been googling and looking in docs for a few days now, but without any effect. I've been doing few templates and been overriding few operators before, but these were inside of a certain class that was representing a custom vector class. I cant seem to find a good way of implementing this,

What's the difference between a sentinel and an end iterator?

这一生的挚爱 提交于 2021-01-21 12:19:08
问题 While reading Eric Niebler's range proposal, I've come across the term sentinel as replacement for the end iterator. I'm having a difficult time understanding the benefits of sentinel over an end iterator. Could someone provide a clear example of what sentintel brings to the table that cannot be done with standard iterator pairs? "A sentinel is an abstraction of a past-the-end iterator. Sentinels are Regular types that can be used to denote the end of a range. A sentinel and an iterator

How to properly implement Iterable structure in Rust? [duplicate]

╄→гoц情女王★ 提交于 2021-01-21 08:11:28
问题 This question already has answers here : How to implement Iterator and IntoIterator for a simple struct? (2 answers) Closed 1 year ago . I'm trying to implement a structure that can be infinitely iterated. Think it like a natural number. I have a limitation: it can't implement Copy trait because the structure contains a String field. I've also implemented an Iterable trait and its only member fn next(&mut self) -> Option<Self::Item> . Currently, I have the following code to iterate over the

Use all but the last element from an iterator

浪子不回头ぞ 提交于 2021-01-21 07:12:09
问题 I want to split a Vec into some parts of equal length, and then map over them. I have an iterator resulting from a call to Vec 's chunks() method. This may leave me with a part that will be smaller than other parts, which will be the last element generated by it. To be sure that all parts have equal length, I just want to drop that last element and then call map() on what's left. 回答1: As Sebastian Redl points out, checking the len gth of each chunk is the better solution for your specific

Use all but the last element from an iterator

对着背影说爱祢 提交于 2021-01-21 07:11:15
问题 I want to split a Vec into some parts of equal length, and then map over them. I have an iterator resulting from a call to Vec 's chunks() method. This may leave me with a part that will be smaller than other parts, which will be the last element generated by it. To be sure that all parts have equal length, I just want to drop that last element and then call map() on what's left. 回答1: As Sebastian Redl points out, checking the len gth of each chunk is the better solution for your specific