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 8372459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:26:15+00:00 2026-06-09T14:26:15+00:00

I was making a "concatenating iterator", i.e. an iterator that would iterate over the

  • 0

I was making a "concatenating iterator", i.e. an iterator that would iterate over the ints in an int**.

Its constructor needs:

  • An array of T**, representing the beginning of each sub-array.
  • An array of T**, representing the end of each sub-array.

Lo and behold, I ran across a situation where goto seemed to be appropriate.

But something within me screamed "NO!!" so I thought I’d come here and ask:

Should I try avoid goto situations like this? (Does it improve the readability if I do?)

#include <algorithm>

template<class T>
class lazy_concat_iterator
{
    // This code was meant to work for any valid input iterator
    // but for easier reading, I'll assume the type is: T**

    mutable T** m_endIt;              // points to an array of end-pointers
    mutable T** m_it;                 // points to an array of begin-pointers
    mutable bool m_started;   // have we started iterating?
    mutable T* m_sub;         // points somewhere in the current sub-array
    mutable T* m_subEnd;      // points to the end of the current sub-array

public:
    lazy_concat_iterator(T** begins, T** ends)
        : m_it(begins), m_endIt(ends), m_started(false) { }

    void ensure_started() const
    {
        if (!m_started)
        {
            m_started = true;
        
        INIT:
            m_sub = *m_it;
            m_subEnd = *m_endIt;

            if (m_sub == m_subEnd)  // End of this subarray?
            {
                ++m_it;
                ++m_endIt;
                goto INIT;  // try next one         <<< should I use goto here?
            }
        }
    }
};

How you could use it:

#include <vector>
#include <cstring>

using namespace std;

int main(int argc, char* argv[])
{
    vector<char*> beginnings(argv, argv + argc);

    vector<char*> endings;
    for (int i = 0; i < argc; i++)
        endings.push_back(argv[i] + strlen(argv[i]));

    lazy_concat_iterator<char> it(&beginnings[0], &endings[0]);
    it.ensure_started();  // 'it' would call this internally, when dereferenced
}
  • 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-06-09T14:26:16+00:00Added an answer on June 9, 2026 at 2:26 pm

    Yes, you can and should avoid goto, for example this code should do the equivalent for what yours does from the INIT label (this also works for input iterators which was a “hidden requirement” as it doesn’t dereference m_it and m_endIt an extra time once the condition is met unlike my previous transformation):

    while ((m_subIt = *m_it) == (m_subEnd = *m_endIt))
    {
        ++m_it;
        ++m_endIt;
    }
    

    Previous answer attempt:

    Even a forever loop would be clearer and neater than a goto. It highlights the obvious “never terminate” possibility even better.

        for (;;)
        {
            m_sub = *m_it;
            m_subEnd = *m_endIt;
    
            if (m_sub != m_subEnd)
                break;
    
            ++m_it;
            ++m_endIt;
        }
    

    Although I don’t see why you need to assign to m_subEnd and m_subIt inside the loop. If you don’t you can rewrite this as a while loop:

    while (*m_it == *m_endIt)
    {
        ++m_it;
        ++m_endIt;
    }
    
    m_subIt = *m_it;
    m_subEnd = *m_endIt;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a webservice that only needs to serve json and it needs
I'm currently making an app that needs to print out to a text file,
I am making a program that inserts a two-dimensional array with random integers and
Making an adobe flex ui in which data that is calculated must use proprietary
Making UML sequence diagram in VS 2010RC I've observed that there is no activation
Making websites that appear correctly in IE is a big problem. Is there any
I'm trying to make search terms bold in this search script that I'm making.
I am making an iPhone app that enables users to search for a city/town/countries/states
I used several applications that are providing "radar view" for listing locations on map.
I have the following PHP example code: $client = new SoapClient("http://example.com/example.wsdl"); $h = Array();

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.