Created
November 13, 2013 02:10
-
-
Save dgodfrey206/7442430 to your computer and use it in GitHub Desktop.
An almost complete implementation of an iterator designed for C-style arrays.
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 <iostream> | |
#include <vector> | |
template <class T, class CharT = char, class Traits = std::char_traits<char>> | |
class array_iterator : public std::iterator<std::output_iterator_tag, | |
void, void, void, void> | |
{ | |
public: | |
array_iterator(T* list) | |
: array(list) { } | |
array_iterator& operator=(const T& val) | |
{ | |
array[pos] = val; | |
return *this; | |
} | |
array_iterator& operator++() { ++pos; return *this; } | |
array_iterator& operator*() { return *this; } | |
private: | |
T* array; | |
int pos = 0; | |
}; | |
int main() | |
{ | |
int x[10]; | |
std::vector<int> v{1, 2, 3}; | |
std::copy(v.begin(), v.end(), array_iterator<int>(x)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment