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
.