http://www.martinbroadhurst.com/how-to-split-a-string-in-c.html#:~:text=How%20to%20split%20a%20string%20in%20C%2B%2B.%201,pystring%3A%3Asplit.%2010%20Use%20my%20C%20split%20function.%20
// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include<fstream>
#include<sstream>
#include<vector>
#include<string>
#include<sstream>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
class A
{
public:
A(iostream &a){
a>>A::rate;
};
A(){ A::rate++;};
static double get_rate(){return A::rate;}
static void set_rate(double r){ A::rate =r;}
public:
static double rate;
~A()
{
A::rate--;
std::cout<<"deconstrucure"<<A::rate<<std::endl;
}
;
};
double A::rate =0;
int f(iostream &S)
{
int h;
S>>h;
return h;
}
struct PersonInfo
{
string name;
vector<string> phone;
};
int main()
{
// cout<< "**********************************"<<endl;
// cout<<f(static_cast<iostream&>(cin) )<<endl;
// A mm(static_cast<iostream&>(cin));
// cout<<"A constructure with [cin]:"<<mm.rate<<endl;
// cout<< "**********************************"<<endl;
fstream f("s.txt",ios_base::out);
if(f.is_open())
{
f<< "xiaozhang,223,22"<<endl;
f<<"xiaowang,3,34"<<endl;
// f.write("xiaozhang,xiaoliang",30)<<endl;
// f.write("xiaowang,xiaozhao",30)<<endl;
cout<<"write success"<<endl;
}
else
{
cout<< "not opened"<<endl;
}
f.close();
fstream f1("s.txt",ios_base::in);
if(f1.is_open())
{
vector<PersonInfo> j;
string h;
while(std::getline(f1,h))
{
stringstream ss(h);
string s0;
PersonInfo p;
int i = 0;
while(std::getline(ss,s0,','))
{
if( 0 == i++)
p.name = s0;
else p.phone.push_back(s0);
}
j.push_back(p);
}
for (auto &a: j) {
cout<< a.name<<":"<<endl;
for(auto &n : a.phone)
cout<<n<<endl;
}
cout<< "read success"<<endl;
}
f1.close();
}
//格式化输出
for (auto &a: j) {
ostringstream formatted;
for(auto & b:a.phone)
{
formatted<< " "<<b;
}
cout<< a.name <<":"<< formatted.str()<<endl;
}