有关清理此终止条件的建议

Advice on cleaning up this termination condition

本文关键字:条件 终止      更新时间:2023-10-16

我一直在使用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;
};