mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-22 17:55:57 +01:00
custom_iterator
This commit is contained in:
28
README.adoc
28
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`
|
||||
** <<algorithms>> 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.
|
||||
|
||||
50
userland/cpp/custom_iterator.cpp
Normal file
50
userland/cpp/custom_iterator.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
// https://cirosantilli.com/linux-kernel-module-cheat#cpp-iterators
|
||||
|
||||
#include <cassert>
|
||||
#include <map>
|
||||
#include <iostream>
|
||||
|
||||
class MyMap {
|
||||
public:
|
||||
std::map<int,int> 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<const int, int>(0, 30)));
|
||||
assert((*it++ == std::pair<const int, int>(2, 33)));
|
||||
assert((*it++ == std::pair<const int, int>(4, 36)));
|
||||
|
||||
for (const auto& v : map) {
|
||||
std::cout << v.first << " " << v.second << std::endl;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user