• c++ STD Gems07


    reverse、rotate、permutation

    #include <iostream>
    #include <vector>
    #include <string>
    #include <iterator>
    #include <algorithm>
    #include <numeric>
    #include <random>
    
    template<class Container>
    void write_to_cout(Container& container, const char* delimiter = " ")
    {
        std::copy(container.begin(), container.end(),
            std::ostream_iterator<typename Container::value_type>(std::cout, delimiter) );
    }
    
    
    void test0()
    {
        std::vector<std::string> a = {"zero", "one", "two", "three", "four", "five", "six"};
        std::vector<std::string> b = {"0", "1", "2", "3", "4", "5", "6"};
    
        write_to_cout(a);
        std::cout << std::endl;
        //test
        std::rotate(a.begin(), a.begin() + 3, a.end());
        write_to_cout(a);
        std::cout << std::endl << std::endl;
    }
    
    void test1()
    {
        std::vector<std::string> b = {"0", "1", "2", "3", "4", "5", "6"};
        
        write_to_cout(b);
        std::cout << std::endl;
    
        // test 
        std::reverse(b.begin(), b.end());
        write_to_cout(b);
        std::cout << std::endl << std::endl;
    }
    
    void test2()
    {
        std::vector<std::string> a = {"zero", "one", "two", "three", "four", "five", "six"};
        write_to_cout(a);
        std::cout << std::endl;
    
        //test algorithm
        std::mt19937 rng( std::random_device{}() );
        std::shuffle(a.begin(), a.end(), rng);
    
        write_to_cout(a);
        std::cout << std::endl << std::endl;
    }
    
    void test3()
    {
        std::string s = "abc";
        std::string s1 = "adc";
        std::string s2 = "acb";
    
        //test 全排列
        while( std::next_permutation(s.begin(), s.end() ) )
        {
            std::cout << s << "
    ";
        }
    
        std::cout << std::endl;
        std::cout << std::is_permutation(s.begin(), s.end(), s1.begin() ) << std:: endl;
        std::cout << std::is_permutation(s.begin(), s.end(), s2.begin() ) << std:: endl;    
    
    }
    
    int main()
    {
        test0();
        test1();
        test2();
        test3();
    
        return 0;
    }
    
  • 相关阅读:
    信息产品是信息化理念的凝缩的精华
    自然科学技术表面上是反应人与自然的关系,更深层还是人与人之间的关系
    思与在,为何没有行
    haproxysocket 参数记录
    zabbix 监控 haproxy 记录
    Centos6.5安装OpenLDAP
    ansible mysql模块的使用今年
    haproxy 官方文档查看
    centos 7 部署 mysql 报错记录
    ansible playbook学习
  • 原文地址:https://www.cnblogs.com/codemeta-2020/p/12121206.html
Copyright © 2020-2023  润新知