我想使用字符串类来从字符串中提取一些信息。给定字符串:< 12,苹果>,< 20,橙色>,< 49,iPhone>
我想让12、20、49到INT数组。这意味着a [0] = 12,a [1] = 20,a [2] = 49。
,让苹果,橙色,iPhone到字符串阵列。这意味着B [0] =" Apple",B [1] =" Orange" B [2] =" iPhone"
我应该如何做?
假设字符串遵循格式 <int,string>,...
。请在下面找到伪代码:
Loop through the string `str` and
{
smaller_sign_pos = str.find('<', prev_pos)
entry_comma_pos = str.find(',', smaller_sign_pos+1)
greater_sign_pos = str.find('>', entry_comma_pos+1)
if (all pos values are not `npos`)
{
int_value = atoi(str.substr(smaller_sign_pos+1, entry_comma_pos-smaller_sign_pos-1))
str_value = str.substr(entry_comma_pos+1, greater_sign_pos-entry_comma_pos-1)
prev_pos = greater_sign_pos+1
append int_value to int array
append str_value to string array
optional: you can check if the comma after '>' exists
}
else
{
break or set the end of loop flag
}
}