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 개 이상의 항목을 허용하지의 원래 목적을 나누기보다이있는 경우 엉망이 간다. 아무도 내가 잘못한 것을 말해 줄 수 있습니까?
는'표준 : set'를 사용하고 또한에 좋은 생각이 아니다 6 개 항목의 수를 제한 클래스의 이름을'set'로 지정하십시오. – PaulMcKenzie
나머지 수업을 보여줄 수 있습니까? –