나는리스트에서 3 명의 플레이어를 선택하도록하는 매우 간단한 프로그램을 가지고있다. 사용자가 동일한 플레이어를 두 번 선택하지 못하게 할 방법이 있습니까? 원래는 사용자가 선택하는 항목이 std :: set에 추가 될 것이고 그 다음에 해당 항목을 세트와 비교하여 검사 할 것입니다. 그렇다면 그 선택은 항상 세트에 추가되기 때문에 그 아이디어가 얼마나 우스운 지 깨달았습니다.사용자가 동일한 선택을 두 번하고 있는지 확인하는 방법은 무엇입니까?
#include <iostream>
#include <string>
int main()
{
int m_NumOfPlayers;
std::string m_PlayerSelection [6] =
{
"Miss Scarlet",
"Mrs. Peacock",
"Colonel Mustard",
"Professor Plum",
"Mrs. White",
"Mr. Green"
};
std::string m_Number [6] =
{
"first",
"second",
"third",
"fourth",
"fifth",
"sixth"
};
for (int i = 0; i < 3; i++)
{
std::cout << "\nPlease choose the " << m_Number[i] << " player" << ":" << std::endl << std::endl;
for (int j = 0; j < 6; j++)
{
std::cout << (j + 1) << ". " << m_PlayerSelection[j] << std::endl;
}
int selection;
std::cin >> selection;
while (selection < 1 || selection > 6)
{
std::cout << "Please choose a number between 1 and 6: ";
std::cin >> selection;
}
}
system("pause");
return 0;
}
귀하의 제안 작동? 세트가 더 적절한 구조 일 것이다. 하나는 빠른 포함 기능입니다. 어쩌면 해시 테이블. 보다 사용자 친화적 인 접근 방법은 선택할 수없는 항목을 숨기거나 제거하는 것입니다. – keyser