C++抽象编程——STL实战(5)——map在数据文件查找中的应用

    xiaoxiao2021-04-15  68

    介绍完了数据文件的基本操作,我们就要来试试怎么用STL中的map集合来处理一些文本文档。首先我们来看一道题目:

    如果你经常飞行,您可以快速了解,世界各机场都有国际航空运输协会(IATA)分配的三字母代码。例如纽约市的肯尼迪机场(John F. Kennedy),字母代码JFK。 然而,其他代码难以识别。大多数基于网络的旅行系统提供了一些查询这些代码作为其客户服务的方法。

    假设你被要求编写一个简单的C ++程序,它从用户那里读取一个三字母的机场代码,并以该机场的位置作出响应,你需要的数据是一个名为AirportCodes.txt的文本文件,其中包含IATA已分配的数千个机场代码的列表。文件的每一行都包含三个字母的代码,一个等号和机场的位置。按照国际机场理事会编制的文件按2009年乘客人数降序排列,我们为了好看,就把文件减少点。 接下来我们就用代码来实现它:

    #include <iostream> #include <fstream> #include <string> #include <cctype> #include <cstdlib> #include <map> using namespace std; /* 函数原型 */ void readCodeFile(map<string,string> & mymap); string toUpperCase(string str); bool isFind(map<string,string> mymap, string code); void error(string msg); /*主函数*/ int main() { map<string,string> airportCodes; readCodeFile(airportCodes); while (true) { string line; cout << "Airport code: "; getline(cin, line); if (line == "") break; string code = toUpperCase(line); if (isFind(airportCodes,code)) { cout << code << " is in " << airportCodes[code] << endl; } else { cout << "There is no such airport code" << endl; } } return 0; } //将字符串转变为大写 ,以便查找 string toUpperCase(string str) { string str1 = ""; for(int i = 0; i <= str.length(); i++){ str1 += toupper(str[i]); } return str1; } //在map中读取 void readCodeFile(map<string,string> & mymap) { ifstream infile; infile.open("F:\\AirportCodes.txt"); if (infile.fail()) error("Can't read the data file"); while (true) { string line; getline(infile, line); if (infile.fail()) break; if (line.length() < 4 || line[3] != '=') { error("Illegal data line: " + line); } string code = toUpperCase(line.substr(0, 3)); mymap[code] = line.substr(4);//把4以后的字符串都赋值给code } infile.close(); } //输出错误信息 void error(string msg){ cerr << msg << endl; exit(EXIT_FAILURE); } //判断是否找到 bool isFind(map<string,string> mymap, string code){ map<string,string>::iterator it; it = mymap.find(code); if(it == mymap.end()) return false; return true; }

    结果如下:

    分析

    这个程序实现了map集合在文件中查找的重要性,它把字符串分开,用substr函数分成两半,前者用于做key,后者用于做value。然后我们自定义一个isFind()函数,判断是否找到这个key对应的value,如果是那就输出,否则就输出找不到。 这里面用到了很多我们之前提到过的东西: mymap[code] = line.substr(4); ——C++抽象编程——如何修改map中的值 string toUpperCase(string str) ;——C++抽象编程——字符串(3)——字符串判断与大小写转换 infile.open(“F:\AirportCodes.txt”);——C++抽象编程——数据文件的各种输出方式

    下面附上txt的文件内容:

    ATL=Atlanta, GA, USA ORD=Chicago, IL, USA LHR=London, England, United Kingdom HND=Tokyo, Japan LAX=Los Angeles, CA, USA CDG=Paris, France DFW=Dallas/Ft Worth, TX, USA FRA=Frankfurt, Germany PEK=Beijing, China MAD=Madrid, Spain DEN=Denver, CO, USA AMS=Amsterdam, Netherlands JFK=New York, NY, USA HKG=Hong Kong, Hong Kong LAS=Las Vegas, NV, USA IAH=Houston, TX, USA PHX=Phoenix, AZ, USA BKK=Bangkok, Thailand SIN=Singapore, Singapore MCO=Orlando, FL, USA EWR=Newark, NJ, USA DTW=Detroit, MI, USA SFO=San Francisco, CA, USA NRT=Tokyo, Japan LGW=London, England, United Kingdom MSP=Minneapolis, MN, USA DXB=Dubai, United Arab Emirates MUC=Munich, Germany MIA=Miami, FL, USA CLT=Charlotte, NC, USA
    转载请注明原文地址: https://ju.6miu.com/read-671189.html

    最新回复(0)