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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:32:32+00:00 2026-06-17T21:32:32+00:00

Imagine we have a vector with only unique values and would like to generate

  • 0

Imagine we have a vector with only unique values and would like to generate all pairs. The algorithm to do so is:

vector< int > data;
// ... pushback some data

vector< vector< int > > pairs;

for( size_t i = 0; i < data.size() - 1; ++i )
{
    for( size_t j = i + 1; j < data.size(); ++j )
    {
        vector< int > pair; 
        pair.push_back( data[i] ); 
        pair.push_back( data[j] );

        pairs.push_back( pair );
    }
}

Now, for triples the algorithm changes to:

vector< int > data;
// ... pushback some data

vector< vector< int > > triples;

for( size_t i = 0; i < data.size() - 2; ++i )
{
    for( size_t j = i + 1; j < data.size() - 1; ++j )
    {
        for( size_t k = j + 1; k < data.size(); ++k )
        {
            vector< int > triple; 
            triple.push_back( data[i] ); 
            triple.push_back( data[j] );
            triple.push_back( data[k] );

            triples.push_back( triple );
        }
    }
}

It’s fairly easy to come up with the code for quadruples and other tuple types.

Could someone show me how to achieve a general algorithm to generate all sort of tuples? And since we are at it how can I calculate the number of tuples given the number of elements in a vector? For pairs the formula is n(n-1)/2.

Thanks!

  • 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-17T21:32:33+00:00Added an answer on June 17, 2026 at 9:32 pm

    What you are describing is called k-combinations, which is a very important concept in the area of combinatorics. The number of unique combinations of k elements from a set of n elements is expressed by the formula n! /(k!(n-k)!).

    To solve this efficiently in a generic way, you should probably look into std::tuple and variadic templates (a C++11 feature). If you do not know the dimensionality at compile-time, however, you’ll have to create a recursive function which takes two arguments: a list of all items and k, the number items to choose from the list.

    Then, for each element e in the set, create a list containing that element and the result of calling the function recursively with k-1 as number to choose and supplying only the elements of the list which come after e in the list. This will prevent creating the same subset in multiple sub-trees of the recursion.

    Hope that’s clear enough for you. 🙂 If you have any more questions, feel free to ask for more explanations.

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

Sidebar

Related Questions

I have std::vector<int> around , in which I need to update(/overwrite) N values starting
Imagine a set of data with given x-values (as a column vector) and several
Imagine I have a class used to represent some trivial numerical data, like a
Imagine you are simulating particle physics. You, then, have a position vector for each
Imagine you have two views with code like the following: controller_a/a.html.erb <%= content_tag(:div) do
Imagine I have a boolean and a string attribute on my model with data
Imagine you have a file a.h #include <iostream> template<typename T> struct A{ int magic;
I have a std::vector of values for which I know the maximum size, but
Imagine that we have two static libraries built with different implementations of std::vector .
Imagine I have XML data: <tag> value</tag> etc .... <othertag> value value2 value_n </othertag>

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.