I saw the following code suggested for creating a map lookup in another thread on stack overflow (I've modified it for my purposes but the general structure remains the same).
#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
}}}
I need it or another method to look through a large number of different cases multiple times quickly so i want to avoid If-statements (since i understand they're quite slow). However when i run it i receive the following error:
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>
what would be a possible way to resolve this, or alternatively, is there another way to do it?
Aucun commentaire:
Enregistrer un commentaire