How can i rewrite set::inser() function,using a vector::insert() :

class Set {
      std::vector<T> set;
public:
     void insert(const T& t)
     {
        set.insert(set.end(), t);
     }

I tried to insert them at the end,but it wont work corectly.

Recommended Answers

All 5 Replies

"It won't work correctly." doesn't give us much to go on. You'll have to elaborate.

In the meantime, look up the vector::push_back() member function.

If i insert lets say 70,20 and 50,the output will be:

Size of set is: 3
Begin: 70
End: 25
0 20 50 25 70 20 50 0 0 135113 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

That looks more like a problem with a different part of your code. I think you should probably post some more code.

Notice that 70, 20, and 50 do appear in the output, and in that order. This tells me that they are inserting properly. But to me, their positions and all the extraneous output would indicate an issue either with the iterators that you're using or your output loop/statements themselves.

#include <iostream>
#include <vector>

template<class T>
class Set
{
  std::vector<T> set;
  typename std::vector<T>::iterator iter_m;
public:
  class iterator;
  friend class iterator;
  class iterator
  {
    Set s;
    typename std::vector<T>::iterator it;
  public:
    iterator(const Set& ss):s(ss)
    {
      it = s.set.begin();
    }
    // end sentinel
    iterator(const Set& ss, bool):s(ss)
    {
      it = s.set.end();
    }
    iterator(const iterator& iter)
    {
      s = iter.s;
      it = iter.it;
    }

    iterator()
    {
      //std::cout << "iterator()" << std::endl;
    }
    iterator& operator =(const iterator& iter)
    {
      if (&iter == this)
      {
        return *this;
      }
      s = iter.s;
      it = iter.it;
      return *this;
    }
    iterator& operator++()
    {
      it++;
      return *this;
    }
    iterator& operator++(int)
    {

      return operator++();
    }
    iterator& operator--()
    {
      it--;
      return *this;
    }
    iterator& operator--(int)
    {

      return operator--();
    }
    bool operator != (const iterator& cIter)
    {
      return this != &cIter;
    }
    T& operator*()
    {
      return *it;
    }
    T* operator->()
    {
      return it;
    }
  };

  Set()
  {
    set.clear();
    iter_m = set.begin();
  }
  void insert(const T& t)
  {
    iter_m = set.insert(iter_m, t);
     std::cout << "dummy" << std::endl;
  }
  void insert(iterator it1, iterator it2)
  {
    set.insert(0, it1, it2);
  }
  void clear()
  {
    set.clear();
  }
  iterator begin()
  {
    return iterator(*this);
  }
  iterator end()
  {
    // call the end sentinel, meaning we create an iterator to the end of the vector
    return iterator(*this, true);
  }
  int size()
  {
    return set.size();
  }
};

int main()
{
  Set<int> mainSet;
  mainSet.insert(70);
  mainSet.insert(20);
  mainSet.insert(50);
  Set<int>::iterator it;

  std::cout << "Size of set is: " << mainSet.size() << std::endl;
  std::cout << "Begin: " << *(it = mainSet.begin()) << std::endl;
  std::cout << "End: " << *(it = mainSet.end()) << std::endl;

  for(it = mainSet.begin(); it != mainSet.end(); ++it)
  {
    std::cout << *it << " ";
  }
  std::cout << std::endl;

}

Why are you writing your own set class?
Use std::set, or if you MUST write your own, at least read the STL code for std::set.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.