zoukankan      html  css  js  c++  java
  • c++ combination by next_permutation

    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    int main() {
        int n, r;
        n = 10;
        r = 2;
    
        std::vector<bool> v(n);
        std::fill(v.end() - r, v.end(), true);
        std::vector< std::vector<int> > sequence_vector;
    
        do {
            std::vector<int> sequence;
            for (int i = 0; i < n; ++i) {
                if (v[i]) {
                    std::cout << (i + 1) << " ";
                    sequence.push_back(i+1);
                }
            }
            std::cout << "
    ";
            sequence_vector.push_back(sequence);
        } while (std::next_permutation(v.begin(), v.end()));
    
        std::cout<<"from vector"<<std::endl;
    
        std::vector<int>::iterator iterator_sequence;
        std::vector< std::vector<int> >::iterator iterator_sequence_vector;
        for(iterator_sequence_vector = sequence_vector.begin();
          iterator_sequence_vector != sequence_vector.end(); iterator_sequence_vector++){
          for (iterator_sequence = (*iterator_sequence_vector).begin();
            iterator_sequence !=  (*iterator_sequence_vector).end(); iterator_sequence++){
            std::cout<<*iterator_sequence<<" ";
          }
          std::cout<<std::endl;
        }
    
        return 0;
    }
    MySQL限时解答,24小时内友哥专业解答
    http://www.yougemysqldba.com
    如有进一步需要请联系微信onesoft007
    微博账号@友哥一指
  • 相关阅读:
    GoF23:工厂模式(Factory)
    CSS
    HTML
    JSP基础学习
    JSTL标签
    Jsoup
    Centos7下tomcat关闭异常问题
    剑指Offer_#18_删除链表的节点
    剑指Offer_#17_打印从1到最大的n位数
    剑指Offer_#16_数值的整数次方
  • 原文地址:https://www.cnblogs.com/youge-OneSQL/p/9487305.html
Copyright © 2011-2022 走看看