将txt文件中每行的4个值存储到对象中-C ++

时间:2019-05-07 19:40:55

标签: c++ file vector

我想采用4个值(每个值用逗号分隔)并将它们存储到Airport对象。每行将有这四个值将存储在一起。因此,调用来自瑞典的所有对象,将会找到来自瑞典的所有对象,并检索相应的值。现在,我有4个不同的向量,它们存储所有4个对象值,但是不一定将所有4个属性/值一起设置为一个对象。非常感谢您的帮助。

文本文件外观示例-

1,Goroka,Goroka,巴布亚新几内亚,GKA,AYGA,-6.081689,145.391881,5282,10,U 2,Madang,Madang,巴布亚新几内亚,MAG,AYMD,-5.207083,145.7887,20,10,U


#include "pch.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
using namespace std;

struct Airport
{
string code;
string name;
string city;
string nation;

friend std::istream& operator>>(std::istream& input, Airport& a);
};

istream& operator>>(std::istream& input, Airport& a)
 {
getline(input, a.code, ',');
getline(input, a.name, ',');
getline(input, a.city, ',');
getline(input, a.nation);
return input;
}
////////////////////////////////////////////

vector<string> split(const string& s, const string& delim)
 {
const bool keep_empty = true;
vector<string> result;

if (delim.empty())
{
    result.push_back(s);
    return result;
}

string::const_iterator substart = s.begin(), subend;

while (true)
{
    subend = search(substart, s.end(), delim.begin(), delim.end());
    string temp(substart, subend);
    if (keep_empty || !temp.empty())
    {
        result.push_back(temp);
    }
    if (subend == s.end())
    {
        break;
    }
    substart = subend + delim.size();
}

return result;
}

// Sorting Function 
bool Sort_By_Name_Ascending(const Airport& a, const Airport& b)
{
return a.name < b.name;
}


int main()
{
vector<Airport> database;
Airport a;

char choice;
string chr;

ifstream inputFile;
inputFile.open("airports.dat");

if (!inputFile)
{
    cout << "File Access Error!";
    return 0;
}

string fileLine;
cout << "Reading File ..." << endl;

while (!inputFile.eof())
{
    getline(inputFile, fileLine);
    vector<string> lineVector = split(fileLine, ",");
    if (lineVector[4].length() == 3)
    {
        while (inputFile >> a)
        {
            database.push_back(a);
        }
    }

}

cout << "What would you like to do?\nA. Sort in Alphabetical Order.\nB. 
Delete an Airport.\nC. Exit Program." << endl;

cin >> choice;

switch (choice)
{
case 'A':
    sort(database.begin(), database.end(), Sort_By_Name_Ascending);
break;

case 'B':
    cout << "Deleting a value" << endl;

break;

case 'C':
return 0;
break;
}


return 0;
}

1 个答案:

答案 0 :(得分:2)

您可能希望组织数据结构以建模输入数据:

struct Airport
{
  std::string    code;
  std::string    name;
  std::string    city;
  std::string    nation;
};

下一步是使operator>>重载以从流中读取实例:

struct Airport
{
  //...
  friend std::istream& operator>>(std::istream& input, Airport& a);
};
std::istream& operator>>(std::istream& input, Airport& a)
{
    std::getline(input, a.code, ',');
    std::getline(input, a.name, ',');
    std::getline(input, a.city, ',');
    std::getline(input, a.nation);
    return input;
}

这简化了输入:

std::vector<Airport> database;
Airport a;
while (inputfile >> a)
{
  database.push_back(a);
}

要进行排序,您可以提出一些函数或函数对象并将其提供给std::sort

bool Sort_By_Name_Ascending(const Airport& a, const Airport& b)
{
  return a.name < b.name;
}

//...
std::sort(database.begin(), database.end(), Sort_By_Name_Ascending);

编辑1:整个记录或一行
整个行和仅对您感兴趣的字段进行建模之间存在差异。如果您的记录是文本行(一条记录==一条文本行),则您可能希望以以下方式读取文本行:一个字符串,然后提取您感兴趣的字段。

std::istream& operator>>(std::istream& input, Airport& a)
{
    std::string record;
    std::getline(input, record);

    // Now extract the interesting fields from the string.
    std::istringstream record_stream;
    unsigned int record_number;
    char         comma;
    record_stream >> record_number;  // Read but ignore.
    record_stream >> comma;          // Read but ignore.
    std::getline(record_stream, a.code, ',');
    std::getline(record_stream, a.name, ',');
    std::getline(record_stream, a.city, ',');
    std::getline(record_stream, a.nation);
    return input;    
}