J
jackstah
I've read before that subclassing off of std::vector is a bad idea
because it's a concrete class. I understand, but there shouldnt be any
issues with this. I just wanted to do a simple example in my project
to combine templates and inheritance so that I would brush up on both.
I have data that is mostly sorted, but there might be some flipflops
here and there. Thus I've subclassed off of vector, and written a
class that, given a comparison function, inserts in sorted order by
linear searching from the back. This is better for me than a b-search
because 99% of the time I want to push_back, and even if I don't, its
just one or two back. Since the class is so short, I include the whole
thing:
*************
#include <vector>
using namespace std;
template <class T>
class SortedVector : public vector<T> {
typedef int (*cmpFn)(const T&, const T&);
public:
SortedVector(cmpFn func) : vector<T>(), myFunc(func) {}
~SortedVector() {}
void push(const T& elem) {
vector<T>::iterator iter = end();
for (int i = vector<T>::size() - 1; i >= 0; i--) {
if (myFunc(vector<T>:perator[](i), elem) >= 0) {
vector<T>::insert(iter, elem);
return;
}
iter--;
}
vector<T>::insert(iter, elem);
}
private:
cmpFn myFunc;
};
************
My problem is with the iterators. I get two errors: "iter was not
declared in this scope" (at every all to iter), and "expected ; before
iter" (in what should be the declaration). I tried initially using
just int's (i.e. insert(i + 1, elem)) but that didn't work either.
Without shifting to the encapsualtion paradigm, what can I do?
because it's a concrete class. I understand, but there shouldnt be any
issues with this. I just wanted to do a simple example in my project
to combine templates and inheritance so that I would brush up on both.
I have data that is mostly sorted, but there might be some flipflops
here and there. Thus I've subclassed off of vector, and written a
class that, given a comparison function, inserts in sorted order by
linear searching from the back. This is better for me than a b-search
because 99% of the time I want to push_back, and even if I don't, its
just one or two back. Since the class is so short, I include the whole
thing:
*************
#include <vector>
using namespace std;
template <class T>
class SortedVector : public vector<T> {
typedef int (*cmpFn)(const T&, const T&);
public:
SortedVector(cmpFn func) : vector<T>(), myFunc(func) {}
~SortedVector() {}
void push(const T& elem) {
vector<T>::iterator iter = end();
for (int i = vector<T>::size() - 1; i >= 0; i--) {
if (myFunc(vector<T>:perator[](i), elem) >= 0) {
vector<T>::insert(iter, elem);
return;
}
iter--;
}
vector<T>::insert(iter, elem);
}
private:
cmpFn myFunc;
};
************
My problem is with the iterators. I get two errors: "iter was not
declared in this scope" (at every all to iter), and "expected ; before
iter" (in what should be the declaration). I tried initially using
just int's (i.e. insert(i + 1, elem)) but that didn't work either.
Without shifting to the encapsualtion paradigm, what can I do?