cgroza
March 4th, 2011, 01:05 PM
Hello everyone. I started to do a limited container just to test my skills after finishing the chapter about iterators in the book "The C++ programming language".
So my "container" basically uses a vector(by default, can be specified as a template argument) to hold its elements. I made this whole thing just to try to implement iterators for it.
Here is the container class code:
#include <iostream>
#include <vector>
using namespace std;
template <class T, class C> class Vec {
private:
typedef T val_type;
C vec;
struct ends {
val_type* first;
val_type* last;
};
ends extrem;
public:
template<class V = val_type>
class iterator {
private:
V* it;
public:
iterator(val_type* a):it(a) {
}
void operator=(val_type* a) {
it=a;
}
void operator++() {
++it;
}
val_type& operator*()const {
return *it;
}
bool operator<(val_type* y) {
return it < y;
}
};
Vec(const int nr):vec(nr) {
extrem.first = &vec[0];
extrem.last = &vec[-1];
}
val_type* end() const {
return extrem.last;
}
val_type* begin() const {
return extrem.first;
}
void push_back(const val_type& a) {
vec.push_back(a);
extrem.first = &vec[0];
extrem.first = &vec[-1];
}
val_type& operator[](int x) {
return vec.at(x);
}
int size() {
return vec.size();
}
};And here is my main:
int main()
{
Vec<int, vector<int> > test(10);
Vec<int, vector<int > >::iterator<int> p = test.begin();
for(int j = 0 ; j < test.size() ; j++) {
test[j] = j;
}
int i = 0;
while (i<test.size()) {
cout<<*p;
++p;
i++;
}
}With this main function, the thing prints out all the elements using my iterator.
The problem is that I want to do the loop with while(p<test.end()){...} like in the book, but the < operator returns something bad and the loop goes crazy and segfaults.
This is the < operator:
bool operator<(val_type* y) {
return it < y;
}Any ideas why? I am I doing something wrong?
So my "container" basically uses a vector(by default, can be specified as a template argument) to hold its elements. I made this whole thing just to try to implement iterators for it.
Here is the container class code:
#include <iostream>
#include <vector>
using namespace std;
template <class T, class C> class Vec {
private:
typedef T val_type;
C vec;
struct ends {
val_type* first;
val_type* last;
};
ends extrem;
public:
template<class V = val_type>
class iterator {
private:
V* it;
public:
iterator(val_type* a):it(a) {
}
void operator=(val_type* a) {
it=a;
}
void operator++() {
++it;
}
val_type& operator*()const {
return *it;
}
bool operator<(val_type* y) {
return it < y;
}
};
Vec(const int nr):vec(nr) {
extrem.first = &vec[0];
extrem.last = &vec[-1];
}
val_type* end() const {
return extrem.last;
}
val_type* begin() const {
return extrem.first;
}
void push_back(const val_type& a) {
vec.push_back(a);
extrem.first = &vec[0];
extrem.first = &vec[-1];
}
val_type& operator[](int x) {
return vec.at(x);
}
int size() {
return vec.size();
}
};And here is my main:
int main()
{
Vec<int, vector<int> > test(10);
Vec<int, vector<int > >::iterator<int> p = test.begin();
for(int j = 0 ; j < test.size() ; j++) {
test[j] = j;
}
int i = 0;
while (i<test.size()) {
cout<<*p;
++p;
i++;
}
}With this main function, the thing prints out all the elements using my iterator.
The problem is that I want to do the loop with while(p<test.end()){...} like in the book, but the < operator returns something bad and the loop goes crazy and segfaults.
This is the < operator:
bool operator<(val_type* y) {
return it < y;
}Any ideas why? I am I doing something wrong?