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

  • Home
  • SEARCH
  • 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 571613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:31:33+00:00 2026-05-13T13:31:33+00:00

I have a class called Action , which is essentially a wrapper around a

  • 0

I have a class called Action, which is essentially a wrapper around a deque of Move objects.

Because I need to traverse the deque of Moves both forward and backwards, I have a forward iterator and a reverse_iterator as member variables of the class. The reason for this is becuase I need to know when I have gone one past the "end" of the deque, both when I am going forwards or backwards.

The class looks like this:

class Action
{
public:
    SetMoves(std::deque<Move> & dmoves) { _moves = dmoves; }
    void Advance();
    bool Finished() 
    {
        if( bForward )
            return (currentfwd==_moves.end());
        else
            return (currentbck==_moves.rend());
    }
private:
    std::deque<Move> _moves;
    std::deque<Move>::const_iterator currentfwd;
    std::deque<Move>::const_reverse_iterator currentbck;
    bool bForward;
};

The Advance function is as follows:

void Action::Advance
{
    if( bForward)
        currentfwd++;
    else
        currentbck++;
}

My problem is, I want to be able to retrieve an iterator to the current Move object, without needing to query whether I am going forwards or backwards. This means one function returning one type of iterator, but I have two types.

Should I forget returning an iterator, and return a const reference to a Move object instead?

  • 1 1 Answer
  • 0 Views
  • 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-13T13:31:33+00:00Added an answer on May 13, 2026 at 1:31 pm

    This is exactly the sort of problem that prompted the design of STL to start with. There are real reasons for:

    1. Not storing iterators along with containers
    2. Using algorithms that accept arbitrary iterators
    3. Having algorithms evaluate an entire range instead of a single item at a time

    I suspect what you’re seeing right now is more or less the tip of the iceberg of the real problems. My advice would be to take a step back, and instead of asking about how to deal with the details of the design as it currently stands, ask a somewhat more general question about what you’re trying to accomplish, and how best to accomplish that end result.

    For those who care primarily about the question in the title, the answer is a heavily qualified “yes”. In particular, a reverse_iterator has a base() member to do that. The qualifications are somewhat problematic though.

    The demonstrate the problem, consider code like this:

    #include <iostream>
    #include <vector>
    #include <iterator>
    
    int main() { 
        int i[] = { 1, 2, 3, 4};
        std::vector<int> numbers(i, i+4);
    
        std::cout << *numbers.rbegin() << "\n";
        std::cout << *numbers.rbegin().base() << "\n";
        std::cout << *(numbers.rbegin()+1).base() << "\n";
    
        std::cout << *numbers.rend() << "\n";
        std::cout << *numbers.rend().base() << "\n";
        std::cout << *(numbers.rend()+1).base() << "\n";
    }
    

    Running this at this particular moment on my particular machine produces the following output:

    4
    0
    4
    -1879048016
    1
    -1879048016
    

    Summary: with rbegin() we must add one before converting to a forward iterator to get an iterator that’s valid — but with rend() we must not add one before converting to get a valid iterator.

    As long as you’re using X.rbegin() and X.rend() as the parameters to a generic algorithm, that’s fine–but experience indicates that converting to forward iterators often leads to problems.

    In the end, however, for the body of the question (as opposed to the title), the answer is pretty much as given above: the problem stems from trying to create an object that combines the collection with a couple of iterators into that collection. Fix that problem, and the whole business with forward and reverse iterators becomes moot.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 430k
  • Answers 430k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Generally it's considered good practice to not place business logic… May 15, 2026 at 1:49 pm
  • Editorial Team
    Editorial Team added an answer Its getting called in iOS 4.0 , when the Home… May 15, 2026 at 1:49 pm
  • Editorial Team
    Editorial Team added an answer This was a bug in 1.4 and has been fixed… May 15, 2026 at 1:49 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.