C++读取一个数学函数并进行排序
C++ reading a mathematical function and sorting
我正在从一个格式为f(x,y,f(x),g)的文件中读取一个函数,一旦我读取了输入,它就被存储为一个向量,我试图获取逗号之间的每个值,所以在这种情况下,我想将x,y f(x)和g作为单独的字符/字符串。我被卡住了,有什么想法吗?
这是我提出的解决方案:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
//Split string into vector of strings
vector<string> split(string str, char delimiter)
{
vector<string> internal;
stringstream ss(str); // Turn the string into a stream.
string tok;
while(getline(ss, tok, delimiter))
{
internal.push_back(tok);
}
return internal;
}
int main()
{
string myInput = "f(x,y,f(x),g)";
//Extract the string between outer brackets
size_t startIndex = myInput.find_first_of("(") + 1;
size_t endIndex = myInput.find_last_of(")");
string innerStr = myInput.substr(startIndex, endIndex-startIndex);
//Split the result by comma
vector<string> sep = split(innerStr, ',');
for(unsigned int i = 0; i < sep.size(); ++i)
{
cout << sep[i] << endl;
}
}
希望它能帮助
相关文章:
- 是否有类似std::lower_bound的函数,而不需要排序/分区输入
- std::sort()函数无法对向量的一部分进行排序
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 用于合并排序的合并函数
- 如何计算此排序函数的时间复杂度?
- 什么是自定义比较器以及如何在 C++ 的排序函数中使用它?
- 为什么用户定义的函数不按照给定的顺序对相同长度的元素进行排序?
- 自定义排序函数中的堆溢出
- 分段 排序函数实现中的错误
- 使用模板化分配器和对向量进行排序的函数
- 为什么我的函数接受"std::string"进行排序不会改变它?
- 对没有比较器或λ函数的向量进行排序?
- C++中的析构函数和构造函数排序
- 数组中的函数排序错误
- 如何命名比较谓词函数排序向量
- 按lexicographical_compare()函数排序
- 构造函数排序(全局作用域)问题
- 按接受c++中参数的函数排序
- 函数排序错误
- 根据不同函数排序的std::向量的交集