您的位置:首页 > 编程语言 > C语言/C++

C++ 中的iostringstream的使用

2015-06-26 17:22 393 查看
#include <iostream>

#include <fstream>

#include <sstream>

#include <vector>

#include <stdlib.h>

struct personInfo{

    std::string name;

    std::vector<std::string> phone;

};

int main()

{

    std::vector<personInfo> personInfoList;

    std::ifstream cin("1.txt");

    if(!cin)

    {

        std::cout<<"open file 1.txt error!" << "\n";

        exit(EXIT_FAILURE);

    }

    if(cin.is_open())

    {

        std::string line;

        while(std::getline(cin,line))

        {

            std::istringstream str(line);

            personInfo people;

            str>>people.name;

            std::string world;

            while(str>>world)

                people.phone.push_back(world);

            personInfoList.push_back(people);

        }

    }

    cin.close();

    for(std::vector<personInfo>::const_iterator entry = personInfoList.begin();

            entry != personInfoList.end(); ++entry)

    {

        std::ostringstream  matted;

        for(std::vector<std::string>::const_iterator nums = entry->phone.begin();

            nums != entry->phone.end(); ++nums)

            matted<<" "<<*nums;

        std::cout<<matted.str()<<"\n";

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c C++