How to combine two or more vectors of arbitrary types in C++











up vote
7
down vote

favorite
1












I've got the following code that combines two vectors of arbitrary types into a combinatorial, i.e. std::vector<std::tuple<A, B>>.



template<class A, class B>
std::vector<std::tuple<A, B>> combine(const std::vector<A>& a, const std::vector<B>& b) {

const auto combine_parts_ = (const A& x, const B& y) {
auto result = std::tuple_cat(std::make_tuple(x), std::make_tuple(y));
return result;
};

std::vector<std::tuple<A, B>> results;

for (const auto& x : a) {
for (const auto& y : b) {
results.push_back(combine_parts_(x, y));
}
}

return results;
}


However, I'm unclear how to extend that to an arbitrary number of types/vectors. I don't care about duplicate types; in fact there may be two or more sets of the same type involved. That's okay.



Some example use cases, for instance:



const auto combinations = combine(
std::vector<int>({1,2,3})
, std::vector<int>({1,2,3})
);
const auto combinations2 = combine(
std::vector<int>({1,2,3})
, std::vector<int>({1,2,3})
, std::vector<bool>({true,false})
);
const auto combinations3 = combine(
std::vector<int>({1,2,3})
, std::vector<int>({1,2,3})
, std::vector<bool>({true,false})
, std::vector<char>({'a','b','c','d','e'})
);


Chiefly, what I want to do is avoid the ugly nested for loop. At the same time, I want to combine some unit testing combinatorial use cases in order to work with the resulting std::tuple<...> as the test case.



Note, I am not talking about permutations of a homogeneous set here. That's been a point of confusion from prior questions.



I think it might have something to do with templates, variadics, std::tuple_cat, somewhere along the way, but I don't know.



Thoughts? Suggestions?










share|improve this question




























    up vote
    7
    down vote

    favorite
    1












    I've got the following code that combines two vectors of arbitrary types into a combinatorial, i.e. std::vector<std::tuple<A, B>>.



    template<class A, class B>
    std::vector<std::tuple<A, B>> combine(const std::vector<A>& a, const std::vector<B>& b) {

    const auto combine_parts_ = (const A& x, const B& y) {
    auto result = std::tuple_cat(std::make_tuple(x), std::make_tuple(y));
    return result;
    };

    std::vector<std::tuple<A, B>> results;

    for (const auto& x : a) {
    for (const auto& y : b) {
    results.push_back(combine_parts_(x, y));
    }
    }

    return results;
    }


    However, I'm unclear how to extend that to an arbitrary number of types/vectors. I don't care about duplicate types; in fact there may be two or more sets of the same type involved. That's okay.



    Some example use cases, for instance:



    const auto combinations = combine(
    std::vector<int>({1,2,3})
    , std::vector<int>({1,2,3})
    );
    const auto combinations2 = combine(
    std::vector<int>({1,2,3})
    , std::vector<int>({1,2,3})
    , std::vector<bool>({true,false})
    );
    const auto combinations3 = combine(
    std::vector<int>({1,2,3})
    , std::vector<int>({1,2,3})
    , std::vector<bool>({true,false})
    , std::vector<char>({'a','b','c','d','e'})
    );


    Chiefly, what I want to do is avoid the ugly nested for loop. At the same time, I want to combine some unit testing combinatorial use cases in order to work with the resulting std::tuple<...> as the test case.



    Note, I am not talking about permutations of a homogeneous set here. That's been a point of confusion from prior questions.



    I think it might have something to do with templates, variadics, std::tuple_cat, somewhere along the way, but I don't know.



    Thoughts? Suggestions?










    share|improve this question


























      up vote
      7
      down vote

      favorite
      1









      up vote
      7
      down vote

      favorite
      1






      1





      I've got the following code that combines two vectors of arbitrary types into a combinatorial, i.e. std::vector<std::tuple<A, B>>.



      template<class A, class B>
      std::vector<std::tuple<A, B>> combine(const std::vector<A>& a, const std::vector<B>& b) {

      const auto combine_parts_ = (const A& x, const B& y) {
      auto result = std::tuple_cat(std::make_tuple(x), std::make_tuple(y));
      return result;
      };

      std::vector<std::tuple<A, B>> results;

      for (const auto& x : a) {
      for (const auto& y : b) {
      results.push_back(combine_parts_(x, y));
      }
      }

      return results;
      }


      However, I'm unclear how to extend that to an arbitrary number of types/vectors. I don't care about duplicate types; in fact there may be two or more sets of the same type involved. That's okay.



      Some example use cases, for instance:



      const auto combinations = combine(
      std::vector<int>({1,2,3})
      , std::vector<int>({1,2,3})
      );
      const auto combinations2 = combine(
      std::vector<int>({1,2,3})
      , std::vector<int>({1,2,3})
      , std::vector<bool>({true,false})
      );
      const auto combinations3 = combine(
      std::vector<int>({1,2,3})
      , std::vector<int>({1,2,3})
      , std::vector<bool>({true,false})
      , std::vector<char>({'a','b','c','d','e'})
      );


      Chiefly, what I want to do is avoid the ugly nested for loop. At the same time, I want to combine some unit testing combinatorial use cases in order to work with the resulting std::tuple<...> as the test case.



      Note, I am not talking about permutations of a homogeneous set here. That's been a point of confusion from prior questions.



      I think it might have something to do with templates, variadics, std::tuple_cat, somewhere along the way, but I don't know.



      Thoughts? Suggestions?










      share|improve this question















      I've got the following code that combines two vectors of arbitrary types into a combinatorial, i.e. std::vector<std::tuple<A, B>>.



      template<class A, class B>
      std::vector<std::tuple<A, B>> combine(const std::vector<A>& a, const std::vector<B>& b) {

      const auto combine_parts_ = (const A& x, const B& y) {
      auto result = std::tuple_cat(std::make_tuple(x), std::make_tuple(y));
      return result;
      };

      std::vector<std::tuple<A, B>> results;

      for (const auto& x : a) {
      for (const auto& y : b) {
      results.push_back(combine_parts_(x, y));
      }
      }

      return results;
      }


      However, I'm unclear how to extend that to an arbitrary number of types/vectors. I don't care about duplicate types; in fact there may be two or more sets of the same type involved. That's okay.



      Some example use cases, for instance:



      const auto combinations = combine(
      std::vector<int>({1,2,3})
      , std::vector<int>({1,2,3})
      );
      const auto combinations2 = combine(
      std::vector<int>({1,2,3})
      , std::vector<int>({1,2,3})
      , std::vector<bool>({true,false})
      );
      const auto combinations3 = combine(
      std::vector<int>({1,2,3})
      , std::vector<int>({1,2,3})
      , std::vector<bool>({true,false})
      , std::vector<char>({'a','b','c','d','e'})
      );


      Chiefly, what I want to do is avoid the ugly nested for loop. At the same time, I want to combine some unit testing combinatorial use cases in order to work with the resulting std::tuple<...> as the test case.



      Note, I am not talking about permutations of a homogeneous set here. That's been a point of confusion from prior questions.



      I think it might have something to do with templates, variadics, std::tuple_cat, somewhere along the way, but I don't know.



      Thoughts? Suggestions?







      c++ vector tuples cartesian-product






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 22:40









      Jarod42

      112k1299179




      112k1299179










      asked Nov 8 at 22:12









      mwpowellhtx

      1038




      1038
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          If you want to compute the Cartesian product of heterogeneous vectors, you may do something like:



          template <std::size_t N>
          bool increase(const std::array<std::size_t, N>& sizes, std::array<std::size_t, N>& it)
          {
          for (std::size_t i = 0; i != N; ++i) {
          const std::size_t index = N - 1 - i;
          ++it[index];
          if (it[index] >= sizes[index]) {
          it[index] = 0;
          } else {
          return true;
          }
          }
          return false;
          }

          template <typename F, std::size_t ... Is, std::size_t N, typename Tuple>
          void apply_impl(F&& f,
          std::index_sequence<Is...>,
          const std::array<std::size_t, N>& it,
          const Tuple& tuple)
          {
          f(std::get<Is>(tuple)[it[Is]]...);
          }

          template <typename F, typename ... Ts>
          void cartesian_product_apply(F&& f, const std::vector<Ts>&... vs)
          {
          constexpr std::size_t N = sizeof...(Ts);
          std::array<std::size_t, N> sizes{{vs.size()...}};
          std::array<std::size_t, N> it{};

          do {
          apply_impl(f, std::index_sequence_for<Ts...>(), it, std::tie(vs...));
          } while (increase(sizes, it));
          }


          And finally:



          template <typename ... Ts>
          std::vector<std::tuple<Ts...>> cartesian_product(const std::vector<Ts>&... vs)
          {
          std::vector<std::tuple<Ts...>> res;

          cartesian_product_apply([&res](const auto&... args) { res.emplace_back(args...); },
          vs...);
          return res;
          }


          With usage similar to:



          std::vector<int> v1 = {1, 2, 3};
          std::vector<std::string> v2 = {" A "," B "};
          std::vector<int> v3 = {4, 5};

          const auto res = cartesian_product(v1, v2, v3);
          for (const auto& t : res) {
          // ...
          }


          Demo






          share|improve this answer























          • It's interesting, but for me it seems MSVC (2017) std::array support is a bit lacking. Still, I think the fundamentals are there.
            – mwpowellhtx
            Nov 8 at 23:58










          • Compile fine here with Msvc 2017 (Version 15.8.7)
            – Jarod42
            Nov 9 at 0:26


















          up vote
          0
          down vote













          Slightly adapted, and with warnings disabled:



          struct CartesianProduct {

          template<typename... Tys>
          std::vector<std::tuple<Tys...>> operator()(const std::vector<Tys>&... vectors) {
          std::vector<std::tuple<Tys...>> results;
          apply([&results](const auto&... args) {
          results.emplace_back(args...); }, vectors...);
          return results;
          }

          private:

          template<std::size_t N>
          bool __increase(const std::array<std::size_t, N>& sizes, std::array<std::size_t, N>& it) {
          for (std::size_t i = 0; i < N; ++i) {
          const std::size_t index = N - 1 - i;
          ++it[index];
          if (it[index] >= sizes[index]) {
          it[index] = 0;
          }
          else {
          return true;
          }
          }
          return false;
          }

          template<typename Cb, std::size_t... Is, std::size_t N, typename Tup>
          void __apply_impl(Cb&& cb, std::index_sequence<Is...>
          , const std::array<std::size_t, N>& it, const Tup& tup) {
          cb(std::get<Is>(tup)[it[Is]]...);
          }

          template <typename Cb, typename... Tys>
          void apply(Cb&& cb, const std::vector<Tys>&... vectors) {
          constexpr std::size_t N = sizeof...(Tys);
          std::array<std::size_t, N> sizes{ {vectors.size()...} };
          #pragma warning (disable: 4834)
          // TODO: TBD: warning C4834: discarding return value of function with 'nodiscard' attribute...
          std::array<std::size_t, N> it{ {(vectors.size(), 0u)...} };
          #pragma warning (default: 4834)

          do {
          __apply_impl(cb, std::index_sequence_for<Tys...>(), it, std::tie(vectors...));
          } while (__increase(sizes, it));
          }
          };


          Remembered to include tuple, vector, as well as array.



          Simple to use:



          CartesianProduct prod;
          // ...
          const auto product_ = prod(ints_, doubles_, bools_);
          CHECK(std::tuple_size<decltype(product_ )::value_type>::value == 3);


          Although not entirely positive what the warning is all about.






          share|improve this answer





















          • warning is for discarded value vs.size() (but used for variadic expansion). Once I think bout it, std::array<std::size_t, N> it{}; should be enough.
            – Jarod42
            Nov 9 at 8:36













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53216947%2fhow-to-combine-two-or-more-vectors-of-arbitrary-types-in-c%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote



          accepted










          If you want to compute the Cartesian product of heterogeneous vectors, you may do something like:



          template <std::size_t N>
          bool increase(const std::array<std::size_t, N>& sizes, std::array<std::size_t, N>& it)
          {
          for (std::size_t i = 0; i != N; ++i) {
          const std::size_t index = N - 1 - i;
          ++it[index];
          if (it[index] >= sizes[index]) {
          it[index] = 0;
          } else {
          return true;
          }
          }
          return false;
          }

          template <typename F, std::size_t ... Is, std::size_t N, typename Tuple>
          void apply_impl(F&& f,
          std::index_sequence<Is...>,
          const std::array<std::size_t, N>& it,
          const Tuple& tuple)
          {
          f(std::get<Is>(tuple)[it[Is]]...);
          }

          template <typename F, typename ... Ts>
          void cartesian_product_apply(F&& f, const std::vector<Ts>&... vs)
          {
          constexpr std::size_t N = sizeof...(Ts);
          std::array<std::size_t, N> sizes{{vs.size()...}};
          std::array<std::size_t, N> it{};

          do {
          apply_impl(f, std::index_sequence_for<Ts...>(), it, std::tie(vs...));
          } while (increase(sizes, it));
          }


          And finally:



          template <typename ... Ts>
          std::vector<std::tuple<Ts...>> cartesian_product(const std::vector<Ts>&... vs)
          {
          std::vector<std::tuple<Ts...>> res;

          cartesian_product_apply([&res](const auto&... args) { res.emplace_back(args...); },
          vs...);
          return res;
          }


          With usage similar to:



          std::vector<int> v1 = {1, 2, 3};
          std::vector<std::string> v2 = {" A "," B "};
          std::vector<int> v3 = {4, 5};

          const auto res = cartesian_product(v1, v2, v3);
          for (const auto& t : res) {
          // ...
          }


          Demo






          share|improve this answer























          • It's interesting, but for me it seems MSVC (2017) std::array support is a bit lacking. Still, I think the fundamentals are there.
            – mwpowellhtx
            Nov 8 at 23:58










          • Compile fine here with Msvc 2017 (Version 15.8.7)
            – Jarod42
            Nov 9 at 0:26















          up vote
          4
          down vote



          accepted










          If you want to compute the Cartesian product of heterogeneous vectors, you may do something like:



          template <std::size_t N>
          bool increase(const std::array<std::size_t, N>& sizes, std::array<std::size_t, N>& it)
          {
          for (std::size_t i = 0; i != N; ++i) {
          const std::size_t index = N - 1 - i;
          ++it[index];
          if (it[index] >= sizes[index]) {
          it[index] = 0;
          } else {
          return true;
          }
          }
          return false;
          }

          template <typename F, std::size_t ... Is, std::size_t N, typename Tuple>
          void apply_impl(F&& f,
          std::index_sequence<Is...>,
          const std::array<std::size_t, N>& it,
          const Tuple& tuple)
          {
          f(std::get<Is>(tuple)[it[Is]]...);
          }

          template <typename F, typename ... Ts>
          void cartesian_product_apply(F&& f, const std::vector<Ts>&... vs)
          {
          constexpr std::size_t N = sizeof...(Ts);
          std::array<std::size_t, N> sizes{{vs.size()...}};
          std::array<std::size_t, N> it{};

          do {
          apply_impl(f, std::index_sequence_for<Ts...>(), it, std::tie(vs...));
          } while (increase(sizes, it));
          }


          And finally:



          template <typename ... Ts>
          std::vector<std::tuple<Ts...>> cartesian_product(const std::vector<Ts>&... vs)
          {
          std::vector<std::tuple<Ts...>> res;

          cartesian_product_apply([&res](const auto&... args) { res.emplace_back(args...); },
          vs...);
          return res;
          }


          With usage similar to:



          std::vector<int> v1 = {1, 2, 3};
          std::vector<std::string> v2 = {" A "," B "};
          std::vector<int> v3 = {4, 5};

          const auto res = cartesian_product(v1, v2, v3);
          for (const auto& t : res) {
          // ...
          }


          Demo






          share|improve this answer























          • It's interesting, but for me it seems MSVC (2017) std::array support is a bit lacking. Still, I think the fundamentals are there.
            – mwpowellhtx
            Nov 8 at 23:58










          • Compile fine here with Msvc 2017 (Version 15.8.7)
            – Jarod42
            Nov 9 at 0:26













          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          If you want to compute the Cartesian product of heterogeneous vectors, you may do something like:



          template <std::size_t N>
          bool increase(const std::array<std::size_t, N>& sizes, std::array<std::size_t, N>& it)
          {
          for (std::size_t i = 0; i != N; ++i) {
          const std::size_t index = N - 1 - i;
          ++it[index];
          if (it[index] >= sizes[index]) {
          it[index] = 0;
          } else {
          return true;
          }
          }
          return false;
          }

          template <typename F, std::size_t ... Is, std::size_t N, typename Tuple>
          void apply_impl(F&& f,
          std::index_sequence<Is...>,
          const std::array<std::size_t, N>& it,
          const Tuple& tuple)
          {
          f(std::get<Is>(tuple)[it[Is]]...);
          }

          template <typename F, typename ... Ts>
          void cartesian_product_apply(F&& f, const std::vector<Ts>&... vs)
          {
          constexpr std::size_t N = sizeof...(Ts);
          std::array<std::size_t, N> sizes{{vs.size()...}};
          std::array<std::size_t, N> it{};

          do {
          apply_impl(f, std::index_sequence_for<Ts...>(), it, std::tie(vs...));
          } while (increase(sizes, it));
          }


          And finally:



          template <typename ... Ts>
          std::vector<std::tuple<Ts...>> cartesian_product(const std::vector<Ts>&... vs)
          {
          std::vector<std::tuple<Ts...>> res;

          cartesian_product_apply([&res](const auto&... args) { res.emplace_back(args...); },
          vs...);
          return res;
          }


          With usage similar to:



          std::vector<int> v1 = {1, 2, 3};
          std::vector<std::string> v2 = {" A "," B "};
          std::vector<int> v3 = {4, 5};

          const auto res = cartesian_product(v1, v2, v3);
          for (const auto& t : res) {
          // ...
          }


          Demo






          share|improve this answer














          If you want to compute the Cartesian product of heterogeneous vectors, you may do something like:



          template <std::size_t N>
          bool increase(const std::array<std::size_t, N>& sizes, std::array<std::size_t, N>& it)
          {
          for (std::size_t i = 0; i != N; ++i) {
          const std::size_t index = N - 1 - i;
          ++it[index];
          if (it[index] >= sizes[index]) {
          it[index] = 0;
          } else {
          return true;
          }
          }
          return false;
          }

          template <typename F, std::size_t ... Is, std::size_t N, typename Tuple>
          void apply_impl(F&& f,
          std::index_sequence<Is...>,
          const std::array<std::size_t, N>& it,
          const Tuple& tuple)
          {
          f(std::get<Is>(tuple)[it[Is]]...);
          }

          template <typename F, typename ... Ts>
          void cartesian_product_apply(F&& f, const std::vector<Ts>&... vs)
          {
          constexpr std::size_t N = sizeof...(Ts);
          std::array<std::size_t, N> sizes{{vs.size()...}};
          std::array<std::size_t, N> it{};

          do {
          apply_impl(f, std::index_sequence_for<Ts...>(), it, std::tie(vs...));
          } while (increase(sizes, it));
          }


          And finally:



          template <typename ... Ts>
          std::vector<std::tuple<Ts...>> cartesian_product(const std::vector<Ts>&... vs)
          {
          std::vector<std::tuple<Ts...>> res;

          cartesian_product_apply([&res](const auto&... args) { res.emplace_back(args...); },
          vs...);
          return res;
          }


          With usage similar to:



          std::vector<int> v1 = {1, 2, 3};
          std::vector<std::string> v2 = {" A "," B "};
          std::vector<int> v3 = {4, 5};

          const auto res = cartesian_product(v1, v2, v3);
          for (const auto& t : res) {
          // ...
          }


          Demo







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 9 at 8:51

























          answered Nov 8 at 22:47









          Jarod42

          112k1299179




          112k1299179












          • It's interesting, but for me it seems MSVC (2017) std::array support is a bit lacking. Still, I think the fundamentals are there.
            – mwpowellhtx
            Nov 8 at 23:58










          • Compile fine here with Msvc 2017 (Version 15.8.7)
            – Jarod42
            Nov 9 at 0:26


















          • It's interesting, but for me it seems MSVC (2017) std::array support is a bit lacking. Still, I think the fundamentals are there.
            – mwpowellhtx
            Nov 8 at 23:58










          • Compile fine here with Msvc 2017 (Version 15.8.7)
            – Jarod42
            Nov 9 at 0:26
















          It's interesting, but for me it seems MSVC (2017) std::array support is a bit lacking. Still, I think the fundamentals are there.
          – mwpowellhtx
          Nov 8 at 23:58




          It's interesting, but for me it seems MSVC (2017) std::array support is a bit lacking. Still, I think the fundamentals are there.
          – mwpowellhtx
          Nov 8 at 23:58












          Compile fine here with Msvc 2017 (Version 15.8.7)
          – Jarod42
          Nov 9 at 0:26




          Compile fine here with Msvc 2017 (Version 15.8.7)
          – Jarod42
          Nov 9 at 0:26












          up vote
          0
          down vote













          Slightly adapted, and with warnings disabled:



          struct CartesianProduct {

          template<typename... Tys>
          std::vector<std::tuple<Tys...>> operator()(const std::vector<Tys>&... vectors) {
          std::vector<std::tuple<Tys...>> results;
          apply([&results](const auto&... args) {
          results.emplace_back(args...); }, vectors...);
          return results;
          }

          private:

          template<std::size_t N>
          bool __increase(const std::array<std::size_t, N>& sizes, std::array<std::size_t, N>& it) {
          for (std::size_t i = 0; i < N; ++i) {
          const std::size_t index = N - 1 - i;
          ++it[index];
          if (it[index] >= sizes[index]) {
          it[index] = 0;
          }
          else {
          return true;
          }
          }
          return false;
          }

          template<typename Cb, std::size_t... Is, std::size_t N, typename Tup>
          void __apply_impl(Cb&& cb, std::index_sequence<Is...>
          , const std::array<std::size_t, N>& it, const Tup& tup) {
          cb(std::get<Is>(tup)[it[Is]]...);
          }

          template <typename Cb, typename... Tys>
          void apply(Cb&& cb, const std::vector<Tys>&... vectors) {
          constexpr std::size_t N = sizeof...(Tys);
          std::array<std::size_t, N> sizes{ {vectors.size()...} };
          #pragma warning (disable: 4834)
          // TODO: TBD: warning C4834: discarding return value of function with 'nodiscard' attribute...
          std::array<std::size_t, N> it{ {(vectors.size(), 0u)...} };
          #pragma warning (default: 4834)

          do {
          __apply_impl(cb, std::index_sequence_for<Tys...>(), it, std::tie(vectors...));
          } while (__increase(sizes, it));
          }
          };


          Remembered to include tuple, vector, as well as array.



          Simple to use:



          CartesianProduct prod;
          // ...
          const auto product_ = prod(ints_, doubles_, bools_);
          CHECK(std::tuple_size<decltype(product_ )::value_type>::value == 3);


          Although not entirely positive what the warning is all about.






          share|improve this answer





















          • warning is for discarded value vs.size() (but used for variadic expansion). Once I think bout it, std::array<std::size_t, N> it{}; should be enough.
            – Jarod42
            Nov 9 at 8:36

















          up vote
          0
          down vote













          Slightly adapted, and with warnings disabled:



          struct CartesianProduct {

          template<typename... Tys>
          std::vector<std::tuple<Tys...>> operator()(const std::vector<Tys>&... vectors) {
          std::vector<std::tuple<Tys...>> results;
          apply([&results](const auto&... args) {
          results.emplace_back(args...); }, vectors...);
          return results;
          }

          private:

          template<std::size_t N>
          bool __increase(const std::array<std::size_t, N>& sizes, std::array<std::size_t, N>& it) {
          for (std::size_t i = 0; i < N; ++i) {
          const std::size_t index = N - 1 - i;
          ++it[index];
          if (it[index] >= sizes[index]) {
          it[index] = 0;
          }
          else {
          return true;
          }
          }
          return false;
          }

          template<typename Cb, std::size_t... Is, std::size_t N, typename Tup>
          void __apply_impl(Cb&& cb, std::index_sequence<Is...>
          , const std::array<std::size_t, N>& it, const Tup& tup) {
          cb(std::get<Is>(tup)[it[Is]]...);
          }

          template <typename Cb, typename... Tys>
          void apply(Cb&& cb, const std::vector<Tys>&... vectors) {
          constexpr std::size_t N = sizeof...(Tys);
          std::array<std::size_t, N> sizes{ {vectors.size()...} };
          #pragma warning (disable: 4834)
          // TODO: TBD: warning C4834: discarding return value of function with 'nodiscard' attribute...
          std::array<std::size_t, N> it{ {(vectors.size(), 0u)...} };
          #pragma warning (default: 4834)

          do {
          __apply_impl(cb, std::index_sequence_for<Tys...>(), it, std::tie(vectors...));
          } while (__increase(sizes, it));
          }
          };


          Remembered to include tuple, vector, as well as array.



          Simple to use:



          CartesianProduct prod;
          // ...
          const auto product_ = prod(ints_, doubles_, bools_);
          CHECK(std::tuple_size<decltype(product_ )::value_type>::value == 3);


          Although not entirely positive what the warning is all about.






          share|improve this answer





















          • warning is for discarded value vs.size() (but used for variadic expansion). Once I think bout it, std::array<std::size_t, N> it{}; should be enough.
            – Jarod42
            Nov 9 at 8:36















          up vote
          0
          down vote










          up vote
          0
          down vote









          Slightly adapted, and with warnings disabled:



          struct CartesianProduct {

          template<typename... Tys>
          std::vector<std::tuple<Tys...>> operator()(const std::vector<Tys>&... vectors) {
          std::vector<std::tuple<Tys...>> results;
          apply([&results](const auto&... args) {
          results.emplace_back(args...); }, vectors...);
          return results;
          }

          private:

          template<std::size_t N>
          bool __increase(const std::array<std::size_t, N>& sizes, std::array<std::size_t, N>& it) {
          for (std::size_t i = 0; i < N; ++i) {
          const std::size_t index = N - 1 - i;
          ++it[index];
          if (it[index] >= sizes[index]) {
          it[index] = 0;
          }
          else {
          return true;
          }
          }
          return false;
          }

          template<typename Cb, std::size_t... Is, std::size_t N, typename Tup>
          void __apply_impl(Cb&& cb, std::index_sequence<Is...>
          , const std::array<std::size_t, N>& it, const Tup& tup) {
          cb(std::get<Is>(tup)[it[Is]]...);
          }

          template <typename Cb, typename... Tys>
          void apply(Cb&& cb, const std::vector<Tys>&... vectors) {
          constexpr std::size_t N = sizeof...(Tys);
          std::array<std::size_t, N> sizes{ {vectors.size()...} };
          #pragma warning (disable: 4834)
          // TODO: TBD: warning C4834: discarding return value of function with 'nodiscard' attribute...
          std::array<std::size_t, N> it{ {(vectors.size(), 0u)...} };
          #pragma warning (default: 4834)

          do {
          __apply_impl(cb, std::index_sequence_for<Tys...>(), it, std::tie(vectors...));
          } while (__increase(sizes, it));
          }
          };


          Remembered to include tuple, vector, as well as array.



          Simple to use:



          CartesianProduct prod;
          // ...
          const auto product_ = prod(ints_, doubles_, bools_);
          CHECK(std::tuple_size<decltype(product_ )::value_type>::value == 3);


          Although not entirely positive what the warning is all about.






          share|improve this answer












          Slightly adapted, and with warnings disabled:



          struct CartesianProduct {

          template<typename... Tys>
          std::vector<std::tuple<Tys...>> operator()(const std::vector<Tys>&... vectors) {
          std::vector<std::tuple<Tys...>> results;
          apply([&results](const auto&... args) {
          results.emplace_back(args...); }, vectors...);
          return results;
          }

          private:

          template<std::size_t N>
          bool __increase(const std::array<std::size_t, N>& sizes, std::array<std::size_t, N>& it) {
          for (std::size_t i = 0; i < N; ++i) {
          const std::size_t index = N - 1 - i;
          ++it[index];
          if (it[index] >= sizes[index]) {
          it[index] = 0;
          }
          else {
          return true;
          }
          }
          return false;
          }

          template<typename Cb, std::size_t... Is, std::size_t N, typename Tup>
          void __apply_impl(Cb&& cb, std::index_sequence<Is...>
          , const std::array<std::size_t, N>& it, const Tup& tup) {
          cb(std::get<Is>(tup)[it[Is]]...);
          }

          template <typename Cb, typename... Tys>
          void apply(Cb&& cb, const std::vector<Tys>&... vectors) {
          constexpr std::size_t N = sizeof...(Tys);
          std::array<std::size_t, N> sizes{ {vectors.size()...} };
          #pragma warning (disable: 4834)
          // TODO: TBD: warning C4834: discarding return value of function with 'nodiscard' attribute...
          std::array<std::size_t, N> it{ {(vectors.size(), 0u)...} };
          #pragma warning (default: 4834)

          do {
          __apply_impl(cb, std::index_sequence_for<Tys...>(), it, std::tie(vectors...));
          } while (__increase(sizes, it));
          }
          };


          Remembered to include tuple, vector, as well as array.



          Simple to use:



          CartesianProduct prod;
          // ...
          const auto product_ = prod(ints_, doubles_, bools_);
          CHECK(std::tuple_size<decltype(product_ )::value_type>::value == 3);


          Although not entirely positive what the warning is all about.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 9 at 0:55









          mwpowellhtx

          1038




          1038












          • warning is for discarded value vs.size() (but used for variadic expansion). Once I think bout it, std::array<std::size_t, N> it{}; should be enough.
            – Jarod42
            Nov 9 at 8:36




















          • warning is for discarded value vs.size() (but used for variadic expansion). Once I think bout it, std::array<std::size_t, N> it{}; should be enough.
            – Jarod42
            Nov 9 at 8:36


















          warning is for discarded value vs.size() (but used for variadic expansion). Once I think bout it, std::array<std::size_t, N> it{}; should be enough.
          – Jarod42
          Nov 9 at 8:36






          warning is for discarded value vs.size() (but used for variadic expansion). Once I think bout it, std::array<std::size_t, N> it{}; should be enough.
          – Jarod42
          Nov 9 at 8:36




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53216947%2fhow-to-combine-two-or-more-vectors-of-arbitrary-types-in-c%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Hercules Kyvelos

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud