STL问题,请支招....
//设计下面的结构,key:表示省名称 value:表示省包含的城市名称
map<string,vector<string>>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
template<class ProvinceType, class CityType>
class A
{
public:
typedef map<ProvinceType, vector<CityType>> MapType;
template<class K, class V>
class Finder
{
public:
Finder(const CityType& _v) : m_value(_v){}
bool operator()(const pair<K, V>& _Pair) const
{
V::const_iterator it = find(_Pair.second.begin(), _Pair.second.end(), m_value);
return(it != _Pair.second.end());
}
private:
CityType m_value;
};
// 通过城市查找归属的省
bool FindProvinceByCity(const string& strCity, string* strProvince)
{
MapType::iterator it = find_if(m_map.begin(), m_map.end(),
Finder<ProvinceType, vector<CityType>>(strCity));
if(it == m_map.end())
{
return false;
}
*strProvince = it->first;
return true;
}
MapType m_map;
};