我試圖接收一串像這樣的點(2,4),(5,8),(12,7),(15.54,3.65)並將其分離爲X數組和y數組,請幫助我仍試圖這樣的初學者,這是災難性的將字符串分隔爲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++;
}
建議整理括號並提供[mcve] – user4581301
推薦:打破工作。 [使用'std :: string :: find'](http://en.cppreference.com/w/cpp/string/basic_string/find)來幫助找到'('和')'然後打印出來它們之間。一旦你有了這個工作,用['std :: stringstream'](http://en.cppreference.com/w/cpp/io/basic_stringstream)和'std :: getline'替換打印出來的代碼來分割成過去在括號之間的數字並打印數字。當這一切都正常工作,你有一個號碼對流,找到一個好方法來存儲對。 – user4581301
謝謝你,我會試試這個 – Antonio