【发布时间】:2017-05-09 00:33:06
【问题描述】:
我正在尝试接收这样的一串点 (2,4),(5,8),(12,7),(15.54,3.65) 并将其分成 x 数组和 y 数组请帮助仍然初学者我试过这个,这是灾难性的
string polygons;
int i = 0;
int length = polygons.length();
string x[10000];
int index = 0;
int k = 0;
getline(cin, polygons);
for (i = 0; i < length; i++)
{
if (polygons[i] == '(')
{
k = polygons.substr(i + 1, 20).find_first_of(",");
x[index] = polygons.substr(i + 1, ((k + i) - (i + 1)));
index++;
}
}
int a = 0;
string y[10000];
int index2 = 0;
int c = 0;
for (a = 0; a < length; a++)
{
if (polygons.substr(a) == ",")
{
c = polygons.substr(a + 1, 20).find_first_of(")");
}
y[index2] = polygons.substr(a + 1, ((c + a) - (a + 1)));
index2++;
}
【问题讨论】:
-
建议整理括号并提供minimal reproducible example
-
建议:分解工作。 Use
std::string::find帮助找到 '(' 和 ')' 然后打印出它们之间的东西。一旦你有这个工作,用std::stringstream和std::getline替换打印出来的代码,拆分成括号之间的数字并打印数字。当一切正常并且你有一个数字对流时,找到一种存储这些对的好方法。 -
您应该在 C++ 中对数组使用
std::vector。