1

Let's say that I will have an algebraic function or expression that is in the form of a std::string. Either a function or a class constructor would accept this string as a parameter.

void function( const std::string& function ) { /* ... */ }

class Foo {
    explicit Foo( const std::string& function ) { /* ... */ }
};

Then let's say I have a function that will parse this string and then convert it into a std::function where my current function signature looks like this:

template<typename Ret, typename... Args>
void parseStringFunc( const std::string& funcStr, std::function<Ret(Args...)>& func ) {
     /*....*/
 }

Now comes the question in mind as it has to do with parsing the string. I have two choices to work from and I wanted to know which one would be the preferred choice as a lookup table.

  • I could have a std::vector<char> operators { '+', '-', '*', '/', '=' }
    • The same above but for numbers or digits [0-9]
    • and for letters [a-z] and [A-Z] where this would be tedious.
  • Or I could make it simpler and have just a std::string:
    • std::string operators { "+-*/=" };
    • std::string numbers{ "0123456789" };
    • std::string letters{ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" };

I'm wondering which would be the preferred choice above to use for a look up table to compare the individual characters within the funcStr that will be parsed to be converted into a std::function.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
  • 1
    This seems to focus on the trivial. I'd expect you to be interested in how do the parsing itself. See my SO answer on how to write a parser. With that in hand, you can decide which of these makes sense to you. See https://stackoverflow.com/a/2336769/120163 – Ira Baxter Mar 07 '19 at 18:18

0 Answers0