C++,std::map 的迭代器

标签 c++ iterator stdmap

如何声明一个迭代器

std::map <T, Point <T> *> ,

哪里:

template <typename T>
struct TList
{
    typedef std::vector < std::map <T, Point <T> *> >  Type;
};

在下面的代码中

int main ()
{
    ....
    std::map <T, Point <T> *> ::iterator i_map;  //Error
    ...
}

g++ 显示此错误:

error: dependent-name `  std::map<T,Point<T>*,std::less<_Key>,std::allocator<std::pair<const T, Point<T>*> > >::iterator' is parsed as a non-type, but instantiation yields a type
note: say `typename  std::map<T,Point<T>*,std::less<_Key>,std::allocator<std::pair<const T, Point<T>*> > >::iterator' if a type is meant

最佳答案

使用 typename作为:

  typename std::map<T, Point <T> *>::iterator i_map;
//^^^^^^^^ here!

因为 iterator是从属名称(因为它取决于 map 的类型 参数 T ),所以 typename这里是必需的。

阅读此常见问题解答以获取详细说明:

Where and why do I have to put the "template" and "typename" keywords?

关于C++,std::map 的迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6021686/

相关文章:

c++ - 删除按键历史(GetKeyState)C++

c++ - 类构造函数 "C2248: ' std::basic_ios<_Elem,_Traits>::basic_ios':无法访问类 'std::basic_ios<_Elem,_Traits>"中声明的私有(private)成员

c++ - 更新元素 STL 列表列表

c++ - 映射插入键但不插入值

c++ - 强制 Visual Studio 链接 lib 文件中的所有符号

c++ - Conan, CMake.test() 生成 XML 报告

标准模板库 : <error-type>*? 的 C++ IntelliSense 错误

numpy - 使用自定义函数迭代和累积 numpy 数组

c++ - 映射唯一值和重复值的有效方法。可以访问键或值的位置

c++ - 使用自定义 less 运算符实现迭代 std::map 会给出更少的元素