C++ 什么是else if的替代品?
很多人告诉我,在我的代码中使用大量(else if)不是好的做法,这是一个糟糕的设计,但我不知道如何改进或更改它,例如,如何改进此代码C++ 什么是else if的替代品?,c++,C++,很多人告诉我,在我的代码中使用大量(else if)不是好的做法,这是一个糟糕的设计,但我不知道如何改进或更改它,例如,如何改进此代码 #include <iostream> #include <cstdio> #include <string> int vowels[5]; int count; int main() { std::string name; std::getline(std::cin, name); for (int
#include <iostream>
#include <cstdio>
#include <string>
int vowels[5];
int count;
int main() {
std::string name;
std::getline(std::cin, name);
for (int i = 0; i < name.size(); i++)
{
if (name[i] == 'a') {
vowels[0]++;
count++;
}
else if (name[i] == 'e') {
vowels[1]++;
count++;
}
else if (name[i] == 'i') {
vowels[2]++;
count++;
}
else if (name[i] == 'o') {
vowels[3]++;
count++;
}
else if (name[i] == 'u') {
vowels[4]++;
count++;
}
}
std::cout << "The number of vowels in this name is : " << count << "\n";
std::cout << "The number of the vowel a : " << vowels[0] << "\n";
std::cout << "The number of the vowel e : " << vowels[1] << "\n";
std::cout << "The number of the vowel i : " << vowels[2] << "\n";
std::cout << "The number of the vowel o : " << vowels[3] << "\n";
std::cout << "The number of the vowel u : " << vowels[4] << "\n";
return 0;
}
#包括
#包括
#包括
int元音[5];
整数计数;
int main(){
std::字符串名;
std::getline(std::cin,name);
对于(int i=0;i std::cout您可以使用开关
语句:
for (int i = 0; i < name.size(); i++)
{
switch (name[i]) {
case 'a':
vowels[0]++;
count++;
break;
case 'e':
vowels[1]++;
count++;
break;
case 'i':
vowels[2]++;
count++;
break;
case 'o':
vowels[3]++;
count++;
break;
case 'u':
vowels[4]++;
count++;
break;
}
}
或带有
和
的std::map
:
#包括
#包括
#包括
#包括
int main(){
标准::映射元音={
{'a',0},
{'e',0},
{'i',0},
{'o',0},
{'u',0}
};
std::string word=“敏捷的棕色狐狸”;
std::for_each(word.cbegin()、word.cend()、[&元音](常量字符c){
自动iter=元音。查找(c);
if(iter!=元音.end()){
iter->second++;
}
});
自动计数=标准::累加(元音.cbegin(),元音.cend(),0,
[](常数内部acc、常数自动和kv){
返回acc+kv.s;
});
std::cout您可以使用开关
语句:
for (int i = 0; i < name.size(); i++)
{
switch (name[i]) {
case 'a':
vowels[0]++;
count++;
break;
case 'e':
vowels[1]++;
count++;
break;
case 'i':
vowels[2]++;
count++;
break;
case 'o':
vowels[3]++;
count++;
break;
case 'u':
vowels[4]++;
count++;
break;
}
}
或带有
和
的std::map
:
#包括
#包括
#包括
#包括
int main(){
标准::映射元音={
{'a',0},
{'e',0},
{'i',0},
{'o',0},
{'u',0}
};
std::string word=“敏捷的棕色狐狸”;
std::for_each(word.cbegin()、word.cend()、[&元音](常量字符c){
自动iter=元音。查找(c);
if(iter!=元音.end()){
iter->second++;
}
});
自动计数=标准::累加(元音.cbegin(),元音.cend(),0,
[](常数内部acc、常数自动和kv){
返回acc+kv.s;
});
std::cout在这个特定示例中,您可以使用开关
,因为char
是一种数字类型:
#include <iostream>
#include <string>
int vowels[5] = {};
int count = 0;
int main() {
std::string name;
std::getline(std::cin, name);
for (char ch : name)
{
switch(ch)
{
case 'a': {
vowels[0]++;
++count;
break;
}
case 'e': {
vowels[1]++;
++count;
break;
}
case 'i': {
vowels[2]++;
++count;
break;
}
case 'o': {
vowels[3]++;
++count;
break;
}
case 'u': {
vowels[4]++;
++count;
break;
}
}
}
std::cout << "The number of vowels in this name is : " << count << "\n";
std::cout << "The number of the vowel a : " << vowels[0] << "\n";
std::cout << "The number of the vowel e : " << vowels[1] << "\n";
std::cout << "The number of the vowel i : " << vowels[2] << "\n";
std::cout << "The number of the vowel o : " << vowels[3] << "\n";
std::cout << "The number of the vowel u : " << vowels[4] << "\n";
return 0;
}
在此特定示例中,您可以使用开关
,因为字符
是一种数字类型:
#include <iostream>
#include <string>
int vowels[5] = {};
int count = 0;
int main() {
std::string name;
std::getline(std::cin, name);
for (char ch : name)
{
switch(ch)
{
case 'a': {
vowels[0]++;
++count;
break;
}
case 'e': {
vowels[1]++;
++count;
break;
}
case 'i': {
vowels[2]++;
++count;
break;
}
case 'o': {
vowels[3]++;
++count;
break;
}
case 'u': {
vowels[4]++;
++count;
break;
}
}
}
std::cout << "The number of vowels in this name is : " << count << "\n";
std::cout << "The number of the vowel a : " << vowels[0] << "\n";
std::cout << "The number of the vowel e : " << vowels[1] << "\n";
std::cout << "The number of the vowel i : " << vowels[2] << "\n";
std::cout << "The number of the vowel o : " << vowels[3] << "\n";
std::cout << "The number of the vowel u : " << vowels[4] << "\n";
return 0;
}
另一种选择是开关
:
for (int i = 0; i < name.size(); i++)
{
++count;
switch (name[i]) {
case 'a':
vowels[0]++;
break;
case 'e':
vowels[1]++;
break;
case 'i':
vowels[2]++;
break;
case 'o':
vowels[3]++;
break;
case 'u':
vowels[4]++;
break;
default:
--count;
break;
}
}
for(int i=0;i
另一种选择-如果您只想计算字符的出现次数,可以使用映射:
std::map<char, int> counts;
for (const char c : name) {
counts[c]++;
}
std::map计数;
for(常量字符c:名称){
计数[c]++;
}
然后,要知道任何字符的计数,只需查找:
std::cout << "The number of 'e's in the string: " << counts['e'] << '\n';
std::cout另一种选择是开关
:
for (int i = 0; i < name.size(); i++)
{
++count;
switch (name[i]) {
case 'a':
vowels[0]++;
break;
case 'e':
vowels[1]++;
break;
case 'i':
vowels[2]++;
break;
case 'o':
vowels[3]++;
break;
case 'u':
vowels[4]++;
break;
default:
--count;
break;
}
}
for(int i=0;i
另一种选择-如果您只想计算字符的出现次数,可以使用映射:
std::map<char, int> counts;
for (const char c : name) {
counts[c]++;
}
std::map计数;
for(常量字符c:名称){
计数[c]++;
}
然后,要知道任何字符的计数,只需查找:
std::cout << "The number of 'e's in the string: " << counts['e'] << '\n';
std::cout用于执行搜索,然后获取索引:
constexpr char vowelNames[] = { 'a', 'e', 'i', 'o', 'u' };
for (const auto c : name) {
const auto it = std::find(
std::begin(vowelNames),
std::end(vowelNames),
c
);
if (it != std::end(vowelNames)) {
++vowels[std::distance(std::begin(vowelNames), it)];
++count;
}
}
用于执行搜索,然后获取索引:
constexpr char vowelNames[] = { 'a', 'e', 'i', 'o', 'u' };
for (const auto c : name) {
const auto it = std::find(
std::begin(vowelNames),
std::end(vowelNames),
c
);
if (it != std::end(vowelNames)) {
++vowels[std::distance(std::begin(vowelNames), it)];
++count;
}
}
您的具体案例的另一种可能性是:
#为您的具体案例包括另一种可能性,使用:
#include在许多情况下,它是if-else if
的替代品。@JeJo它一直是一个全局变量。在许多情况下,它也是if-else if
的替代品。@JeJo它一直是一个全局变量。std::map
对于这一点来说是一个过分的做法。@Acorn这就是为什么它不是第一个建议:)我希望map版本的性能比交换机差一个数量级。出于这个原因,我甚至不会提供它作为解决方案。@rustyx虽然我同意你的观点,即给定一个可以通过这样一个简单的交换机解决的实际问题,std::map
方法由于性能差异是不可取的。然而,我相信OP是一个初学者,只要简单介绍一下std::map
,我就会从中受益。我可能不是最好的人(也不是使用最好的工具)引入这样的概念,但尽管如此-这是一种解决问题的方法,使用std::map
可以很好地解决这些差异。对于这一点,std::map
是一种相当过分的做法。@Acorn这就是为什么它不是第一个建议:)我预计map版本的性能比交换机差一个数量级。出于这个原因,我甚至不会提供它作为解决方案。@rustyx虽然我同意您的观点,即通过这样一个简单的开关
可以解决现实世界中的问题,但由于性能差异,std::map
方法将是不可取的。不过,我相信OP是一个初学者,可以从简单的介绍中获益