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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:01:46+00:00 2026-06-03T04:01:46+00:00

Here is a simple two-container zip function in C++: template <typename A, typename B>

  • 0

Here is a simple two-container zip function in C++:

template <typename A, typename B>
std::list<std::pair<A, B> > simple_zip(const std::list<A> & lhs,
                                       const std::list<B> & rhs)
{
  std::list<std::pair<A, B> >  result;
  for (std::pair<typename std::list<A>::const_iterator,
                 typename std::list<B>::const_iterator> iter
       =
       std::pair<typename std::list<A>::const_iterator,
                 typename std::list<B>::const_iterator>(lhs.cbegin(),
                                                        rhs.cbegin());
       iter.first != lhs.end() && iter.second != rhs.end();
       ++iter.first, ++iter.second)
  {
    result.push_back( std::pair<A, B>(*iter.first, *iter.second) );
  }
  return result;
}

How would I extend this to an arbitrary number of containers with variadic templates?

I’d like general_zip to accept a tuple of lists (each list can contain a different type) and return a list of tuples.

  • 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-03T04:01:48+00:00Added an answer on June 3, 2026 at 4:01 am

    It seems like this should work

    std::list<std::tuple<>> simple_zip() {
      return {};
    }
    
    template <typename ...T>
    std::list<std::tuple<T...>> simple_zip(std::list<T>... lst)
    {
      std::list<std::tuple<T...>>  result;
      for (int i = 0, e = std::min({lst.size()...}); i != e; i++) {
        result.emplace_back(std::move(lst.front())...);
        [](...){} ((lst.pop_front(), 0)...);
      }
      return result;
    }
    

    @Potatoswatter had the good (IMO) remark that this may copy more than needed when the lists are of different size, and that using only iterators will be better since pop_front does more than actually needed. I think the following “fixes” the iterator one at the cost of more code.

    template <typename ...T>
    std::list<std::tuple<T...>> simple_zip(std::list<T>... lst)
    {
      std::list<std::tuple<T...>>  result;
      struct {
        void operator()(std::list<std::tuple<T...>> &t, int c,
                 typename std::list<T>::iterator ...it) {
          if(c == 0) return;
          t.emplace_back(std::move(*it++)...);
          (*this)(t, c-1, it...);
        }
      } zip;
      zip(result, std::min({lst.size()...}), lst.begin()...);
      return result;
    }
    
    std::list<std::tuple<>> simple_zip() {
      return {};
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a simple sorting program of an ArrayList: ArrayList<String> list = new ArrayList<String>();
Here is my .classpath file, after I have added two more external jars (org.restlet.ext.simple.jar
Pretty simple set-up here, I'm just trying to get two divs to sit side-by-side.
my sample code is here include 'simple_html_dom.php'; function get_all_links($url){ global $host; $html = new
Hi i've got here a simple program, but it's not working properly. When i
Please excuse me if I'm simple here, I want to create a simple widget
I have to be missing something simple here but it escapes me. After the
I hope I am not missing something very simple here. I have done a
I think I'm just missing something simple here but here is what I am
Here is some simple code: DIR* pd = opendir(xxxx); struct dirent *cur; while (cur

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.