c++ - 如何在 std::map 类中定义迭代器

标签 c++ stl iterator compiler-errors

我需要帮助来定义专业 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;
  }
};

错误:

In member function 'T* CaseInsensitiveMap<T>::lookup(const T&)':
    error: expected ';' before 'itr'
    error: 'itr' was not declared in this scope

最佳答案

typename 关键字添加到变量的类型中:

typename CaseInsensitiveMap<T>::iterator itr = find(key);

无论如何,你不应该继承STL容器。了解为什么不应该这样做 here .

编辑:由于您要实现的只是一个不区分大小写的映射,因此您可以通过这种方式实现它,无需继承 std::map,只需提供您自己的比较对象:

#include <iostream>
#include <map>
#include <string>

using namespace std;

struct nocase_compare {
    bool operator() (const unsigned char& c1, const unsigned char& c2) const {
      return tolower (c1) < tolower (c2);
    }
};

struct map_comparer {
  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
  }
};

template<class T>
struct CaseInsensitiveMap {
    typedef std::map<std::string, T, map_comparer> type;
};

int main() {
    CaseInsensitiveMap<int>::type my_map;
    my_map["foo"] = 12;
    std::cout << my_map["FoO"] << "\n";
    my_map["FOO"] = 100;
    std::cout << my_map["fOo"] << "\n";
}

这个输出:

12
100

关于c++ - 如何在 std::map 类中定义迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10438152/

相关文章:

c++ - 如何将不同文件的内容写入一个 vector 以供进一步使用 getline

C++ 环境变量

c++ - 在对象中使用或不使用 'this'

C++ 装饰器模式

c++ - 将加密数据保存在内存中

multithreading - 使用互锁增量来增量STL映射的value字段。它是线程安全的吗?

c++ - 如何非常快速地找到 2 个几乎相同的文件之间的差异?

c++ - 从任何地方访问头部 STL 列表

rust - 为什么借用的范围不是迭代器,但范围是?

c++ - 将迭代器返回到 STL 容器中的元素