Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 721715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:54:20+00:00 2026-05-14T05:54:20+00:00

Based on the following question: Check if one string is a rotation of other

  • 0

Based on the following question: Check if one string is a rotation of other string

I was thinking of making a cyclic iterator type that takes a range, and would be able to solve the above problem like so:

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;
}

My question, Is there already something like this available? I’ve checked Boost and STL and neither have an exact implementation.

I’ve got a simple hand-written (derived from a std::forward_iterator_tag specialised version of std::iterator) one but would rather use an already made/tested implementation.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:54:21+00:00Added an answer on May 14, 2026 at 5:54 am

    There is nothing like this in the standard. Cycles don’t play well with C++ iterators because a sequence representing the entire cycle would have first == last and hence be the empty sequence.

    Possibly you could introduce some state into the iterator, a Boolean flag to represent “not done yet.” The flag participates in comparison. Set it true before iterating and to false upon increment/decrement.

    But it might just be better to manually write the algorithms you need. Once you’ve managed to represent the whole cycle, representing an empty sequence might have become impossible.

    EDIT: Now I notice that you specified the number of cycles. That makes a big difference.

    template< class I >
    class cyclic_iterator
     /* : public iterator< bidirectional, yadda yadda > */ {
        I it, beg, end;
        int cnt;
        cyclic_iterator( int c, I f, I l )
            : it( f ), beg( f ), end( l ), cnt( c ) {}
    public:
        cyclic_iterator() : it(), beg(), end(), cnt() {}
    
        cyclic_iterator &operator++() {
            ++ it;
            if ( it == end ) {
                ++ cnt;
                it = beg;
            }
        } // etc for --, post-operations
    
        friend bool operator==
            ( cyclic_iterator const &lhs, cyclic_iterator const &rhs )
            { return lhs.it == rhs.it && lhs.cnt == rhs.cnt; } // etc for !=
    
        friend pair< cyclic_iterator, cyclic_iterator > cycle_range
            ( int c, I f, I l ) {//factory function, better style outside this scope
            return make_pair( cyclic_iterator( 0, f, l ),
                              cyclic_iterator( c, f, l ) );
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For the following question, I am looking for an answer that is based on
My question is concerning SQL connection status, load, etc. based on the following code:
Now following my series of python newbie questions and based on another question .
The following php check a question number against a range and assigns a chapter
I have the following code that is working to assign a locale based first
This question is based on my plan at the thread . The following figure
I have the following based on another SE question ( Hash Rings On PostgreSql)
My question is the following. I have xml that is versioned by a namespace.
The following range-based for loop works fine in VS2012: int values[] = {1, 2,
Based on the following example URL structure: mysite.com/mypage.aspx?a=red&b=green&c=blue Pages in the application use ASP.net

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.