1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#include <iostream>
#include <string>
int main() {
std::string s =
"{\"pp\":\"alpha\"}..............."
"{\"cc\":\"one two three\"}.........."
"{\"cc\":\"four five\"},,,,,,,,,,,,,,,,,,"
"{\"pp\":\"beta\"}..............."
"{\"cc\":\"six seven\"}.........."
"{\"cc\":\"eight\"}..................."
"{\"cc\":\"nine\"},,,,,,,,,,,,,";
int pp_count = 0;
size_t start = s.find("\"pp\"");
while (start != s.npos) {
++pp_count;
start += 6;
size_t end = s.find('"', start);
if (end == s.npos) {
std::cout << "can't find pp ending quote\n";
break;
}
std::cout << pp_count << ": "
<< s.substr(start, end - start) << '\n';
int cc_count = 0;
size_t pp_next = s.find("\"pp\"", end);
start = s.find("\"cc\"", end);
while (start != s.npos && start < pp_next) {
++cc_count;
start += 6;
end = s.find('"', start);
if (end == s.npos) {
std::cout << "can't find cc ending quote\n";
break;
}
std::cout << " " << cc_count << ": "
<< s.substr(start, end - start) << '\n';
start = s.find("\"cc\"", end);
}
start = pp_next;
}
}
|