Pair-wise iteration in C#, or sliding window enumerator
In .NET 4 this becomes even easier:- var input = new[] { “a”, “b”, “c”, “d”, “e”, “f” }; var result = input.Zip(input.Skip(1), (a, b) => Tuple.Create(a, b));
In .NET 4 this becomes even easier:- var input = new[] { “a”, “b”, “c”, “d”, “e”, “f” }; var result = input.Zip(input.Skip(1), (a, b) => Tuple.Create(a, b));
It works if the iterator is a random access iterator, which vector’s iterators are (see reference). The STL function std::advance can be used to advance a generic iterator, but since it doesn’t return the iterator, I tend use + if available because it looks cleaner. C++11 note Now there is std::next and std::prev, which do … Read more
EDIT: This same trick was already posted by someone else in a previous thread. It is easy to have std::istream_iterator do what you want: namespace detail { class Line : std::string { friend std::istream & operator>>(std::istream & is, Line & line) { return std::getline(is, line); } }; } template<class OutIt> void read_lines(std::istream& is, OutIt dest) … Read more
There are several approaches, each with their own pros and cons. Below three approaches with a cost-benefit analysis. ADL through custom non-member begin() / end() The first alternative provides non-member begin() and end() function templates inside a legacy namespace to retrofit the required functionality onto any class or class template that can provide it, but … Read more
To be able to use range-based for, your class should provide const_iterator begin() const and const_iterator end() const members. You can also overload the global begin function, but having a member function is better in my opinion. iterator begin() and const_iterator cbegin() const are also recommended, but not required. If you simply want to iterate … Read more
[The best answer was, unfortunately, deleted by a moderator because it was a link-only answer. I understand why link-only answers are discouraged; deleting it, however, has robbed future seekers of very useful information. The link has remained stable for more than seven years and continues to work at the time of this writing.] I strongly … Read more
Like this: std::string s(“Test string”); std::string::iterator it = s.begin(); //Use the iterator… ++it; //… std::cout << “index is: ” << std::distance(s.begin(), it) << std::endl;
map.entrySet().removeIf(entry -> entry.getValue().equals(“0”)); You can’t do it with streams, but you can do it with the other new methods. EDIT: even better: map.values().removeAll(Collections.singleton(“0”));
To understand what is happening here it is helpful to understand how for loops work in Rust. Basically a for loop is a short hand for using an iterator, so: for item in some_value { // … } is basically a short-hand for let mut iterator = some_value.into_iter(); while let Some(item) = iterator.next() { // … Read more
Your main problem is that you are calling a method called first() in the iterator. What you are meant to do is use the property called first: …append(iter->first) rather than …append(iter->first()) As a matter of style, you shouldn’t be using new to create that string. std::string something::toString() { std::map<std::string, std::string>::iterator iter; std::string strToReturn; //This is … Read more