Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
Created November 13, 2013 02:10
Show Gist options
  • Save dgodfrey206/7442430 to your computer and use it in GitHub Desktop.
Save dgodfrey206/7442430 to your computer and use it in GitHub Desktop.
An almost complete implementation of an iterator designed for C-style arrays.
#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