小编Lol*_*ola的帖子

将文件读入结构(c ++)

我对C++非常陌生,我想将一个文本文件读入一个结构中.文本文件在第一行有一个double,后面的行作为礼物名称(愿望)存在.我创建了一个结构,愿望清单,作为双重和愿望的载体存在.所以我做了以下事情:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

struct Gift
{
    double price;
    string name;
};

typedef vector<Gift> Giftstore;
typedef vector<string> Wishes;


int size(Giftstore& g) {return static_cast<int>(g.size());}

int size(Wishes& w) {return static_cast<int>(w.size());}


struct Wishlist
{
    double budget;
    Wishes wishes;
};


void reading_wishlist(ifstream& file, Wishlist& wish_list)
{
    if (file)
    {
        double money;

        file>>money;
        wish_list.budget<<money;
    }

    while(file)
    {
        string name;
        getline(file, name)
        wish_list.wishes.push_back(name);
    }

    file.close();
};


void print(Wishlist wish_list)
{
    cout<<"Budget: "<<wish_list.budget<<endl;
    cout<<"Wishes: "<<endl;

    for(int i=0; i<size(wish_list.wishes()); i++) …
Run Code Online (Sandbox Code Playgroud)

c++ vector data-structures

3
推荐指数
1
解决办法
2345
查看次数

标签 统计

c++ ×1

data-structures ×1

vector ×1