【发布时间】:2018-02-05 01:00:09
【问题描述】:
我试图找到元素的排列和组合,每个元素有 2 个对象 - nC2 或 nP2。我可以通过以下代码找到组合。有什么优雅的方法来重写它吗?另外,有什么方法可以找到排列吗?以下只是一个示例,我的数据集包含近 2000 个元素。所以,速度也是一个因素。
#include <iostream>
#include <vector>
#include <string>
int main() {
std::vector<std::string> array = {"a", "b", "c", "d", "e"};
std::vector<std::string>::iterator it = array.begin();
for ( ; it < array.end(); it++ ) {
for (std::vector<std::string>::iterator it_next = it+1 ; it_next < array.end(); it_next++ ) {
std::cout << *it << *it_next << "\n";
}
}
}
程序输出-
gcc 版本 4.6.3
ab 交流 广告 ae 公元前 BD 是 光盘 ce 去
【问题讨论】:
-
它工作正常,考虑使用Code Review。
-
@Ron ... 为什么要删除 repl 链接?
-
请注意这里有两个独立的问题: 1. 无论如何要改进组合? 2.如何计算排列?
-
@Ron 明白并说得通。仍在学习规则:)