[英]C++ Switch Statement inputs
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. 我正在编写一个C ++程序,提示用户输入,然后跟踪输入的输入次数。 I am currently using a do-while loop and a switch statement.
我目前正在使用do-while循环和switch语句。 The part I am having trouble with is the switch statement.
我遇到问题的部分是switch语句。 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. //然后我希望程序能够知道并最终输出数字“4”和“3”的输入次数。 I thinking possibly using some sort of increment counting form, but not 100% sure.
我想可能使用某种递增计数形式,但不是100%肯定。 Thanks!
谢谢!
You'll probably want to use a std::map<int,int>
. 您可能想要使用
std::map<int,int>
。 Here's why. 这就是原因。
Let's look at alternatives, starting with the obvious: 让我们看看替代方案,从明显的开始:
int count0;
int count1;
int count2;
int count3;
int count4;
...
switch(input) {
case 0: ++count0; break;
case 1: ++count1; break;
case 2: ++count2; break;
case 3: ++count3; break
case 4: ++count4; break;
}
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: 我们可以通过指定一个数组来减少变量数:
int count[5];
...
switch(input) {
case 0: ++count[0]; break;
case 1: ++count[1]; break;
case 2: ++count[2]; break;
case 3: ++count[3]; break;
case 4: ++count[4]; break;
}
This still suffers from too many almost-but-not-quite identical lines of code. 这仍然会受到太多几乎但不完全相同的代码行的影响。 Let's try to get rid of the switch statement:
让我们试着摆脱switch语句:
int count[5];
...
++count[input];
Ah, now we are getting somewhere! 啊,现在我们到了某个地方! By eliminating the
switch
statement, we have one easily-maintained line of code. 通过消除
switch
语句,我们有一个易于维护的代码行。 But what if the user (accidentally or maliciously) enters a 6? 但是如果用户(意外或恶意)进入6? Then we will increment
count[6]
, which does not exist. 然后我们将增加
count[6]
,这是不存在的。 This is a Bad Thing. 这是一件坏事。 We could increase the size of the array:
我们可以增加数组的大小:
int count[50000];
...
++count[input];
Now we are safe from the user. 现在我们对用户是安全的。 If he enters a 6, the Bad Thing no longer happens.
如果他进入6,则不再发生坏事。 Uh-oh, what about if the user enters 51000?
呃哦,如果用户输入51000呢? We will increment
count[51000]
which does not exist. 我们将增加不存在的
count[51000]
。 It should be obvious that we can't win this game -- for any number we choose, the user might choose that number plus 1. 很明显,我们无法赢得这场比赛 - 对于我们选择的任何数字,用户可能会选择该数字加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.
如果我们只是要求用户输入一些数字,那么我们将浪费其他49,997个条目。
Fortunately C++ has a data structure that we can use which: 幸运的是,C ++有一个我们可以使用的数据结构:
That data structure is called a map: 该数据结构称为地图:
std::map<int,int> count;
...
++count[input];
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.
只分配我们使用的条目,并自动分配我们使用的每个条目。
std::map<int, int> frequency;
int value_entered_by_user = f();
frequency[value_entered_by_user]++;
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.
在开始时将元素初始化为0,并在输入相应的输入值时递增相应的元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.