Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从字符串解析代数函数或表达式_C++_String_Parsing_C++17 - Fatal编程技术网

C++ 从字符串解析代数函数或表达式

C++ 从字符串解析代数函数或表达式,c++,string,parsing,c++17,C++,String,Parsing,C++17,假设我有一个代数函数或表达式,它的形式是std::string。函数或类构造函数都会接受此字符串作为参数 void function( const std::string& function ) { /* ... */ } class Foo { explicit Foo( const std::string& function ) { /* ... */ } }; 然后假设我有一个函数,它将解析这个字符串,然后将它转换成std::function,其中我当前的函数签

假设我有一个代数函数或表达式,它的形式是
std::string
。函数或类构造函数都会接受此字符串作为参数

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

class Foo {
    explicit Foo( const std::string& function ) { /* ... */ }
};
然后假设我有一个函数,它将解析这个字符串,然后将它转换成
std::function
,其中我当前的函数签名如下所示:

template<typename Ret, typename... Args>
void parseStringFunc( const std::string& funcStr, std::function<Ret(Args...)>& func ) {
     /*....*/
 }
模板
void parseStringFunc(常量std::string&funcStr,std::function&func){
/*....*/
}
现在想到了这个问题,因为它与解析字符串有关。我有两个选择,我想知道哪一个是作为查找表的首选

  • 我可以有一个
    std::vector操作符{'+','-','*','/','='}
    • 以上相同,但适用于数字或数字[0-9]
    • 对于字母[a-z]和[a-z]来说,这会很乏味
  • 或者我可以简化它,只需要一个
    std::string
    • std::字符串运算符{“+-*/=”}
    • std::字符串编号{“0123456789”}
    • std::字符串字母{“abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz”}

我想知道上面哪个是比较
funcStr
中单个字符的查找表的首选选项,这些字符将被解析为
std::function

,这似乎集中在琐碎的问题上。我希望您对解析本身的方式感兴趣。请参阅我关于如何编写解析器的答案。有了这些,你就可以决定哪些对你有意义。看这似乎集中在琐事上。我希望您对解析本身的方式感兴趣。请参阅我关于如何编写解析器的答案。有了这些,你就可以决定哪些对你有意义。看见