我想用开关评估一个字符串但是当我读取用户输入的字符串时会抛出以下错误.
#include<iostream>
using namespace std;
int main() {
string a;
cin>>a;
switch (string(a)) {
case "Option 1":
cout<<"It pressed number 1"<<endl;
break;
case "Option 2":
cout<<"It pressed number 2"<<endl;
break;
case "Option 3":
cout<<"It pressed number 3"<<endl;
break;
default:
cout<<"She put no choice"<<endl;
break;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误:从类型'std :: string {aka std :: basic_string}'无效转换为类型'int
Ser*_*hiy 78
如前所述,switch只能用于整数值.因此,您只需要将"case"值转换为整数.您可以使用c ++ 11中的constexpr来实现它,因此可以在编译时计算constexpr函数的一些调用.
类似的东西......
switch (str2int(s))
{
case str2int("Value1"):
break;
case str2int("Value2"):
break;
}
Run Code Online (Sandbox Code Playgroud)
str2int就像(从这里实现):
constexpr unsigned int str2int(const char* str, int h = 0)
{
return !str[h] ? 5381 : (str2int(str, h+1) * 33) ^ str[h];
}
Run Code Online (Sandbox Code Playgroud)
另一个例子,下一个函数可以在编译时计算:
constexpr int factorial(int n)
{
return n <= 1 ? 1 : (n * factorial(n-1));
}
int f5{factorial(5)};
// Compiler will run factorial(5)
// and f5 will be initialized by this value.
// so programm instead of wasting time for running function,
// just will put the precalculated constant to f5
Run Code Online (Sandbox Code Playgroud)
Ker*_* SB 17
甲switch语句只能被用于整数值,而不是为用户定义的类型的值.即使可能,您的输入操作也不起作用.
你可能想要这个:
#include <string>
#include <iostream>
std::string input;
if (!std::getline(std::cin, input)) { /* error, abort! */ }
if (input == "Option 1")
{
// ...
}
else if (input == "Option 2")
{
// ...
}
// etc.
Run Code Online (Sandbox Code Playgroud)
msk*_*her 17
您可以将字符串映射到枚举值,然后打开枚举:
enum Options {
Option_Invalid,
Option1,
Option2,
//others...
};
Options resolveOption(string input);
// ...later...
switch( resolveOption(input) )
{
case Option1: {
//...
break;
}
case Option2: {
//...
break;
}
// handles Option_Invalid and any other missing/unmapped cases
default: {
//...
break;
}
}
Run Code Online (Sandbox Code Playgroud)
解析枚举可以实现为一系列if检查:
Options resolveOption(std::string input) {
if( input == "option1" ) return Option1;
if( input == "option2" ) return Option2;
//...
return Option_Invalid;
}
Run Code Online (Sandbox Code Playgroud)
或者地图查找:
Options resolveOption(std::string input) {
static const std::map<std::string, Option> optionStrings {
{ "option1", Option1 },
{ "option2", Option2 },
//...
};
auto itr = optionStrings.find(input);
if( itr != optionStrings.end() ) {
return *itr;
}
return Option_Invalid;
}
Run Code Online (Sandbox Code Playgroud)
rmn*_*rmn 11
您只能对可转换为int的类型使用switch-case.
但是,您可以定义std::map<std::string, std::function> dispatcher并使用它dispatcher[str]()来达到相同的效果.
| 归档时间: |
|
| 查看次数: |
209721 次 |
| 最近记录: |