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

时间:2011-05-16 18:27:39

标签: 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

4 个答案:

答案 0 :(得分:5)

typename用作:

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

由于iterator是依赖名称(因为它取决于地图的类型参数T),因此此处需要typename

阅读此常见问题解答,了解详细说明:

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

答案 1 :(得分:0)

typename std::map <T, Point <T> *> ::iterator i_map;是否有效?

答案 2 :(得分:0)

typename TList<T>::Type::value_type::iterator怎么样?

答案 3 :(得分:0)

将“typename”放在错误行之前:std::map <T, Point <T> *> ::iterator i_map;

示例:

typename vector<T>::iterator vIdx; 

// On your case : typename std::map <T, Point<T>*>::iterator i_map;

vIdx= find(oVector->begin(), oVector->end(), pElementToFind); //To my case
相关问题