C의 문자열 ++에서 값의 쌍을 추출 할 수 있습니다 : 나는 동안 중첩 사용하여 시도했다어떻게 내가이 형식의 문자열이
...
"name1":1234 " name2 " : 23456 "name3" : 12345
등을 루프와 두 개의 정수 string::substr
에 사용할 위치와 길이를 저장하는 것이지만 적절한 방법을 찾을 수는 없습니다 (대부분 문자열로 끝납니다).
값을 저장하지 않아도됩니다. 값을 가져 오는 즉시 처리 할 수있는 함수를 호출 할 수 있기 때문에 값을 저장할 필요가 없습니다. 사전에
void SomeClass::processProducts(std::string str) {
unsigned int i = 0;
std::string name;
while (i < str.length()) {
if (str[i] == '\"') {
int j = 1;
while (str[i + j] != '\"') {
j++;
}
name = str.substr(i + 1, j - 1);
i += j;
}
else if (str[i] >= '0' && str[i] <= '9') {
int j = 1;
while (str[i + j] >= '0' && str[i + j] <= '9') {
j++;
}
//This is just processes the values
std::stringstream ss;
std::string num = str.substr(i, j);
ss.str(num);
int products = 0;
ss >> products;
if (products == 0) {
Util::error(ERR_WRONG_PRODUCTS);
}
int pos = getFieldPos(name);
if (pos == -1) {
Util::error(ERR_WRONG_NAME);
}
else {
fields[pos].addProducts(products);
}
i += j;
}
i++;
}
}
감사 :
이것은 내가 지금까지 한 일이다.
에 오신 것을 환영합니다. [The Tour] (http://stackoverflow.com/tour)를 읽으신 후 [Help Center] (http://stackoverflow.com/help/asking)의 자료를 참조하십시오. 여기에 물어보십시오. –
질문을 편집하여 실제 작동하지 않는 코드를 표시하십시오. 지금까지 직접 디버깅하기 위해 무엇을 했습니까? –
어떤 결과를 원하십니까? 예를 들어 "name1"에서 무엇을 원하겠습니까? –