C++ Switch 语句输入
我正在编写一个 C++ 程序,提示用户输入,然后跟踪输入的次数。我目前正在使用 do-while 循环和 switch 语句。我遇到问题的部分是 switch 语句。我不知道如何跟踪输入的次数。例如:
Enter Value: 4
Enter Value: 4
Enter Value: 4
Enter Value: 3
Enter Value: 3
// 然后我希望程序能够知道并最终输出数字“4”和“3”被输入了多少次。 我想可能会使用某种增量计数形式,但不是 100% 确定。 谢谢!
I am writing a C++ program that prompts the user for an input and then keeps track of how many times that input is entered. I am currently using a do-while loop and a switch statement. The part I am having trouble with is the switch statement. I can't figure out how to keep track of how many times an input is entered. For example:
Enter Value: 4
Enter Value: 4
Enter Value: 4
Enter Value: 3
Enter Value: 3
// I then want the program to be able to know and then eventually output, how many times the number '4' and '3' were entered.
I thinking possibly using some sort of increment counting form, but not 100% sure.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能需要使用
std::map
。原因如下。让我们看看替代方案,从显而易见的开始:
这符合您的要求:评估输入,并跟踪特定输入被看到的次数。这种形式确实存在许多问题:
我们可以通过指定一个数组来减少变量数量:
这仍然受到太多几乎但不完全相同的代码行的影响。让我们尝试摆脱 switch 语句:
啊,现在我们已经取得进展了!通过消除
switch
语句,我们有了一行易于维护的代码。但是如果用户(意外或恶意)输入 6 该怎么办?然后我们将增加count[6]
,它不存在。这是一件坏事。我们可以增加数组的大小:现在我们对用户来说是安全的。如果他输入 6,那么坏事就不会再发生。呃哦,如果用户输入 51000 呢?我们将增加不存在的
count[51000]
。很明显,我们无法赢得这场游戏——对于我们选择的任何数字,用户可能会选择该数字加 1。即使我们能赢,我们仍然会输。如果我们只要求用户输入几个数字,那么我们就会浪费数组中的其他 49,997 个条目。
幸运的是,C++ 有一个我们可以使用的数据结构:
该数据结构称为映射:
映射有点像数组,但以特殊方式自行增长。只有我们使用的条目才会被分配,并且我们使用的每个条目都会自动分配。
You'll probably want to use a
std::map<int,int>
. Here's why.Let's look at alternatives, starting with the obvious:
This does what you ask: you evaluate the input, and keep track of the number of times that specific input has been seen. This form does suffer from many problems:
We can reduce the variable count by specifing an array:
This still suffers from too many almost-but-not-quite identical lines of code. Let's try to get rid of the switch statement:
Ah, now we are getting somewhere! By eliminating the
switch
statement, we have one easily-maintained line of code. But what if the user (accidentally or maliciously) enters a 6? Then we will incrementcount[6]
, which does not exist. This is a Bad Thing. We could increase the size of the array:Now we are safe from the user. If he enters a 6, the Bad Thing no longer happens. Uh-oh, what about if the user enters 51000? We will increment
count[51000]
which does not exist. It should be obvious that we can't win this game -- for any number we choose, the user might choose that number plus 1.Even if we could win, we'd still lose. If we are only asking the user to enter a few numbers, then we will have wasted the other 49,997 entries in the arary.
Fortunately C++ has a data structure that we can use which:
That data structure is called a map:
A map is sort of like an array, but grows itself in a special way. Only the entries that we use are ever allocated, and every entry that we use is automatically allocated.
如果输入值的范围有限,则可以使用数组。数组的每个元素代表一个输入值。在开始时将元素初始化为 0,并在输入相应的输入值时递增适当的元素。
If your range of input values is limited, you can use an array. Each element of the array represents an input value. Initialize the elements to 0 at the beginning and increment the appropriate element when its corresponding input value is entered.