码迷,mamicode.com
首页 > 编程语言 > 详细

c++ STD Gems07

时间:2019-12-30 19:43:32      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:c++   ota   ons   class   adc   rev   value   tor   ret   

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 << "\n";
    }

    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;
}

c++ STD Gems07

标签:c++   ota   ons   class   adc   rev   value   tor   ret   

原文地址:https://www.cnblogs.com/codemeta-2020/p/12121206.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!