• <small id='z9JBT'></small><noframes id='z9JBT'>

      <bdo id='z9JBT'></bdo><ul id='z9JBT'></ul>

    1. <i id='z9JBT'><tr id='z9JBT'><dt id='z9JBT'><q id='z9JBT'><span id='z9JBT'><b id='z9JBT'><form id='z9JBT'><ins id='z9JBT'></ins><ul id='z9JBT'></ul><sub id='z9JBT'></sub></form><legend id='z9JBT'></legend><bdo id='z9JBT'><pre id='z9JBT'><center id='z9JBT'></center></pre></bdo></b><th id='z9JBT'></th></span></q></dt></tr></i><div id='z9JBT'><tfoot id='z9JBT'></tfoot><dl id='z9JBT'><fieldset id='z9JBT'></fieldset></dl></div>

    2. <legend id='z9JBT'><style id='z9JBT'><dir id='z9JBT'><q id='z9JBT'></q></dir></style></legend>
        <tfoot id='z9JBT'></tfoot>

        如何在 C++ 中创建多个向量的组合而无需硬编码循

        时间:2023-08-03
            <bdo id='FXJvI'></bdo><ul id='FXJvI'></ul>
            <legend id='FXJvI'><style id='FXJvI'><dir id='FXJvI'><q id='FXJvI'></q></dir></style></legend>

              <tfoot id='FXJvI'></tfoot>
                <tbody id='FXJvI'></tbody>

                1. <i id='FXJvI'><tr id='FXJvI'><dt id='FXJvI'><q id='FXJvI'><span id='FXJvI'><b id='FXJvI'><form id='FXJvI'><ins id='FXJvI'></ins><ul id='FXJvI'></ul><sub id='FXJvI'></sub></form><legend id='FXJvI'></legend><bdo id='FXJvI'><pre id='FXJvI'><center id='FXJvI'></center></pre></bdo></b><th id='FXJvI'></th></span></q></dt></tr></i><div id='FXJvI'><tfoot id='FXJvI'></tfoot><dl id='FXJvI'><fieldset id='FXJvI'></fieldset></dl></div>

                  <small id='FXJvI'></small><noframes id='FXJvI'>

                  本文介绍了如何在 C++ 中创建多个向量的组合而无需硬编码循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有几个看起来像这样的数据:

                  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 = ...
                  
                  #Note also that the member of each vector is always 3.
                  

                  我想要做的是在 Vector1 到 VectorK 中创建所有元素组合.因此最终我们希望得到这个输出(使用 Vector1,2,3):

                  What I want to do is to create all combination of elements in Vector1 through out VectorK. Hence in the end we hope to get this output (using Vector1,2,3):

                  TCC
                  TCG
                  TCT
                  TGC
                  TGG
                  TGT
                  TAC
                  TAG
                  TAT
                  CCC
                  CCG
                  CCT
                  CGC
                  CGG
                  CGT
                  CAC
                  CAG
                  CAT
                  ACC
                  ACG
                  ACT
                  AGC
                  AGG
                  AGT
                  AAC
                  AAG
                  AAT
                  

                  我现在遇到的问题是我的以下代码通过对循环进行硬编码来实现.由于向量的数量可以变化,我们需要一种灵活的方法来获得相同的结果.有吗?

                  The problem I am having now is that the following code of mine does that by hardcoding the loops. Since number of Vectors can be varied, we need a flexible way to get the same result. Is there any?

                  我的这段代码最多只能处理 3 个向量(硬编码):

                  This code of mine can only handle up to 3 Vectors (hardcoded):

                  #include <iostream>
                  #include <vector>
                  #include <fstream>
                  #include <sstream>
                  using namespace std;
                  
                  
                  int main  ( int arg_count, char *arg_vec[] ) {
                  
                      vector <string> Vec1;
                            Vec1.push_back("T");
                            Vec1.push_back("C");
                            Vec1.push_back("A");
                  
                      vector <string> Vec2;
                            Vec2.push_back("C");
                            Vec2.push_back("G");
                            Vec2.push_back("A");
                  
                      vector <string> Vec3;
                            Vec3.push_back("C");
                            Vec3.push_back("G");
                            Vec3.push_back("T");
                  
                  
                  
                       for (int i=0; i<Vec1.size(); i++) {
                          for (int j=0; j<Vec2.size(); j++) {
                              for (int k=0; k<Vec1.size(); k++) {
                                  cout << Vec1[i] << Vec2[i] << Vec3[k] << endl;
                              }
                          }
                       }
                  
                  
                  
                      return 0;
                  }
                  

                  推荐答案

                  这样做可以解决问题:

                  void printAll(const vector<vector<string> > &allVecs, size_t vecIndex, string strSoFar)
                  {
                      if (vecIndex >= allVecs.size())
                      {
                          cout << strSoFar << endl;
                          return;
                      }
                      for (size_t i=0; i<allVecs[vecIndex].size(); i++)
                          printAll(allVecs, vecIndex+1, strSoFar+allVecs[vecIndex][i]);
                  }
                  

                  致电:

                  printAll(allVecs, 0, "");
                  

                  这篇关于如何在 C++ 中创建多个向量的组合而无需硬编码循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:了解递归以生成排列 下一篇:捕获“堆栈溢出"递归 C++ 函数中的异常

                  相关文章

                  最新文章

                  <i id='WAF4j'><tr id='WAF4j'><dt id='WAF4j'><q id='WAF4j'><span id='WAF4j'><b id='WAF4j'><form id='WAF4j'><ins id='WAF4j'></ins><ul id='WAF4j'></ul><sub id='WAF4j'></sub></form><legend id='WAF4j'></legend><bdo id='WAF4j'><pre id='WAF4j'><center id='WAF4j'></center></pre></bdo></b><th id='WAF4j'></th></span></q></dt></tr></i><div id='WAF4j'><tfoot id='WAF4j'></tfoot><dl id='WAF4j'><fieldset id='WAF4j'></fieldset></dl></div>
                  <legend id='WAF4j'><style id='WAF4j'><dir id='WAF4j'><q id='WAF4j'></q></dir></style></legend>

                  <small id='WAF4j'></small><noframes id='WAF4j'>

                  1. <tfoot id='WAF4j'></tfoot>
                    • <bdo id='WAF4j'></bdo><ul id='WAF4j'></ul>