LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-03-2010, 10:47 AM   #1
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Rep: Reputation: 16
set implementation


Hello i need to implement a Set class(like in stl) using a vector.Here is my code that doesnt work corectly:

Code:
#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();
  }
  T& operator [] (int index)
  {
    return set[index];
  }
};

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(int i = 0; i < mainSet.size(); i++)
  {
    std::cout << mainSet[i] << " ";
  }
  std::cout << std::endl;

}
If my input is: 70, 20, 50 then my output would look like this:

Code:
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
The end() function is not working coreclty and i dont know why it gives me so much numbers in output,some help would be apreciated.Thank you.
 
Old 09-03-2010, 10:49 AM   #2
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Original Poster
Rep: Reputation: 16
i forgot to say that the output is when i use:
Code:
for(it = mainSet.begin(); it != mainSet.end(); it++)
{
   cout << *it;
}
 
Old 09-04-2010, 03:57 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You call mainSet.end() which returns an iterator object (which is positioned to the end of the vector) and compare it with another iterator object. The two objects are different and so the end clause of your loop is not satisfied.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Different Implementation of VPN depam Linux - Networking 0 12-22-2006 06:54 PM
errno implementation vineeth_thampi Linux - Newbie 2 12-06-2006 05:39 AM
DNS Implementation janskey Linux - General 1 09-14-2006 11:07 AM
linux shell implementation for a set of commands syeda tabassum Linux - Software 0 11-03-2004 11:53 AM
implementation of linux shell for a set of commands satishkumar LinuxQuestions.org Member Intro 1 10-09-2004 04:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration