匹配字符串缩写

问题描述:

我正在处理一个问题,其中有两个机场名称的输入文件。每个机场都有一个标准化缩写,如旧金山(SFO)和洛杉矶(LAX)匹配字符串缩写

第一个输入文件使用这些缩写,第二个文件使用城市名称。

我正在寻找一个优雅的解决方案,而不是使用std::string.compare()以及30个不同的if-else条件。

+3

使用[性病::图](http://en.cppreference.com/w/cpp/container/map) –

Map各缩写到其全名

// Init map 
std::map<std::string, std::string> airports; 
airports["SFO"] = "San Francisco"; 
airports["LAX"] = "Los Angeles"; 

bool cmpAirports(std::string abbr, std::string fullname) 
{ 
    auto fname = airports.find(abbr); 
    if (fname == airports.end()) 
     return 0; // No airport with such abbreviation found 
    return fname->second == fullname; 
}