模板类中的迭代器

iterator in template class

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

我有以下模板类:

template <typename _Type, typename _Comparator = equal_to<_Type> >
class CSearch
{
...
};

它应该存储STL内容,如list, set或string。我将所有元素(例如字符串)存储在私有类成员中:

map<int,_Type> seqs;

现在我想使用迭代器,但是<_Type>::const_iterator有一个问题。

template <typename _Type, typename _Comparator>
void CSearch<_Type,_Comparator>::Foo1(int id, const _Type & needle)
{
seqs.insert(make_pair(id,needle));
for(_Type::const_iterator it=seqs[0].begin();it!=seqs[0].end();it++)
cout<<*it<<" ";
cout<<endl;
}

或类似

 for(map<int,_Type>::const_iterator it=seqs.begin();it!=seqs.end();it++)
 cout<<*it<<" ";

<_Type>::const_iterator是依赖类型

将其改为typename <_Type>::const_iterator