Home  >  Q&A  >  body text

c++ - 读入txt文件 如何存入struct 和map

大家好,之前提过一个关于txt的问题但是没有说清楚,从新提问一次 不好意思!
现在有一个txt文档,要求是提取文档,然后根据时间差计算。
现在重点文件都木有提取好 就先不谈计算啦。

现在问题是我想用multimap提取把公司名字作为key,然后存储时间和数字。
但是用multimap直接提取,不知道为什么it->first出现了很多次这两个公司名,但是如果用upper_bound最后不是应该只有两个么?
代码如下

        while(infile>>date>>time1>>compnay_name>>number){
            
            

        cout<<date<<" "<<time1<<" "<<compnay_name<<" "<<number<<'\n';
        
        trans_map.insert(pair<string,double>(compnay_name,number));
        multimap<string, double>::iterator  iter1 = trans_map.find(compnay_name);
        multimap<string, double>::iterator  last = trans_map.upper_bound(compnay_name);


            for (; iter1 != last; ++iter1){
                cout << iter1->first << endl;

            }

这个放弃了之后,想用struct自定义一个格式,现在公司名字可以找到了。
但是想问一下大神们,如果我想取出公司名,存到一个array应该怎么做呢?

txt文档 和代码 如下:
05-03-2016 11:03:15.034371 "GOOGLE" 804.60
05-03-2016 11:04:15.034371 "GOOGLE" 99.60
05-03-2016 11:04:55.034371 "GOOGLE" 4.60
05-03-2016 11:03:05.034371 "APLLE" 123.60
05-03-2016 11:04:05.034371 "APLLE" 23.60
05-03-2016 11:04:25.034371 "APLLE" 89.60

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

using namespace std;
multimap<string,double> trans_map;
multimap<string,double>::iterator iter;



struct CompanyRecord{
    string Company;
    
    struct CompanyInfo{
        string  Date;
        string  TimeT;
        double  Number;
    };
    
    CompanyInfo cf;
    CompanyRecord(string Company_, string Date_, string TimeT_, double Number_){
        Company = Company_;
        cf.Date = Date_;
        cf.Number = Number_;
        cf.TimeT = TimeT_;
    }
};

string file1("/Users/FanY/Desktop/log.txt");
ifstream infile;
string date;
string time1;
string compnay_name;
double number;



void ReadTxt2(){
    
    
    typedef multimap<string, CompanyRecord::CompanyInfo>  cmap;
    cmap mm;
    
    
    infile.open(file1.c_str());
    
    if(!infile){
        
        cerr<<"error:enable open file:"<<file1<<endl;
        
    }
    else{
        
        cout<<"open file:"<<file1<<" success"<<endl;
        
        
        while(infile>>date>>time1>>compnay_name>>number){
            
            CompanyRecord datainfo = CompanyRecord(compnay_name,date,time1,number);
            
            pair<string, CompanyRecord::CompanyInfo> paircompany(datainfo.Company, datainfo.cf);
            mm.insert(paircompany);
            
            cmap::iterator i = mm.find(datainfo.Company);
            cmap::iterator lastone = mm.upper_bound(datainfo.Company);
            

            
            for( ; i != lastone; ++i){
                cout << i->first<<" "<< i->second.Date<< " " << i->second.TimeT<< endl;
            }
            
        }
        
        
    }
    
}

int main(){
    
    ReadTxt2();
    
    
    return 0;
    
    
}
ringa_leeringa_lee2714 days ago767

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 14:58:13

    I didn’t understand the description of the question at the beginning.
    The error is because the first parameter type of CompanyRecord's constructor is string *, and you passed it a string.
    Also don’t use global variables.

    multimap<string, double> trans_map;
    while(infile >> date >> time1 >> company_name >> number)
        trans_map.insert({ company_name, number });
        
    for(auto iPair : trans_map)
        cout << iPair.first << " " << iPair.second << endl;
    输出:
    "APLLE" 123.6
    "APLLE" 23.6
    "APLLE" 89.6
    "GOOGLE" 804.6
    "GOOGLE" 99.6
    "GOOGLE" 4.6
    map<string, vector<double>> trans_map;
    while(infile >> date >> time1 >> company_name >> number)
        trans_map[company_name].push_back(number);
        
    for(auto iPair : trans_map)
    {
        cout << iPair.first << endl;
        for(double n : iPair.second)
            cout << n << endl;
    }
    输出:
    "APLLE"
    123.6
    23.6
    89.6
    "GOOGLE"
    804.6
    99.6
    4.6

    The usage of struct is similar.

    reply
    0
  • Cancelreply