473,396 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Subclassing Off Of Vector

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>::operator[](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?

Aug 8 '06 #1
3 2690
In article <11**********************@n13g2000cwa.googlegroups .com>,
ja******@gmail.com wrote:
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 would either inherit privately, or contain a vector.
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>::operator[](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?
typename vector<T>::iterator
Aug 8 '06 #2
On 8 Aug 2006 11:31:37 -0700 in comp.lang.c++, ja******@gmail.com
wrote,
> vector<T>::iterator iter = end();
Should be:
typename vector<T>::iterator iter = end();

(Don't know if that will solve your whole problem.)
Aug 8 '06 #3

<ja******@gmail.comwrote in message
news:11**********************@n13g2000cwa.googlegr oups.com...
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>::operator[](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?
Now you have another class which is the same as std::vector except it
implements your favorite algorithm. When you come up with a second algorithm
are you going to invert a third class? This is a Bad Idea.

Another way to look at is this: what does data storage have to do with a
specific way to keep things sorted? Abolutely nothing.

Algorithms in C++ are best coded as functions if at all possible. For
instance:

template <typename COLL, typename OP>
void
insert_sorted(COLL &c, typename COLL::value_type elem, OP op)
{
typename COLL::iterator i = c.end();
typename COLL::iterator k = i;
--k;
for (typename COLL::size_type j = 0; j < c.size(); ++j)
{
if (op(*k, elem))
{
c.insert(i, elem);
return;
}
--i;
--k;
}
c.insert(c.begin(), elem);
}

(untested)

If this works at all it will work with std::list and std::deque as well as
std::vector. You can use std::greater<and std::less<to get decreasing or
increasing sort order. To use it call

std::less<intop;
insert_sorted(my_vector, 2, op);
insert_sorted(my_vector, 7, op);

and so forth. If you come up with a second algorithm you can code it the
same way without generating spurious classes.

Cy
Aug 8 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Chris Cioffi | last post by:
Hello all! I'm trying to subclass int such that once it reaches a certain value, it flips back to the starting count. This is for use as an X12 control number. Subclassing int and getting the...
2
by: BJörn Lindqvist | last post by:
A problem I have occured recently is that I want to subclass builtin types. Especially subclassing list is very troublesome to me. But I can't find the right syntax to use. Take for example this...
11
by: Brent | last post by:
I'd like to subclass the built-in str type. For example: -- class MyString(str): def __init__(self, txt, data): super(MyString,self).__init__(txt) self.data = data
1
by: Pierre | last post by:
Folks, I'm pretty new to OOP, so I still have problems with inheritance and delegation. I defined 2 Numeric MaskedArray subclasses, as: class Temp(MA,object): def __init__(self,data): self =...
5
by: TG | last post by:
Hi. i've already something about inheriting from array a few weeks ago and had my answer. But again, there is something that I don't understand. Here is my vector class, which works quite well :...
16
by: manatlan | last post by:
I've got an instance of a class, ex : b=gtk.Button() I'd like to add methods and attributes to my instance "b". I know it's possible by hacking "b" with setattr() methods. But i'd like to do...
5
by: Ray | last post by:
Hi all, I am thinking of subclassing the standard string class so I can do something like: mystring str; .... str.toLower (); A quick search on this newsgroup has found messages by others
9
by: mrstevegross | last post by:
I ran into a weird behavior with lexical scope in Python. I'm hoping someone on this forum can explain it to me. Here's the situation: I have an Outer class. In the Outer class, I define a...
0
by: Ethan Furman | last post by:
Hamish McKenzie wrote: Greetings! I am not sure of the proper way to fix this issue, but the problem you have is that lists do not have a __new__ method: --list <type 'list'>
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.