473,398 Members | 2,125 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,398 software developers and data experts.

Inherit vector class

I was running through a data structures book and I was curious if anyone could inform me of how to inherit the vector class to do a simple search and erase function. The example states:

vector_plus is a subclass of the vector class.
vector_plus has no new fields.
Define:

void erase_item(const T& item);

Here is an example of my header file:

Expand|Select|Wrap|Line Numbers
  1. #ifndef VECTOR_PLUS
  2. #define VECTOR_PLUS
  3. #include <vector>
  4. using namespace std;
  5.  
  6. template<class T>
  7. class vector_plus : public vector<T>
  8. {
  9.   public:
  10.     vector_plus();
  11.     void erase_item(const T& item);
  12. };
  13.  
  14. #endif
In the erase_item method I would use the find method defined in <algorithm> and the erase method in <vector>.

Thank you so much for your help.
Apr 15 '06 #1
3 4851
Banfa
9,065 Expert Mod 8TB
In the erase_item method I would use the find method defined in <algorithm> and the erase method in <vector>.
Did you try it? Did it work?
Apr 18 '06 #2
I tried to implement the class but for some reason I couldn't access the begin() method and the end() method of the vector class. I had to place the start iterator and the end of the list iterator in order for it to search the whole vector, but I couldn't get it to work.
Apr 18 '06 #3
Hi

There sould not be any problems.
The following code compiles fine.
Post the error msgs that you got.

Expand|Select|Wrap|Line Numbers
  1. #include <vector>
  2. using namespace std;
  3.  
  4. template<class T>
  5. class vector_plus : public vector<T>
  6. {
  7.   public:
  8.     vector_plus();
  9.     void erase_item(const T& item)
  10.     {
  11.     iterator itr= find(begin(), end(), item);
  12.     if(itr != end()) erase(itr);
  13.     }
  14. };
Regards
Prasannaa
May 2 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Freddy Flares | last post by:
Like this. /********************/ class Card { public: Card() : rank(0), suit(0) {} ... private: int rank; int suit;
5
by: Ernst Murnleitner | last post by:
Hello, is it possible to derive from std::vector and derive also its iterator? If I do it like in the example below, I get a problem when I need the begin of the vector: begin() returns the...
4
by: Slavyan | last post by:
(I just started to learn C#.NET) What's the syntax in c# for a class to inherit more than one class. I know following syntax: public class MyClass : MyOtherClass { } but I need to inherit...
0
by: cagenix | last post by:
I was running through a data structures book and I was curious if anyone could inform me of how to inherit the vector class to do a simple search and erase function. The example states: ...
11
by: TG | last post by:
Hi there. I'm trying to create a simple class called Vector which inherit from array. class Vector(array): def __init__(self,length): """initialize a vector of random floats of size length....
21
by: T.A. | last post by:
I understand why it is not safe to inherit from STL containers, but I have found (in SGI STL documentation) that for example bidirectional_iterator class can be used to create your own iterator...
3
by: Greg | last post by:
How do I inherit or templatise from a continer class, without having to reinterate (type) the member functions ?
2
by: =?iso-8859-1?q?Erik_Wikstr=F6m?= | last post by:
I'm working on a project where I need a couple of classes that purely conceptually are related to each other such that I could create a base-class and have a is-a relation between each of the...
4
by: nw | last post by:
Hi All, I currently have a vector of objects (lets call them MyObject). I want to perform various operations regularly on the whole vector, for example find the maximum, average, or operations...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.