zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    MySQL--自增列持久化问题
    MySQL--”自然键”和”代理键”优缺点
    MySQL--REPLACE INTO更新自增列值引发的异常
    MySQL Inception--原理和注意事项
    MySQL Inception--安装
    MySQL--关联更新
    MySQL--Delete语句别名+LIMIT
    MySQL Disk--SSD 特性
    BootStrap简介
    BootStrap简单使用
  • 原文地址:https://www.cnblogs.com/codemeta-2020/p/12121206.html
Copyright © 2011-2022 走看看