Thread: make_tuple() from vector elements & storing varied tuple_size<T> tuples

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    203

    make_tuple() from vector elements & storing varied tuple_size<T> tuples

    Larger number: N, smaller number: K
    Task:
    Code:
    for (k = 1; k <= K; k++)
    to choose k from N and save the results of all such possible combinations for each k in suitable container(s) that can be acccessed (directly or through pointers)

    What I have so far: code (thanks Google) to generate each of these combinations for 1 through K and, I think, a way of capturing each particular combination for each k in a vector
    Outstandings, etc: (a) how to do a make_tuple() with the elements of this vector directly (assuming tuple is the best container to save each particular result?) (b) how do I store and access these tuples (via pointers?) as the type of the tuple changes with k or do I need K containers (c) or maybe there is some other way of approaching this problem that I have overlooked?

    Code that will work and output to console: #10958326 - Pastie

    Code that will not produce any visible output because it has been edited and commented to show outstandings (a) and (b) from above:

    Code:
    #include <iostream>
    #include <algorithm>
    #include <string>
    #include<vector>
    #include<ctime>
    
    using namespace std;
    
    int main()
    {
        cout<<"Choose N: ";
        int N;
        cin>> N;
        cout<<"Choose K: ";
        int K;
        cin>>K;
    
    
        clock_t tStart = clock();
        for (int j = 1; j <= K; j++)
        {
            string bitmask(j, 1);
            bitmask.resize(N, 0);
    
    
            do
            {
                vector<int> v{};
                for (int i = 0; i < N; ++i)
                {
                    if (bitmask[i]) v.emplace_back(i+1);
                }
                //make_tuple(v.begin(),v.end()) // doesn't work;
                //place tuple_size<j> in a container;
                //    cout << "\n";
            }   while (prev_permutation(bitmask.begin(), bitmask.end()));
                //tuple_size<j> increases by 1 from next j loop ;
        }
        cout<<"Time taken: "<<(clock() - tStart)/CLOCKS_PER_SEC<<" secs\n";
    }
    Thanks, as always, for any help, suggestions
    Last edited by sean_cantab; 11-08-2016 at 11:32 AM.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Code:
    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <vector>
    #include <chrono>
    using namespace std;
    
    int main() {
        int N, K;
        cout << "Choose N: ";
        cin >> N;
        cout << "Choose K: ";
        cin >> K;
    
        vector<vector<int>*> w;
    
        auto tStart = chrono::high_resolution_clock::now();
    
        for (int i = 1; i <= K; i++) {
            string bits(i, 1);
            bits.resize(N, 0);
            do {
                w.push_back(new vector<int>());
                for (int j = 0; j < N; ++j)
                    if (bits[j])
                        w[w.size()-1]->push_back(j + 1);
            } while (prev_permutation(bits.begin(), bits.end()));
        }
    
        auto tEnd = chrono::high_resolution_clock::now();
    
        for (const auto& combo: w) {
            for (int num: *combo)
                cout << num << ' ';
            cout << '\n';
        }
    
        cout << chrono::duration_cast<chrono::milliseconds>
                (tEnd - tStart).count() << " ms\n";
    
        for (auto& combo: w)
            delete combo;
    }

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    Wow! and not a tuple in sight! Many thanks

    I also managed to figure out make_tuple with vector elements directly and then access the elements of the tuple:

    Code:
    #include <iostream>
    #include<vector>
    #include<tuple>
    
    using namespace std;
    
    int main()
    {
        vector<int> v {1, 2, 3, 4};
        auto myTuple = make_tuple(v.begin(), v.end());
        for (size_t i = 0; i < v.size(); i++)
        {
            cout<<*(get<0>(myTuple)+i)<<"\t";
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing user inputs into array and splitting into elements.
    By Lina_inverse in forum C Programming
    Replies: 4
    Last Post: 10-05-2012, 11:39 PM
  2. Storing objects in Vector.
    By darren78 in forum C++ Programming
    Replies: 8
    Last Post: 06-17-2010, 12:18 AM
  3. Storing a vector inside a vector
    By csonx_p in forum C++ Programming
    Replies: 1
    Last Post: 09-14-2008, 05:15 AM
  4. vector of 2d array elements?
    By jmartrican in forum C++ Programming
    Replies: 6
    Last Post: 10-02-2007, 09:05 PM
  5. Vector elements
    By strickey in forum C++ Programming
    Replies: 5
    Last Post: 02-04-2005, 10:30 AM

Tags for this Thread