我需要帮助来定义专业map
,我无法获得专业的 map::iterator 来正确编译。
我如何需要为find()
调用定义这个迭代器?
代码:
// My case-insensitive-less functor, which has historically worked fine for me:
struct ci_less : std::binary_function<std::string, std::string, bool>
{
// case-independent (ci) compare_less binary function
struct nocase_compare : public std::binary_function<unsigned char,unsigned char,bool>
{
bool operator() (const unsigned char& c1, const unsigned char& c2) const {
return tolower (c1) < tolower (c2);
}
};
bool operator() (const std::string & s1, const std::string & s2) const {
return std::lexicographical_compare (s1.begin (), s1.end (),
s2.begin (), s2.end (),
nocase_compare()); // comparison
}
};
//My offending code:
template <class T>
class CaseInsensitiveMap : public map<string, T, ci_less>
{
public:
// This one actually works, but it requires two "find()" calls.
// I can't ethically call find twice.
const T* lookup(const T& key) const {
if (find(key) == map<string, T, ci_less>::end()) return 0;
else return &find(key)->first;
}
// This one complains with errors shown below.
T* lookup(const T& key) {
CaseInsensitiveMap<T>::iterator itr = find(key);
if (itr == map<string, T, ci_less>::end()) return 0;
else return itr->second;
}
};
错误:
在成员函数中
'T* CaseInsensitiveMap<T>::lookup(const T&)'
:错误:错误之前
预期 :未在此范围内声明';'
'itr'
'itr'