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;
    }
    
  • 相关阅读:
    如何准备面试复试(1)
    如何应对糟糕的面试官(2)
    美国:经济危机让烟民吸烟量增加
    MOV 指令的注意事项
    一个简单的ajax无刷新翻页的程序
    SQL注入漏洞全接触
    php+mysql非暴力查表的注入语句写法总结
    在你的成长过程中,有五个人非常重要
    爱就在那里,不增不减
    php的一些书籍
  • 原文地址:https://www.cnblogs.com/codemeta-2020/p/12121206.html
Copyright © 2011-2022 走看看