I am trying to calculate a lot of combinations in C++. I came up with the following implement by myself, but its efficiency is not satisfactory. It takes more than 3 seconds to get C-18-2 (every 2 combination of 18), I believe this can be done in much less time.
vector<vector<int>> Mytool::combo2(int len){ MatrixXd ma = MatrixXd::Zero(len*len*len,2); int ind = 0; for (int i = 0 ;i<len;i++){ for (int j = 0 ;j<len;j++){ VectorXd v1(2); v1<<i,j; ma.row(ind) = v1; ind++; } }; ind = 0; vector<vector<int>> res; for (int i=0;i<ma.rows();i++){ int num1 = ma(i,0); int num2 = ma(i,1); if (num1!=num2){ vector<int> v1; v1.push_back(num1); v1.push_back(num2); sort(v1.begin(),v1.end()); if (find(res.begin(),res.end(),v1)==res.end()) res.push_back(v1); } } return res; } Any hints or advise will be helpful. Thank you in advance.