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

C++, iterator to std::map

本文关键字:map std 迭代器 C++      更新时间:2023-10-16

如何将迭代器声明到

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是一个依赖名称(因为它取决于映射的类型参数T),所以这里需要typename

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

我必须在哪里以及为什么必须放置"模板"和"类型名称"关键字?

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

typename TList<T>::Type::value_type::iterator呢?

把"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
相关文章: