[ create a new paste ] login | about

Link: http://codepad.org/Bpm179Gv    [ raw code | output | fork ]

k06a - C++, pasted on May 14:
#include <iostream>
#include <fstream>
#include <iterator>

using namespace std;

template<typename T>
class bit_iterator : public iterator<input_iterator_tag, T>
{
    istream_iterator<T> * it;
    int pos;
    int value;

public:
    bit_iterator(istream_iterator<T> * it) : it(it), pos(0) {}
    bit_iterator(const bit_iterator & bit) : it(bit.it), pos(0) {}
    
    bool operator == (const bit_iterator & rhs) { return *it == *rhs.it; }
    bool operator != (const bit_iterator & rhs) { return *it != *rhs.it; }

    bit_iterator & operator ++ ()
    {
        pos++;
        if (pos == 8*sizeof(T))
        {
            ++(*it);
            pos = 0;
        }
        return *this;
    }

    bit_iterator operator ++ (int) 
    {
        bit_iterator tmp(*this);
        operator++();
        return tmp;
    }

    int & operator * ()
    {
        return value = ((**it) >> pos) & 1;
    }
};

int main(int argc, char * argv[])
{
    fstream file(argv[0], ios_base::in);

    bit_iterator<char> it(new istream_iterator<char>(file));
    bit_iterator<char> eof(new istream_iterator<char>());

    cout << count(it,eof,1);
}


Output:
1
234296


Create a new paste based on this one


Comments: