gpt4 book ai didi

C++ 读取数学函数和排序

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:25:28 24 4
gpt4 key购买 nike

我正在从格式为 f(x,y,f(x),g) 的文件中读取一个函数,一旦我读取了输入,它就被存储为一个 vector ,我试图获取逗号之间的每个值所以在这种情况下,我想将 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;
}

}

希望对你有帮助

关于C++ 读取数学函数和排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26519945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com