⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vector_perm.h

📁 利用递归查找数字的排列组合 使用了STL技术
💻 H
字号:
// 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -