Data structures or coding styles in C++ for avoiding long elseif chain when parsing?


Lately I have created some small parsers of data. My initial code structure

// more cases here ...
else if(!strcmp(X,"somekey")){
  // Parse according to "somekey" behavior.
}
// more cases here ...

worked fine for small number of parse cases, but the obvious drawback is that this soon grows into a huge if-elseif-else chain which is very ugly and non-modular and can be pain in the ass to debug if small errors sneak in somewhere.

A more modular C way of handling this could be done with function pointers.

for(int i = 0; i < NR_of_parse_cases; i++){
   if(!strcmp(X,lKey(i))){
     parseAs(i)(X,data_to_parse);
     continue;
   }
}

Now we have made it more modular. We can populate two arrays with c-strings and corresponding pointers to functions without having to copy-paste or comment out huge chunks of code with all errors that could bring. But there are still several non-optimal things here. If inside for loop is ugly and also slow. I suspect there must be some more moder C++:y way to do this, maybe "maps" or something similar to python "dict" ?