1.工程文件
QT = core
CONFIG += c++17 cmdline
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
2.主程序
#include <QCoreApplication>
#include <vector>
#include <iostream>
using namespace std;
vector<vector<int>> getCombinationList(int nums[],int ncount)
{
vector<vector<int>> retvv = vector<vector<int>>();
retvv.push_back(vector<int>());
for (int n=0; n<ncount; n++) {
int size = retvv.size();
for (int i = 0; i < size; i++) {
vector<int> newSub = vector<int>(retvv[i]);
newSub.push_back(n);
retvv.push_back(newSub);
}
}
return retvv;
}
void showCombinationList( vector<vector<int>> va )
{
std::cout << "[ \n";
for (int var = 0; var < va.size(); ++var) {
std::cout << " [ ";
for(int j=0; j< va[var].size();j++)
{
std::cout << va[var][j] << " ";
}
std::cout << "]\n";
}
std::cout << "]\n";
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int test[] = {1,2,3,4,5};
vector<vector<int>> va = getCombinationList(test, 5);
showCombinationList(va);
return a.exec();
}
3.执行结果
[
[ ]
[ 0 ]
[ 1 ]
[ 0 1 ]
[ 2 ]
[ 0 2 ]
[ 1 2 ]
[ 0 1 2 ]
[ 3 ]
[ 0 3 ]
[ 1 3 ]
[ 0 1 3 ]
[ 2 3 ]
[ 0 2 3 ]
[ 1 2 3 ]
[ 0 1 2 3 ]
[ 4 ]
[ 0 4 ]
[ 1 4 ]
[ 0 1 4 ]
[ 2 4 ]
[ 0 2 4 ]
[ 1 2 4 ]
[ 0 1 2 4 ]
[ 3 4 ]
[ 0 3 4 ]
[ 1 3 4 ]
[ 0 1 3 4 ]
[ 2 3 4 ]
[ 0 2 3 4 ]
[ 1 2 3 4 ]
[ 0 1 2 3 4 ]
]