Howto create combinations of several vectors without hardcoding loops in C++?

前端 未结 10 970
慢半拍i
慢半拍i 2020-11-29 08:40

I have several data that looks like this:

Vector1_elements = T,C,A
Vector2_elements = C,G,A
Vector3_elements = C,G,T
..... up to ...
VectorK_elements = ...

         


        
10条回答
  •  粉色の甜心
    2020-11-29 09:13

    I too am interested in building some sort of easy to rinse and repeat combinatorial. I am familiar with the odometer driven type approach, if you will, where you've got walking indices. Something along those lines. The point is, to easily build out the tuples across an arbitrary set of unrelated vectors.

    This does not quite answer your question, I don't think, but you could build static/design time combinations using a variadic production such as the following, where T1-3 are arbitrary types:

    template
    void push_back_tupled_combos(V& v) {
      // Variadic no-args no-op
    }
    
    template
    void push_back_tupled_combos(V& v, A a, B b, C c, Args... args) {
        v.push_back({ a, b, c });
        push_back_tupled_combos(v, args...);
    }
    
    template
    void push_back_tupled_combos(V& v, Args... args) {
    }
    

    Assuming you've got a vector that looks something like this:

    typedef vector> CombosVector;
    
    CombosVector combos;
    
    push_back_tupled_combos(combos
      , 1, 2, 3
      , 4, 5, 6
      , 7, 8, 9, ...);
    

    Like I said, this is a design time consideration. It does not build tuples across a run time range of vectors. That's the down side. The up side, however, is that you gain compile time comprehension of your vectored tuples.

    Again, not quite what you, or even I, are after, but maybe it helps spark favorable feedback.

提交回复
热议问题