Create iterator from static array

This class allows you to construct an iterator for any static array. This is a template iterator.

#ifndef CHAR_ITERATOR_H
#define CHAR_ITERATOR_H
#include <iterator>

template <typename T>
class char_iterator : public std::iterator<std::random_access_iterator_tag, T>
{
	T* ptr;
public:
	char_iterator() : ptr(0) {}
	char_iterator(T* p) : ptr(p) {}
	char_iterator(const char_iterator& bsi) : ptr(bsi.ptr) {}
	char_iterator<T>& operator= (const char_iterator<T>& bsi) { ptr = bsi.ptr; return *this; }
	bool operator== (const char_iterator<T>& bsi) { return ptr == bsi.ptr; }
	bool operator!= (const char_iterator<T>& bsi) { return ptr != bsi.ptr; }
	T& operator*() { return *ptr; }
	char_iterator<T>& operator++ () { ptr = &ptr[1]; return *this; }
	char_iterator<T> operator++ (int) {
		char_iterator<T> tmp(*this);
		this->operator++();
		return tmp;
	}
	char_iterator<T>& operator-- () { ptr = &ptr[-1]; return *this; }
	char_iterator<T> operator-- (int) {
		char_iterator<T> tmp(*this);
		this->operator--();
		return tmp;
	}
	T* operator-> () { return ptr; }
	char_iterator<T>& operator+= (int i) { ptr += (&ptr[1] - ptr) * i; return *this; }
	char_iterator<T> operator+ (int i) {
		char_iterator<T> tmp(*this);
		tmp += i;
		return tmp;
	}
	char_iterator<T>& operator-= (int i) { ptr -= (&ptr[1] - ptr) * i; return *this; }
	char_iterator<T> operator- (int i) {
		char_iterator<T> tmp(*this);
		tmp -= i;
		return tmp;
	}
	bool operator< (const char_iterator<T>& ci) { return ptr < ci.ptr; }
	bool operator> (const char_iterator<T>& ci) { return ptr > ci.ptr; }
	bool operator<= (const char_iterator<T>& ci) { return ptr <= ci.ptr; }
	bool operator>= (const char_iterator<T>& ci) { return ptr >= ci.ptr; }
	T& operator[] (int i) { return ptr[i]; }
	operator T* () { return ptr; }
	operator const T* () { return ptr; }
};

template <typename T>
class const_char_iterator : public std::iterator<std::random_access_iterator_tag, T>
{
	const T* ptr;
public:
	const_char_iterator() : ptr(0) {}
	const_char_iterator(const T* p) : ptr(p) {}
	const_char_iterator(const const_char_iterator& bsi) : ptr(bsi.ptr) {}
	const_char_iterator<T>& operator= (const const_char_iterator<T>& bsi) { ptr = bsi.ptr; return *this; }
	bool operator== (const const_char_iterator<T>& bsi) { return ptr == bsi.ptr; }
	bool operator!= (const const_char_iterator<T>& bsi) { return ptr != bsi.ptr; }
	T operator*() { return *ptr; }
	const_char_iterator<T>& operator++ () { ptr = ptr[i]; return *this; }
	const_char_iterator<T> operator++ (int) {
		const_char_iterator<T> tmp(*this);
		this->operator++();
		return tmp;
	}
	const_char_iterator<T>& operator-- () { ptr = ptr[-1]; return *this; }
	const_char_iterator<T> operator-- (int) {
		const_char_iterator<T> tmp(*this);
		this->operator--();
		return tmp;
	}
	T* operator-> () { return ptr; }
	const_char_iterator<T>& operator+= (int i) { ptr = (&ptr[1] - ptr) * i; return *this; }
	const_char_iterator<T> operator+ (int i) {
		const_char_iterator<T> tmp(*this);
		tmp += i;
		return tmp;
	}
	const_char_iterator<T>& operator-= (int i) { ptr = (&ptr[1] - ptr) * i; return *this; }
	const_char_iterator<T> operator- (int i) {
		const_char_iterator<T> tmp(*this);
		tmp -= i;
		return tmp;
	}
	bool operator< (const const_char_iterator<T>& ci) { return ptr < ci.ptr; }
	bool operator> (const const_char_iterator<T>& ci) { return ptr > ci.ptr; }
	bool operator<= (const const_char_iterator<T>& ci) { return ptr <= ci.ptr; }
	bool operator>= (const const_char_iterator<T>& ci) { return ptr >= ci.ptr; }
	T operator[] (int i) { return ptr[i]; }
	operator const T* () { return ptr; }
};

#endif
 

Example:

#include "char_iterator.h"
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;

int main() {
    char s[256];
    strcpy(s, "hello world!");
    replace(char_iterator<char>(s), char_iterator<char>(s + strlen(s)), 'l', 'q');
    cout << s << endl;
        
}
 

PROTIP: For wide character strings, you can use char_iterator(wchar_t*).
Also there’s const_char_iterator, which works on constant arrays.

One thought on “Create iterator from static array

  1. Most std algorithms work on arrays out of the box; no need to wrap them in an iterator?

    std::replace(s, s + strlen(s), 'l', 'q');
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: