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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:25:53+00:00 2026-05-28T19:25:53+00:00

I’ve got some example Python code that I need to mimic in C++. I

  • 0

I’ve got some example Python code that I need to mimic in C++. I do not require any specific solution (such as co-routine based yield solutions, although they would be acceptable answers as well), I simply need to reproduce the semantics in some manner.

Python

This is a basic sequence generator, clearly too large to store a materialized version.

def pair_sequence():
    for i in range(2**32):
        for j in range(2**32):
            yield (i, j)

The goal is to maintain two instances of the sequence above, and iterate over them in semi-lockstep, but in chunks. In the example below the first_pass uses the sequence of pairs to initialize the buffer, and the second_pass regenerates the same exact sequence and processes the buffer again.

def run():
    seq1 = pair_sequence()
    seq2 = pair_sequence()

    buffer = [0] * 1000
    first_pass(seq1, buffer)
    second_pass(seq2, buffer)
    ... repeat ...

C++

The only thing I can find for a solution in C++ is to mimic yield with C++ coroutines, but I haven’t found any good reference on how to do this. I’m also interested in alternative (non general) solutions for this problem. I do not have enough memory budget to keep a copy of the sequence between passes.

  • 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-28T19:25:53+00:00Added an answer on May 28, 2026 at 7:25 pm

    Generators exist in C++, just under another name: Input Iterators. For example, reading from std::cin is similar to having a generator of char.

    You simply need to understand what a generator does:

    • there is a blob of data: the local variables define a state
    • there is an init method
    • there is a “next” method
    • there is a way to signal termination

    In your trivial example, it’s easy enough. Conceptually:

    struct State { unsigned i, j; };
    
    State make();
    
    void next(State&);
    
    bool isDone(State const&);
    

    Of course, we wrap this as a proper class:

    class PairSequence:
        // (implicit aliases)
        public std::iterator<
            std::input_iterator_tag,
            std::pair<unsigned, unsigned>
        >
    {
      // C++03
      typedef void (PairSequence::*BoolLike)();
      void non_comparable();
    public:
      // C++11 (explicit aliases)
      using iterator_category = std::input_iterator_tag;
      using value_type = std::pair<unsigned, unsigned>;
      using reference = value_type const&;
      using pointer = value_type const*;
      using difference_type = ptrdiff_t;
    
      // C++03 (explicit aliases)
      typedef std::input_iterator_tag iterator_category;
      typedef std::pair<unsigned, unsigned> value_type;
      typedef value_type const& reference;
      typedef value_type const* pointer;
      typedef ptrdiff_t difference_type;
    
      PairSequence(): done(false) {}
    
      // C++11
      explicit operator bool() const { return !done; }
    
      // C++03
      // Safe Bool idiom
      operator BoolLike() const {
        return done ? 0 : &PairSequence::non_comparable;
      }
    
      reference operator*() const { return ij; }
      pointer operator->() const { return &ij; }
    
      PairSequence& operator++() {
        static unsigned const Max = std::numeric_limts<unsigned>::max();
    
        assert(!done);
    
        if (ij.second != Max) { ++ij.second; return *this; }
        if (ij.first != Max) { ij.second = 0; ++ij.first; return *this; }
    
        done = true;
        return *this;
      }
    
      PairSequence operator++(int) {
        PairSequence const tmp(*this);
        ++*this;
        return tmp;
      }
    
    private:
      bool done;
      value_type ij;
    };
    

    So hum yeah… might be that C++ is a tad more verbose 🙂

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

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.