HOWTO创建多个载体的组合,而无需在C ++中硬编码的循环? [英] Howto create combinations of several vectors without hardcoding loops in C++?
问题描述
我有几个数据,看起来像这样:
Vector1_elements = T,C,A
Vector2_elements = C,G,A
Vector3_elements = C,G,T
..... 取决于 ...
VectorK_elements = ...
#Note也即每个矢量的构件总是3。
我想要做的是通过创建出VectorK在向量1元素的所有组合。 因此,我们到底希望得到这个输出(使用Vector1,2,3):
台泥
TCG
TCT
TGC
TGG
TGT
TAC
标签
TAT
CCC
CCG
CCT
CGC
CGG
CGT
CAC
CAG
猫
ACC
ACG
法案
AGC
AGG
AGT
AAC
AAG
AAT
现在我遇到的问题是,我的下面的code确实通过硬编码的循环。 由于向量的数量可以是多种多样的,我们需要一个灵活的方式来得到同样的结果。 有没有?
我这code只能处理高达3载体(硬codeD):
的#include<的iostream>
#包括<载体>
#包括< fstream的>
#包括< sstream>
使用名字空间std;
INT主(INT ARG_COUNT,字符* arg_vec []){
矢量<串GT; VEC 1;
Vec1.push_back(T);
Vec1.push_back(C);
Vec1.push_back(A);
矢量<串GT; VEC2;
Vec2.push_back(C);
Vec2.push_back(G);
Vec2.push_back(A);
矢量<串GT; VEC3;
Vec3.push_back(C);
Vec3.push_back(G);
Vec3.push_back(T);
的for(int i = 0; I< Vec1.size();我++){
对于(INT J = 0; J< Vec2.size(); J ++){
对于(INT K = 0; K< Vec1.size(); k ++){
COUT<< VEC 1 [1] - ;&其中; VEC2 [1] - ;&其中; VEC3 [k]的&其中;&其中; ENDL;
}
}
}
返回0;
}
这将这样的伎俩:
无效printAll(常量矢量<矢量<串GT;>&安培; allVecs,为size_t vecIndex,串strSoFar)
{
如果(vecIndex> = allVecs.size())
{
COUT<< strSoFar<< ENDL;
返回;
}
用于(为size_t I = 0; I< allVecs [vecIndex] .size();我++)
printAll(allVecs,vecIndex + 1,strSoFar + allVecs [vecIndex] [I]);
}
与电话:
printAll(allVecs,0,);
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.
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?
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;
}
This will do the trick:
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]);
}
Call with:
printAll(allVecs, 0, "");
这篇关于HOWTO创建多个载体的组合,而无需在C ++中硬编码的循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!