سؤال

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;
}
هل كانت مفيدة؟

المحلول

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
}

نصائح أخرى

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top