我对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)