【发布时间】:2017-11-16 19:20:35
【问题描述】:
我正在尝试编写一个 tempate 函数,它接受一个序列(通过 2 个迭代器)并计算该序列的排列数,其中没有连续的相同元素。 我就是这么做的
template<class Iterator>
size_t count_perm(Iterator p, Iterator q)
{
if (p == q)
return 1;
size_t count = 0;
while(std::next_permutation(p, q)){
if(std::adjacent_find(p,q) != q)
++count;
}
}
/*Example
std::array<int, 3> a1 = {1,2,3};
size_t c1 = count_perm(a1.begin(), a1.end()); // 6
std::array<int, 5> a2 = {1,2,3,4,4};
size_t c2 = count_perm(a2.begin(), a2.end()); // 36*/
如果我通过 const 迭代器,此代码将不起作用。我应该更改哪些内容才能使其与 const 迭代器一起使用?
【问题讨论】:
-
我建议您与math.stackexchange.com 的人交谈。我敢打赌,他们可以为您提供一种更有效的计算方式。