gpt4 book ai didi

c++ - 集合中倒数第二个的模板; reverse_iterator 不编译

转载 作者:太空宇宙 更新时间:2023-11-04 13:08:31 27 4
gpt4 key购买 nike

我正在将两个现有函数转换为模板,这两个函数检查迭代器是否是 std::map 中的最后一项和倒数第二项。 isLastItem 模板编译,isSecondLastItem 失败并出现我不理解的错误消息。

导致错误的一行是 typename T::reverse_iterator secondLastIt = collection.rbegin();

但是模板和非模板版本看起来是一样的。

typedef std::map<time_t, std::string>  TInfoMap;

class MainClass
{
public:
MainClass() {};
void test();

private:
/* Doesn't work */
template <typename T>
bool
isSecondLastTemplate(const T &collection, const typename T::const_iterator &searchItem)
{
typename T::reverse_iterator secondLastIt = collection.rbegin();
return true;
}

/* this works */
bool
isSecondLastNoTemplate(const TInfoMap &infoMap, const TInfoMap::const_iterator &searchItem)
{
TInfoMap::reverse_iterator secondLastIt = infoMap.rbegin();
return true;
}

/* This works */
template <typename T>
bool
isLastItem(const T &collection, const typename T::const_iterator &searchItem)
{
if (collection.size() == 1)
return true;

// point to last item
typename T::const_iterator lastItem = collection.end();
--lastItem;

return searchItem == lastItem;
}

private:
TInfoMap mInfoMap;
};


void
MainClass::test()
{
mInfoMap[1] = "One";
mInfoMap[2] = "Two";
mInfoMap[3] = "Three";

TInfoMap::iterator it = mInfoMap.begin();

if (isLastItem(mInfoMap, it))
printf("Last Item!\n");

if (isSecondLastTemplate(mInfoMap, it))
printf("Second Last!\n");

if (isSecondLastNoTemplate(mInfoMap, it))
printf("Second Last!\n");

}

int
main(int argc, char **argv)
{
MainClass theStuff;
theStuff.test();
}

编译器错误:

In constructor ‘std::reverse_iterator<_Iterator>::reverse_iterator(const std::reverse_iterator<_Iter>&) 
[with _Iter = std::_Rb_tree_const_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >,
_Iterator = std::_Rb_tree_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >]’:

junk3.cpp:19: instantiated from here
error: no matching function for call to ‘std::_Rb_tree_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::_Rb_tree_iterator(std::_Rb_tree_const_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >)’
candidates are:
std::_Rb_tree_iterator<_Tp>::_Rb_tree_iterator(std::_Rb_tree_node<_Tp>*) [with _Tp = std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]
std::_Rb_tree_iterator<_Tp>::_Rb_tree_iterator() [with _Tp = std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]

std::_Rb_tree_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>,
std::allocator<char> > > >::_Rb_tree_iterator(const std::_Rb_tree_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&)

最佳答案

你需要一个 const_reverse_iterator 因为 collectionconst

  template <typename T>
bool
isSecondLastTemplate(const T &collection, const typename T::const_iterator &searchItem)
{
typename T::const_reverse_iterator secondLastIt = collection.rbegin();
^^^^^^^^^^^^^^^^^^^^^^
return true;
}

关于c++ - 集合中倒数第二个的模板; reverse_iterator 不编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41020416/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com