在工作中,遇到查询条件是多个时,需要多个数组组合,才能满足条件,这时,就用到了多个数组组合的算法,比如,数组array1={“a”,“b”,“c”},数组array2={“d”,“e”} ,最后,要组合成一个数组{“ad”,“ae”,“bd”,“be”,“cd”,“ce”}废话少说,上代码。
std::vector<std::vector<std::string>> m_vecCombitation;
std::vector<std::string> vecAdd;
void GetCombination(int k, std::vector<std::vector<std::string>>vecInput,
std::vector<std::string>vecAdd)
{
if (k < vecInput.size())
{
std::vector<std::string> vecArr = vecInput[k];
if (vecArr.size() == 0)
{
return;
}
for (int i = 0; i < vecArr.size(); i++)
{
std::vector<ColorProperties> vecNew = vecAdd;
vecNew.push_back(vecArr[i]);
GetCombination(k + 1, vecInput, vecNew);
}
}
else
{
std::vector<ColorProperties> vecArr;
for (int i = 0; i < vecAdd.size(); i++)
{
vecArr.push_back(vecAdd[i]);
}
m_vecCombitation.push_back(vecArr);
}
}