vector_perm.h

来自「利用递归查找数字的排列组合 使用了STL技术」· C头文件 代码 · 共 56 行

H
56
字号
// vector_perm.h
#include<vector>


#ifndef __VECTOR_PERM_H_
#define __VECTOR_PERM_H_


//recursive template permutation function
//Note : func parameters must always be vector<T> !
template <typename T, typename Func>
void vector_permutation(std::vector<T>& now,
          std::vector<T> next, Func func)
{
  int size=now.size();
  if(size>0)
  {
    for(int cnt=0; cnt<size;++cnt)
    {
      std::vector<T> vt;

      std::vector<T>::const_iterator it=now.begin();
      for(int cnt1=0;cnt1<size;++cnt1)
      {
        if(cnt1==cnt)
        {
          ++it;
          continue;
        }
        else
          vt.push_back(*it);
        ++it;  
      }
      
      std::vector<T>::const_iterator it1=now.begin();
      --it1;
      for(int cnt2=0;cnt2<=cnt;++cnt2)
      {
        ++it1;
      }
      
      next.push_back(*it1);
      vector_permutation(vt,next,func);
      next.pop_back();        
    }
    
  }          
  else
  {
    func(next);
  }  
}


#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?