有关清理此终止条件的建议
Advice on cleaning up this termination condition
我一直在使用C++11函数,以便与python的itertools.combs(input,2)做同样的事情,到目前为止,这就是我所拥有的:
编辑删除了@DavidRodríguez-dribeas建议的外部λ
#include <iostream>
#include <functional>
#include <vector>
using namespace std;
template <class T>
function<pair<T*, T*>()> combinations(vector<T> & input) {
auto it1 = input.begin();
auto end = input.end();
auto it2 = next(it1);
return [=]() mutable {
if (it2 == end) {
it1++;
it2 = next(it1);
}
if (it2 != end)
return pair<T*,T*>(&(*it1), &(*it2++));
return pair<T*,T*>(&*end, &*end);
};
};
int main (void) {
vector<int> numbers{1,2,3,4,5,6};
auto func = combinations(numbers);
while ( true ) {
auto i = func();
if (i.first == &*(numbers.end())) break;
cout << *(i.first) << ',' << *(i.second) << endl;
}
return 0;
};
我对用于迭代组合的方法不满意,是否有清理它的任何建议?
以下是我最喜欢的方法的文档和代码。 以下是该库将如何用于您的示例:
#include <iostream>
#include <vector>
#include "combinations"
using namespace std;
int main (void) {
vector<int> numbers{1,2,3,4,5,6};
for_each_combination(numbers.begin(), numbers.begin()+2, numbers.end(),
[](vector<int>::const_iterator b, vector<int>::const_iterator e)
{
if (b != e)
{
cout << *b;
for (auto i = b+1; i != e; ++i)
cout << ',' << *i;
cout << endl;
}
return false;
});
}
1,2
1,3
1,4
1,5
1,6
2,3
2,4
2,5
2,6
3,4
3,5
3,6
4,5
4,6
5,6
如果需要,将示例用法更改为一次考虑 3 或 4 个项目而不是 2 个项目是微不足道的。 还可以一次处理 N 中的各种排列 k。
更新
添加间接寻址级别以说明如何处理在矢量中移动/交换效率不高的项向量:
#include <iostream>
#include <vector>
#include "combinations"
using namespace std;
int main (void) {
vector<int> numbers{1,2,3,4,5,6};
vector<vector<int>::const_iterator> num_iters;
num_iters.reserve(numbers.size());
for (auto i = numbers.begin(); i != numbers.end(); ++i)
num_iters.push_back(i);
for_each_combination(num_iters.begin(), num_iters.begin()+2, num_iters.end(),
[](vector<vector<int>::const_iterator>::const_iterator b,
vector<vector<int>::const_iterator>::const_iterator e)
{
if (b != e)
{
cout << **b;
for (auto i = b+1; i != e; ++i)
cout << ',' << **i;
cout << endl;
}
return false;
});
}
我发现 Oliver Kowalke 的协程库已被 Boosts 同行评审接受,应该包含在下一个版本中。稍微跳了一下枪,我通过使用 boost-dev 存储库 (https://gitorious.org/boost-dev/boost-dev) 的协程分支试了一下。
g++ -I path/to/boost-dev -std=c++11 test_code.cpp -o run_test_code -static -L path/to/boost-dev/stage/lib/ -lboost_context
#include <boost/coroutine/all.hpp>
#include <boost/bind.hpp>
#include <boost/range.hpp>
#include <iostream>
#include <vector>
using namespace std;
using namespace boost;
template <typename T>
using coro_pairT_void = coroutines::coroutine<pair<T&,T&>(void)>;
template <typename T>
void combinations(typename coro_pairT_void<T>::caller_type & self, vector<T> & input ) {
for (auto it1 = input.begin(), itend = input.end(); it1 != itend; it1++) {
for (auto it2 = std::next(it1); it2 != itend; it2++) {
self(pair<T&, T&>(*it1,*it2));
}
}
};
int main( void ) {
vector<int> numbers{1,2,3,4,5,6};
coro_pairT_void<int> func(bind(combinations<int>, _1, numbers));
for (auto it(begin(func)), itend(end(func)); it != itend; ++it) {
cout << it->first << ',' << it->second << endl;
}
return 0;
};
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 在没有太多条件句的情况下,我如何避免被零除
- 基于多个条件处理地图中的所有元素
- 条件constexpr函数
- 无论条件是否为true,if总是在c++中执行
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- C++ 即使不满足条件,循环也会终止
- 对于检查 >=0 终止条件时的循环索引类型
- 条件运算符不允许程序终止
- 即使仍然满足条件,该循环也会终止
- 有关清理此终止条件的建议
- 循环终止条件错误的 cpp 向量
- 循环中没有检查条件,但循环仍然终止C++
- 如何有条件地终止OpenMP中的并行区域
- cURL在条件下终止下载
- 满足条件后如何终止函数
- while 循环在满足条件时不会终止 (c++)
- 是否定义有一个随机的循环终止条件?