从文件中读取地图

时间:2016-04-23 21:21:17

标签: c++ dictionary

我已经摸不着头几天了。我在从一个文件中的映射中插入值时遇到问题,我尝试使用带有插入器迭代器的复制算法,没有运气,当我尝试使用循环时出错了。

#ifndef CPLANE_H
#define CPLANE_H
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class CPlane
{
private:
string m_strBrand;
int m_iHours;
public:
friend istream& operator>>(istream& fromStream, CPlane& pl)
{
fromStream>>pl.m_strBrand;
fromStream>>pl.m_iHours;
return fromStream;
}

#ifndef CAIRTRAVEL_H
#define CAIRTRAVEL_H
#include "CPlane.h"
#include<map> 
#include<iterator>
#include<list>
class CAirtravel
{
private:
string m_strCompanyName;
string m_strNationality;
map<CPlane,unsigned> m_planeAndCount;
multimap<string,unsigned> m_destAndFlights;

public:
void readIn(istream& is)
{
    CPlane pl;
    unsigned value,value2;
    string val;
    is>>m_strCompanyName;
    is>>m_strNationality;
    //copy(istream_iterator<pair<CPlane,unsigned>>   (is),istream_iterator<pair<CPlane,unsigned>>(),inserter(m_planeAndCount()));
     for(int i =0;i<2;i++)
     {
        is >> pl;
        is >> value;
        m_planeAndCount.insert(pair<CPlane,unsigned>(pl,value));
     }
    for(int i =0;i<2;i++)
     {
         is >> val;
         is >> value2;
         m_destAndFlights.insert(pair<string,unsigned>(val,value2));
     }
 }
friend istream& operator>>(istream &fromStream, CAirtravel& tr)
{
    tr.readIn(fromStream);
    return fromStream;
}

#ifndef CAIRPORT_H
#define CAIRPORT_H
#include "CAirtravel.h"
#include<algorithm>
#include<fstream>

class CAirport 
{
private:
string m_strName;
unsigned m_uNum;
multimap<CAirtravel,unsigned>m_mAirtr;

public:

CAirport(){}

CAirport(const CAirport& port)
{
    m_strName = port.m_strName;
    m_uNum = port.m_uNum;
    m_mAirtr = port.m_mAirtr;
}

CAirport(const string& FileName,const string& name, const int& num)
{
    CAirtravel tr;
    unsigned value;
    m_strName = name;
    m_uNum = num;
    ifstream iStream(FileName);
    if(iStream.good())

    {
        for(int i=0;i<2;i++)
        {
        iStream >> tr >> value;
        m_mAirtr.insert(pair<CAirtravel,unsigned>(tr,value));
        }
    }
    iStream.close();

 }

我的文件构建如下:

AirFr
France
bmw 42 35
audi 35 643
Bulgaria 24
France 13
123

TurkishAirLines
Turkey
dsa 15 16
asd 73 643
Adasfsdfs 43
ywyerye 13
52534

0 个答案:

没有答案