diff --git a/README.adoc b/README.adoc index 768e822..e1c30ce 100644 --- a/README.adoc +++ b/README.adoc @@ -20213,6 +20213,13 @@ Programs under link:userland/cpp/[] are examples of https://en.wikipedia.org/wik *** link:userland/cpp/multimap.cpp[]: `std::multimap` ** <> contains a benchmark comparison of different c++ containers +[[cpp-standards]] +==== C++ standards + +Like for C, you have to pay for the standards... insane. So we just use the closest free drafts instead. + +https://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents + [[cpp-initialization-types]] ==== C++ initialization types @@ -20470,13 +20477,6 @@ https://stackoverflow.com/questions/51031060/are-c17-parallel-algorithms-impleme link:userland/cpp/parallel_sort.cpp[] -[[cpp-standards]] -==== C++ standards - -Like for C, you have to pay for the standards... insane. So we just use the closest free drafts instead. - -https://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents - [[cpp17]] ===== C++17 N4659 standards draft @@ -20517,6 +20517,20 @@ Replaces decltype with type of an expression at compile time. More powerful than `auto` as you can use it in more places. +[[cpp-concepts]] +==== C++ concepts + +[[cpp-iterators]] +===== C++ iterators + +https://stackoverflow.com/questions/37031805/preparation-for-stditerator-being-deprecated/38103394 + +link:userland/cpp/custom_iterator.cpp[]: there is no way to easily define a nice custom iterator, you just have to wrap existing iterators and add a gazillion wrapper methods: + +* https://stackoverflow.com/questions/8054273/how-to-implement-an-stl-style-iterator-and-avoid-common-pitfalls +* https://stackoverflow.com/questions/3582608/how-to-correctly-implement-custom-iterators-and-const-iterators +* https://stackoverflow.com/questions/6471019/can-should-i-inherit-from-an-stl-iterator + === POSIX Programs under link:userland/posix/[] are examples of POSIX C programming. diff --git a/userland/cpp/custom_iterator.cpp b/userland/cpp/custom_iterator.cpp new file mode 100644 index 0000000..7f1e98c --- /dev/null +++ b/userland/cpp/custom_iterator.cpp @@ -0,0 +1,50 @@ +// https://cirosantilli.com/linux-kernel-module-cheat#cpp-iterators + +#include +#include +#include + +class MyMap { + public: + std::map map; + class iterator { + using It = decltype(map)::iterator; + It it; + public: + using difference_type = It::difference_type; + using value_type = It::value_type; + using pointer = It::pointer; + using reference = It::reference; + using iterator_category = std::bidirectional_iterator_tag; + + iterator(It begin) : it(begin) {} + iterator& operator++() {it++; return *this;} + iterator operator++(int) {auto retval = *this; ++(*this); return retval;} + iterator& operator--() {it--; return *this;} + iterator operator--(int) {auto retval = *this; --(*this); return retval;} + bool operator==(iterator other) const { return it == other.it; } + bool operator!=(iterator other) const { return !(*this == other); } + value_type operator*() { + auto pair = *it; + return std::make_pair(2*pair.first, 3*pair.second); + } + }; + iterator begin() { return iterator(map.begin()); } + iterator end() { return iterator(map.end()); } +}; + +int main() { + MyMap map; + map.map.emplace(0, 10); + map.map.emplace(1, 11); + map.map.emplace(2, 12); + + auto it = map.begin(); + assert((*it++ == std::pair(0, 30))); + assert((*it++ == std::pair(2, 33))); + assert((*it++ == std::pair(4, 36))); + + for (const auto& v : map) { + std::cout << v.first << " " << v.second << std::endl; + } +}