## C++中有标准循环迭代器吗？内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (2)
• 关注 (0)
• 查看 (80)

``````std::string s1 = "abc" ;
std::string s2 = "bca" ;
std::size_t n = 2; // number of cycles
cyclic_iterator it(s2.begin(),s2.end(),n);
cyclic_iterator end;

if (std::search(it, end, s1.begin(),s1.end()) != end)
{
std::cout << "s1 is a rotation of s2" << std::endl;
}
``````

### 2 个回答

```#include <vector>

template <typename T, typename Container = std::vector<T>, typename Iterator = Container::iterator>
class RingIterator : public std::iterator <std::bidirectional_iterator_tag, T, ptrdiff_t>
{
Container& data;

Iterator   cursor;
Iterator   begin;
Iterator   end;

public:

RingIterator (Container& v) : data(v), cursor(v.begin()), begin(v.begin()), end(v.end()) {}

RingIterator (Container& v, const Iterator& i) : data(v), cursor(i), begin(v.begin()), end(v.end()) {}

RingIterator (Container& v, const Iterator& i, const Iterator& j) : data(v), cursor(i), begin(i), end(j) {}

RingIterator (Container& v, size_t i) : data(v), cursor(v.begin() + i % v.size()), begin(v.begin()), end(v.end()) {}

bool operator == (const RingIterator& x) const
{
return cursor == x.cursor;
}

bool operator != (const RingIterator& x) const
{
return ! (*this == x);
}

reference operator*() const
{
return *cursor;
}

RingIterator& operator++()
{
++cursor;
if (cursor == end)
cursor = begin;
return *this;
}

RingIterator operator++(int)
{
RingIterator ring = *this;
++*this;
return ring;
}

RingIterator& operator--()
{
if (cursor == begin)
cursor = end;
--cursor;
return *this;
}

RingIterator operator--(int)
{
RingIterator ring = *this;
--*this;
return ring;
}

RingIterator insert (const T& x)
{
return RingIterator (data, data.insert (cursor, x));
}

RingIterator erase()
{
return RingIterator (data, data.erase (cursor));
}
};

template <typename T, typename Iterator>
class CyclicIterator : public std::iterator <std::bidirectional_iterator_tag, T, ptrdiff_t>
{
Iterator   cursor;
Iterator   begin;
Iterator   end;

public:

CyclicIterator (const Iterator& i, const Iterator& j) : cursor(i), begin(i), end(j) {}

bool operator == (const CyclicIterator& x) const
{
return cursor == x.cursor;
}

bool operator != (const CyclicIterator& x) const
{
return ! (*this == x);
}

reference operator*() const
{
return *cursor;
}

CyclicIterator& operator++()
{
++cursor;
if (cursor == end)
cursor = begin;
return *this;
}

CyclicIterator operator++(int)
{
CyclicIterator ring = *this;
++*this;
return ring;
}

CyclicIterator& operator--()
{
if (cursor == begin)
cursor = end;
--cursor;
return *this;
}

CyclicIterator operator--(int)
{
CyclicIterator ring = *this;
--*this;
return ring;
}
};

#include <iostream>
#include <iomanip>

#include <list>

enum { CycleSize = 9, ContainerSize };

template <typename cyclicIterator>
void test (cyclicIterator& iterator, size_t mn)
{
int m = mn;
while (m--)
for (int n = mn; n--; ++iterator)
std::cout << std::setw(3) << *iterator << ' ';
--iterator;
m = mn;
while (m--)
for (int n = mn; n--; --iterator)
std::cout << std::setw(3) << *iterator << ' ';
}

template <typename containers>
{
while (container.size() < ContainerSize)
container.push_back (container.size());
}

void main (void)
{
typedef std::vector<int>     vContainer;
typedef vContainer::iterator vIterator;
typedef std::list<int>       lContainer;
typedef lContainer::iterator lIterator;

vIterator vbegin = v.begin() + 1;

RingIterator <int, vContainer, vIterator> vring (v, vbegin, v.end());
CyclicIterator <int, vIterator> vcycle (vbegin, v.end());