Created
August 21, 2018 19:01
-
-
Save kumailxp/6a7c5ddaec5a5e7e3d3dae02007d196c to your computer and use it in GitHub Desktop.
implementing an iterator
This file contains 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
//============================================================================ | |
// Name : class_02.cpp | |
// Author : Kumail Ahmed | |
// Version : | |
// Copyright : Your copyright notice | |
// Description : Hello World in C++, Ansi-style | |
//============================================================================ | |
#include <iostream> | |
#include <iterator> | |
#include <numeric> | |
using namespace std; | |
template<typename T> | |
class RefIter { | |
public: | |
// Iterator traits, previously from std::iterator. | |
using value_type = T; | |
using difference_type = std::ptrdiff_t; | |
using pointer = T*; | |
using reference = T&; | |
using iterator_category = std::forward_iterator_tag; | |
RefIter() = default; | |
RefIter(int* input_ref, int _size) : size(_size) { | |
it_refs = new T(_size); | |
for(int i = 0; i < _size; i++) { | |
*(it_refs + i ) = *(input_ref +i); | |
} | |
} | |
reference operator*() const { | |
return it_refs[current]; | |
} | |
pointer operator->() const { | |
return &it_refs[current]; | |
} | |
// Move position forward 1 place | |
RefIter& operator++() { | |
++current; | |
return *this; | |
} | |
RefIter operator++(int) { | |
RefIter tmp = *this; | |
++current; | |
return tmp; | |
} | |
// Comparison operators | |
bool operator== (const RefIter& rhs) { | |
return current == rhs.current; | |
} | |
bool operator!= (const RefIter& rhs) { | |
return (current < size); | |
} | |
private: | |
T* it_refs; | |
int size; | |
int current {0}; | |
}; | |
class Book { | |
private: | |
int bookID; | |
int* refs; | |
int size; | |
public: | |
Book(int _bookID, int* _refs, int _size) : bookID(_bookID), size(_size) { | |
refs = new int(_size); | |
refs = _refs; | |
} | |
Book() { | |
bookID=-1; | |
size = 0; | |
refs = nullptr; | |
} | |
Book(const Book& b) { | |
bookID = b.bookID; | |
size = b.size; | |
refs = new int(b.size); | |
for (int i = 0; i < b.size; i++) | |
*(refs + i) = *(b.refs + i); | |
} | |
void printRefs() const { | |
cout << "BookID: " << bookID << endl; | |
for(int i = 0 ; i < size; i++) { | |
cout << "refs values at " << i << " is " << *(refs+i) << endl; | |
} | |
} | |
void changeRefs(int *_refs, int _size) { | |
delete [] refs; | |
refs = new int(_size); | |
for (int i = 0; i < _size; i++) | |
*(refs+ i) = *(_refs + i); | |
size = _size; | |
} | |
friend void swap(Book& lhs, Book& rhs) { | |
using std::swap; | |
swap(lhs.bookID, rhs.bookID); | |
swap(lhs.size, rhs.size); | |
swap(lhs.refs, rhs.refs); | |
} | |
Book& operator=(Book b) { | |
swap(*this, b); | |
return *this; | |
} | |
~Book() { | |
delete[] refs; | |
} | |
using iterator = RefIter<int>; | |
iterator begin() const { return iterator(refs, size);} | |
iterator end() const { return iterator(nullptr, 0);} | |
}; | |
int main() { | |
int refIDs[] = {1,2,3,4,5}; | |
// int refIDs2[] = {10,20,30,40,50,60,70,80,90}; | |
Book b1(0, refIDs, 5); | |
// Book b3; | |
// b3 = b1; | |
// | |
// b3.printRefs(); | |
// cout << "=============\n"; | |
// b1.changeRefs(refIDs2, 9); | |
// b3 = b1; | |
// b3.printRefs(); | |
Book::iterator v0 = ++b1.begin(); | |
Book::iterator v1 = v0; | |
if(v0 == v1) | |
cout << "equal" << endl; | |
cout << "sum: " | |
<< accumulate(b1.begin(), b1.end(),0) << endl; | |
for(auto& BI : b1) | |
cout << "value is: " << BI << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment