更新时间:2021-12-05 08:55:04
我基本上只需要它来警告用户是否已经在数组中输入了
a编号,并提供了另一个cin>> pBallNums
语句。
在这种情况下,只需使用 std :: set
,并使用它的 std :: set :: emplace
方法将用户输入存储到集合中。
In that case simply use std::set
and use it's std::set::emplace
method to store the user input into the set.
来自 cppreference .com ,
template< class... Args >
std::pair<iterator,bool> emplace( Args&&... args );
std :: set: :emplace
std::set::emplace
将一对由迭代器组成的对返回到插入的元素,或者将已经存在的元素
,并且出现布尔值表示是否发生了插入。插入时为True
,插入时为 False 。
Returns a pair consisting of an iterator to the inserted element, or the already-existing element if no insertion happened, and a bool denoting whether the insertion took place. True for Insertion, False for No Insertion.
仅针对您的情况使用此信息并再次循环以供下一次用户输入。
Simply take this information for your case and loop again for the next user input.
此处是示例代码(查看直播):
Here is a sample code (See Live):
#include <iostream>
#include <set>
int main()
{
std::set<int> mySet;
for(int loopCount{ 0 }; loopCount != 5; )// repeat until maximum loop count
{
int input; std::cin >> input; // user input
// get the std::pair<iterator,bool>
const auto pairIter = mySet.emplace(input);
// increment maximum loop count, if insertion successful
if (pairIter.second) ++loopCount;
}
for (const int userInput : mySet)
std::cout << userInput << " ";
return 0;
}
样本输入:
1
1
2
3
4
2
4
3
5
输出:
1 2 3 4 5