By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,655 Members | 1,875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,655 IT Pros & Developers. It's quick & easy.

Inherit vector class

P: 3
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
Share this Question
Share on Google+
3 Replies


Banfa
Expert Mod 5K+
P: 8,916
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

P: 3
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

P: 21
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

Post your reply

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