gpt4 book ai didi

c++ - 将 std::list 扩展为循环列表

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:27:37 24 4
gpt4 key购买 nike

我想实现一个基于 std::list 的循环列表。我想从列表的好处中获益,但添加了一个特定的功能:它的迭代器运算符++ 和 -- 应该跳过边缘并且操作(插入/删除)不能使现有的迭代器无效。我处理模板的技能很弱,理解标准容器对我来说是不可能的。因此我需要你的帮助。到目前为止,我还没有那么远:D。抱歉,即使是很多帖子也对我没有任何帮助。

编辑:好吧,经过大量的工作,陡峭的学习曲线,从 std::list::iterator 继承的失败方法,短期的萧条和对你的方法的卑鄙返回(是的,你们都是对的)我终于做到了.受到您所有贡献的启发,我现在可以发布我上次所做的事情……大约 12 小时 :D 基本上是您的建议,但有不错的小运算符(operator)。

#pragma once
#include <list>
using std::list;

template<class T>
class cyclic_iterator;


template<class T>
class cyclicList : public list<T>
{
public:
typedef cyclic_iterator<T> cyclic_iterator;

cyclic_iterator cycbegin()
{// not the purpose, but needed for instanziation
return cyclic_iterator( *this, this->begin());
}

cyclic_iterator cycend()
{// not the purpose, but needed for instanziation
return cyclic_iterator( *this, this->end());
}
};




template<class T>
class cyclic_iterator
{
public:
// To hop over edges need to know the container
cyclic_iterator(){}
cyclic_iterator(typename list<T>::iterator i)
: mIter(i){}
cyclic_iterator(list<T> &c)
: mContainer(&c){}
cyclic_iterator(list<T> &c, typename list<T>::iterator i)
: mContainer(&c), mIter(i){}

cyclic_iterator<T>& operator=(typename list<T>::iterator i)
{// assign an interator
mIter = i;
return *this;
}

cyclic_iterator<T>& operator=(list<T> &c)
{// assign a container
mContainer = &c;
return *this;
}

bool operator==(const cyclic_iterator<T>& rVal) const
{// check for equality
return (this->mIter == rVal.mIter && this->mContainer == rVal.mContainer) ? true : false;
}

bool operator!=(const cyclic_iterator<T>& rVal) const
{// check for inequality
return !(this->operator==(rVal));
}

cyclic_iterator<T>& operator++()
{// preincrement
++mIter;
if (mIter == mContainer->end())
{ mIter = mContainer->begin(); }
return *this;
}

cyclic_iterator<T> operator++(int)
{ // postincrement
cyclic_iterator<T> tmp = *this;
++*this;
return tmp;
}

cyclic_iterator<T>& operator--()
{// predecrement
if (mIter == mContainer->begin())
mIter = --mContainer->end();
else --mIter;
return *this;
}

cyclic_iterator<T> operator--(int)
{// postdecrement
cyclic_iterator<T> tmp = *this;
--*this;
return tmp;
}

cyclic_iterator<T>& operator+=(int j)
{// hop j nodes forward
for (int i = 0; i < j; ++i)
++(*this);
return *this;
}

cyclic_iterator<T>& operator-=(int j)
{// hop j nodes backwards
for (int i = 0; i < j; ++i)
--(*this);
return *this;
}

T& operator*()
{
return *mIter;
}

typename list<T>::iterator & getStdIterator()
{
return mIter;
}

private:
list<T>* mContainer;
typename list<T>::iterator mIter;

};

最佳答案

你不能做一个不同的迭代器类型吗?

#include <iterator>
#include <list>

template <typename T, typename Alloc>
struct cyclic_iterator
: std::iterator<typename std::list<T, Alloc>::iterator::iterator_category, T>
{
typedef std::list<T, Alloc> list_type;

cyclic_iterator & operator++()
{
++iter;
if (iter == container.end()) { iter = container.begin(); }
return *this;
}

T & operator*() { return *iter; }

cyclic_iterator(typename list_type::iterator it, list_type & l)
: iter(it)
, container(l)
{
if (it == container.end()) { it = container.begin(); }
}

// everything else

private:
typename list_type::iterator iter;
list_type & container;
};

有 helper :

template <typename List>
cyclic_iterator<typename List::value_type, typename List::allocator_type>
make_cyclic_iterator(typename List::iterator it, List & l)
{
return cyclic_iterator<typename List::value_type, typename List::allocator_type>(it, l);
}

用法:

// goes round and round forever

for (auto ci = make_cyclic_iterator(mylist.begin(), mylist); ; ++ci)
{
std::cout << *ci << std::endl;
}

(通过一些修改,此代码可以在任何公开begin/end迭代器的容器上工作。)

关于c++ - 将 std::list 扩展为循环列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12882090/

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