zoukankan      html  css  js  c++  java
  • C++ STD Gems01

    本文是根据油管大神的C++标准库课程的一个学习笔记,该课程主要介绍c++标准库中一些非常有用并且代码经常用到的工具。

    copy 、copy_backward 、copy_n 、copy_if、swap_ranges

    #include <iostream>
    #include <iterator>
    #include <string>
    #include <algorithm>
    #include <vector>
    #include <cctype>
    
    
    template<typename Container>
    void write_to_cout(const Container& container, const char* delimiter = " ")
    {
        std::copy( container.begin(), container.end(),
            std::ostream_iterator<typename Container::value_type>( std::cout, delimiter) );
    }
    
    
    // 测试函数copy
    void test0()
    {
        // init test constainer
        std::vector<std::string> a = {"zero", "one", "two", "three", "four", "five", "six"};
        std::vector<std::string> b = {"0", "1", "2", "4", "5", "6", "7", "8", "9"};
    
        // wtire initial statements
        write_to_cout(a);
        std::cout << std::endl;
        write_to_cout(b);
        std::cout << std::endl;
    
        //test algorithm
        std::copy(a.begin(), a.begin() + 3, b.begin() + 4);
        write_to_cout(b);
        std::cout << std::endl;
        std::cout << std::endl;
    }
    
    void test1()
    {
        std::vector<std::string> a = {"zero", "one", "two", "three", "four", "five", "six"};
        std::vector<std::string> b = {"0", "1", "2", "4", "5", "6", "7", "8", "9"};
    
        write_to_cout(a);
        std::cout << std::endl;
        write_to_cout(b);
        std::cout << std::endl;
    
        std::copy(a.begin(), a.end(), std::back_inserter(b));
        write_to_cout(b);
        std::cout << std::endl;
        std::cout << std::endl;
    }
    
    void test2()
    {
        // std::vector<std::string> a = {"zero", "one", "two", "three", "four", "five", "six"};
        std::vector<std::string> b = {"0", "1", "2", "4", "5", "6", "7", "8", "9"};
    
        // write_to_cout(a);
        // std::cout << std::endl;
        write_to_cout(b);
        std::cout << std::endl;
    
        // test algorithm
        std::copy(b.begin(), b.begin() + 4, b.begin() +3);  // 元素从前往后挨个复制过去
        write_to_cout(b);
        std::cout << std::endl;
        std::cout << std::endl;
    }
    
    void test3()
    {
        std::vector<std::string> a = {"zero", "one", "two", "three", "four", "five", "six"};
        std::vector<std::string> b = {"0", "1", "2", "4", "5", "6", "7", "8", "9"};
    
        write_to_cout(a);
        std::cout << std::endl;
        write_to_cout(b);
        std::cout << std::endl;
    
        //test algorithm
        std::copy_backward(a.begin(), a.begin() + 2, b.begin() + 3); //从后往前挨个复制过去
        write_to_cout(b);
        std::cout << std::endl;
        std::cout << std::endl;
    }
    
    void test4()
    {
        std::vector<int> a;
        std::copy_n(std::istream_iterator<int>(std::cin), 5, std::back_inserter(a)); // 输入指定的数量的元素
        write_to_cout(a);
        std::cout << std::endl;
        std::cout << std::endl;
    }
    
    void test5()
    {
        std::string a = "HellO, WOrlD";
        std::string b = "wHat are yoU dOinG";
    
        write_to_cout(a);
        std::cout << std::endl;
        write_to_cout(b);
        std::cout << std::endl;
    
        //test algorithm
        std::string uppers;
        // 有条件地复制
        std::copy_if(a.begin(), a.end(), std::back_inserter(uppers), [](unsigned char c){return std::isupper(c);} );  // 为什么直接写std::isupper不行呢
        std::copy_if(b.begin(), b.end(), std::back_inserter(uppers), [](unsigned char c){return std::isupper(c);} );
    
        write_to_cout(uppers);
        std::cout << std::endl << std::endl;
    }
    
    void test6()
    {
        std::vector<std::string> a = {"zero", "one", "two", "three", "four", "five", "six"};
        std::vector<std::string> b = {"0", "1", "2", "4", "5", "6", "7", "8", "9"};
    
        write_to_cout(a);
        std::cout << std::endl;
        write_to_cout(b);
        std::cout << std::endl;
    
        //test algorithm
        std::swap_ranges(a.begin(), a.begin() + 3, b.begin());  //作用于copy等效
        write_to_cout(b);
        std::cout << std::endl << std::endl;
    
    }
    
    int main()
    {
        test0(); 
        test1();
        test2();
        test3();
        test4();
        test5();
        test6();
        return 0;
    }
    
  • 相关阅读:
    Java多线程 编写三各类Ticket、SaleWindow、TicketSaleCenter分别代表票信息、售票窗口、售票中心。 售票中心分配一定数量的票,由若干个售票窗口进行出售,利用你所学的线程知识来模拟此售票过程。
    java中异常处理机制 throw抛出自定义业务逻辑异常 throws继续抛出 catch捕获后会自动继续抛向调用方法
    Map集合应用 取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,输出格式为:a(2)b(1)k(2)...
    Java中List集合排序的方法 比较器的使用 根据学生对象数学 语文 英语成绩总和进行sort排序
    美国银行
    Time Difference
    马来西亚与新加坡两国的标准时间为UTC+8
    java主要城市时区对照表
    韩国时区 KST
    AIX 系统
  • 原文地址:https://www.cnblogs.com/codemeta-2020/p/12111605.html
Copyright © 2011-2022 走看看