c++ 字符串分割

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;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值