Cannot use .begin() or .end() on an array

Arrays have no member functions as they aren’t a class type. This is what the error is saying. You can use std::begin(arr) and std::end(arr) from the <iterator> header instead. This also works with types that do have .begin() and .end() members, via overloading: #include <array> #include <vector> #include <iterator> int main() { int c_array[5] = … Read more

Some help understanding “yield”

A method using yield return must be declared as returning one of the following two interfaces: IEnumerable<SomethingAppropriate> IEnumerator<SomethingApropriate> (thanks Jon and Marc for pointing out IEnumerator) Example: public IEnumerable<AClass> YourMethod() { foreach (XElement header in headersXml.Root.Elements()) { yield return (ParseHeader(header)); } } yield is a lazy producer of data, only producing another item after the … Read more

Can/Should I inherit from an STL iterator?

Short answer Many consider that the class std::iterator does not offer much compared to regular type aliases, and even obfuscates them a bit by not explicitly providing the names and relying on the order of the template parameters instead. It is deprecated in C++17 and is likely to be gone in a few years. This … Read more

Does std::map::iterator return a copy of value or a value itself?

I want to add a follow up answer to this question for people using C++11 iterators… The following code: std::map<std::string, std::string> m({{“a”,”b”},{“c”,”d”}}); for (auto i : m) { std::cout << i.first << “: ” << i.second << std::endl; } does copy the key and value since “auto” is a value by default, not a const … Read more

what’s the point of having both Iterator.forEachRemaining() and Iterable.forEach()?

To understand why the two methods both exist, you need to first understand what are Iterator and Iterable. An Iterator basically is something that has a “next element” and usually, an end. An Iterable is something that contains elements in a finite or infinite sequence and hence, can be iterated over by keep getting the … Read more

Is end() required to be constant in an STL map/set?

You write (emphasis by me): ยง23.1.2.8 in the standard states that insertion/deletion operations on a set/map will not invalidate any iterators to those objects (except iterators pointing to a deleted element). Actually, the text of 23.1.2/8 is a bit different (again, emphasis by me): The insert members shall not affect the validity of iterators and … Read more