2014-09-25 2 views
0

그래서 문자열이 배열에 6 개 이상의 항목을 가질 수 없도록하는 초기 목적의 함수를 수정한다고 가정합니다. 코드를 보겠습니다.중복이 허용되지 않는 기능입니까? (C++)

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 

이것은 내 시도입니다.

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 

는하지만이 중복을 방지하지 않습니다하지, 그것은 6 개 이상의 항목을 허용하지의 원래 목적을 나누기보다이있는 경우 엉망이 간다. 아무도 내가 잘못한 것을 말해 줄 수 있습니까?

+1

는'표준 : set'를 사용하고 또한에 좋은 생각이 아니다 6 개 항목의 수를 제한 클래스의 이름을'set'로 지정하십시오. – PaulMcKenzie

+0

나머지 수업을 보여줄 수 있습니까? –

답변

1

std::set에 중복을 저장하지 않으므로 C++ 방법은 std::set을 사용하는 것입니다.

#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")); 
} 

출력 : 중복 된 항목이 세트에 배치 할 시도에도 불구하고

Ace 
Eight 
Five 
Four 
Jack 
Joker 
King 
Nine 
Queen 
Seven 
Six 

참고 한 항목 만이 존재한다. 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")); 
} 

출력 :

Ace 
Five 
Four 
Joker 
Three 
Two 
관련 문제