Last active
October 13, 2015 20:17
-
-
Save kevinkreiser/b0920ee2c1de37f84d92 to your computer and use it in GitHub Desktop.
Make primative array iterable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <string> | |
| #include <iostream> | |
| template <class T> | |
| struct iterable_t { | |
| public: | |
| using iterator = T*; | |
| iterable_t(T* first, size_t size): head(first), tail(first + size), count(size){} | |
| iterable_t(T* first, T* end): head(first), tail(end), count(end - first){} | |
| T* begin() { return head; } | |
| T* end() { return tail; } | |
| size_t size() const { return count; } | |
| protected: | |
| T* head; | |
| T* tail; | |
| size_t count; | |
| }; | |
| int main() { | |
| int a[] = {1,2,3,4,5}; | |
| char b[] = {'a','b','c','d','e'}; | |
| std::string c[] = {"one","two","three","four","five"}; | |
| const size_t d[] = {11,12,13,14,15}; | |
| for(const auto& i : iterable_t<int>(a, 5)) | |
| std::cout << i << std::endl; | |
| for(const auto& i : iterable_t<char>(b, 5)) | |
| std::cout << i << std::endl; | |
| for(const auto& i : iterable_t<std::string>(c, 5)) | |
| std::cout << i << std::endl; | |
| iterable_t<const size_t> iterable(d, 5); | |
| for(iterable_t<const size_t>::iterator i = iterable.begin(); i != iterable.end(); ++i) | |
| std::cout << *i << std::endl; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment