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
|
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <limits>
struct PersonInfo
{
std::string Name;
int Age {};
std::string Sex;
};
int main()
{
const std::string inp {"{Name:Jhon, Age:20, Sex:Male}, {Name:Kyle, Age:25, Sex:Male} , {Name:foo bar, Age:32, Sex:Female}"};
std::istringstream iss(inp);
std::vector<PersonInfo> vpi;
for (std::string elem; getline(iss, elem, '}'); ) {
PersonInfo pi;
const std::string sv {elem.substr(elem.rfind('{') + 1)};
std::istringstream iel(sv);
std::string tmp;
getline(iel, pi.Name, ':');
getline(iel, pi.Name, ',');
getline(iel, tmp, ':');
iel >> pi.Age;
iel.ignore(std::numeric_limits<std::streamsize>::max(), ',');
getline(iel, pi.Sex, ':');
getline(iel, pi.Sex, ',');
vpi.push_back(pi);
}
for (const auto& [name, age, sex] : vpi)
std::cout << name << ' ' << age << ' ' << sex << '\n';
}
| |