Search code examples
c++sortingabstract-data-type

A function where duplicates are not allowed? (C++)


So I'm suppose modify a function whose initial purpose is so that a string cannot have more than 6 item in its array. Heres the code

template<class ItemType>
bool Bag<ItemType>::Add(const ItemType& new_entry)
{
  bool has_room_to_add = item_count_ < max_items_;
  if (has_room_to_add)
   {
      items_[item_count_] = new_entry;
      item_count_++;
   }  // end if
  return has_room_to_add;
}  // end add

This is my attempt at it.

template<class ItemType>
bool set<ItemType>::Add(const ItemType& new_entry)
{
  string checker[] = { "Joker", "Ace", "Two", "Three",
      "Four", "Five", "Six", "Seven",
      "Eight", "Nine", "Ten", "Jack",
      "Queen", "King" };
  bool has_room_to_add = item_count_ < max_items_;

  //compares the new entry to every item in the string and if there is a duplicate, the loop breaks and nothing is added.
   if (has_room_to_add)
   {
         for ( int i =0; i <=13; i++)
        {
            if (checker[i] == items_[item_count_])
                break;  //ends loop

            else if (i==13)
            {
                items_[item_count_] = new_entry;
                break;  //ends loop
            }  // end if
        } // end for
   } //end if

// increases item_count_ if a new item is added to a set.
  if (items_[item_count_] == new_entry)
      item_count_++;

  return has_room_to_add;
}  // end add

But not only does this not prevent duplicates, it breaks the original purpose of not allowing more than 6 items and goes haywire if there is more. Can anyone tell me what I did wrong?


Solution

  • The C++ way to do this is to use a std::set, since a std::set doesn't store duplicates.

    #include <set>
    #include <string>
    #include <iostream>
    #include <iterator>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
       string checker[] = { "Joker", "Ace", "Two", "Three",
          "Four", "Five", "Six", "Seven",
          "Eight", "Nine", "Ten", "Jack",
          "Queen", "King",  "Joker", "Ace", "Two", "Three",
          "Four", "Five", "Six", "Seven",
          "Eight", "Nine", "Ten", "Jack",
          "Queen", "King" };
    
       set<string> mySet;
    
       // insert all of the items in the array into the set  
       copy(checker, checker + sizeof(checker)/sizeof(checker[0]), std::inserter(mySet, mySet.begin()));
       // output the results
       copy(mySet.begin(), mySet.end(), std::ostream_iterator<string>(cout, "\n"));
    }
    

    Output:

    Ace
    Eight
    Five
    Four
    Jack
    Joker
    King
    Nine
    Queen
    Seven
    Six
    

    Note that even though duplicate entries are attempted to be placed in the set, only one entry exists. To limit the number of items to 6:

    #include <set>
    #include <string>
    #include <iostream>
    #include <iterator>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
       string checker[] = { "Joker", "Ace", "Two", "Three",
          "Four", "Five", "Six", "Seven",
          "Eight", "Nine", "Ten", "Jack",
          "Queen", "King",  "Joker", "Ace", "Two", "Three",
          "Four", "Five", "Six", "Seven",
          "Eight", "Nine", "Ten", "Jack",
          "Queen", "King" };
    
       set<string> mySet;
    
       // insert all of the items in the array into the set  
       for (size_t i = 0; i < sizeof(checker)/sizeof(checker[0]); ++i)
       {
          if ( mySet.size() < 6 )
             mySet.insert(checker[i]);
          else
             break;
       }
    
       // output the results
       copy(mySet.begin(), mySet.end(), std::ostream_iterator<string>(cout, "\n"));
    }
    

    Output:

    Ace
    Five
    Four
    Joker
    Three
    Two