0

I'm doing an exercise and encounter a problem.
To solve this exercise, I have to write a template class MySet as a wrapper of STL::set. In the following, there is code like
pair<MySet<int>::iterator, MySet<int>::iterator> p where pair comes from the STL.
Now what should I do to support MySet<int>::iterator?
I've tried typedef set<T>::iterator MySet<T>::iterator
and typedef T* iterator but they all failed.

===============================EDIT================================

#include <iostream>
#include <set>

using namespace std;

template<class T, class Q = greater<T> >
class MySet
{
public:
    using iterator = typename set<T, Q>::iterator;
    typedef typename set<T, Q>::iterator iterator;
    set<T, Q> tset;
    void insert(T value)
    {
        tset.insert(value);
    }
};

int main()
{
    MySet<int> intset;
    intset.insert(5);
    intset.insert(10);
    MySet<int>::iterator p;
}
dastan
  • 1,006
  • 1
  • 16
  • 36

2 Answers2

0

You can inherit from std::set like this:

    template <typename T>
    class MySet: public std::set<T>
   {

   };

   int main()
   {
     MySet<int>::iterator itr;
     // do other things

     return 0;
   }

Although inheriting STL containers is not a good practice. See this link

Community
  • 1
  • 1
Rakib
  • 7,435
  • 7
  • 29
  • 45
0

something like this

template<class T>
class MySet
{
public:
    //using iterator = typename set<T>::iterator; // if with c++11
    typedef typename set<T>::iterator iterator; // if without c++11
    // rest of your code
}
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
  • Trying `typedef set::iterator iterator` failed. error: need 'typename' before 'std::set::iterator' because 'std::set' is a dependent scope| `using iterator = set::iterator` failed either. (my compiler doesn't support c++11) – dastan Apr 29 '14 at 03:05
  • I followed your advice and wrote a test, but still not work. See my new EDIT. – dastan Apr 29 '14 at 03:44
  • @user3505816 [this](http://coliru.stacked-crooked.com/a/1c11dd8aa8f707f2) compiles – Bryan Chen Apr 29 '14 at 04:03
  • Yes, indeed. It seems the website using compiler which support c++11 while my dosen't. Is there any other way to solve my problem? Thanks. – dastan Apr 29 '14 at 05:25
  • 1
    @user3505816 I mean comment out `using iterator = typename set::iterator;` in your code – Bryan Chen Apr 29 '14 at 05:33
  • Wow it works! I'm new to this syntax, would you please explain it a little? – dastan Apr 29 '14 at 05:49
  • @user3505816 [typedef](http://en.cppreference.com/w/cpp/language/typedef) and [using](http://en.cppreference.com/w/cpp/language/type_alias) – Bryan Chen Apr 29 '14 at 05:52