问题描述
我看到以下代码建议用于在堆栈溢出时在另一个线程中创建地图查找(我已出于我的目的对其进行了修改,但一般结构保持不变)。
#include <string>
#include <map>
#include <algorithm>
using namespace std;
namespace kwd { //define namespace
enum Options{ //enumerate alternatives
Option_Invalid,assert
//repeat for more options
};
Options resolveOptions(string input){ //find enum values
static const map < string,Options> optionStrings { //inputmap
{ "assert",assert }
//repeat for more options
};
//finally closing with
auto itr = optionStrings.find(input);
if( itr != optionStrings.end() ) {
return *itr;
}
return Option_Invalid;
}
}
string commentMake(string commentarray[]) {
int commentarraylen =sizeof(commentarray)/sizeof(commentarray[0]);
string comment = "";
for(int i =0; i < commentarraylen; i++){
switch (kwd::resolveOptions(commentarray[i])){
case kwd::Option_Invalid:
comment += commentarray[i] + " ";
break;
//repeat for more options
}}}
我需要它或其他方法来快速查看大量不同的案例,所以我想避免使用 If 语句(因为我知道它们很慢)。但是,当我运行它时,我收到以下错误:
spoofcode.cpp: In function ‘kwd::Options kwd::resolveOptions(std::string)’:
spoofcode.cpp:28:16: error: cannot convert ‘const std::pair<const std::__cxx11::basic_string<char>,kwd::Options>’ to ‘kwd::Options’ in return
28 | return *itr;
| ^~~~
| |
| const std::pair<const std::__cxx11::basic_string<char>,kwd::Options>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)