美文网首页
[GeekBand][STL与泛型编程]第一周作业

[GeekBand][STL与泛型编程]第一周作业

作者: 散夜霜 | 来源:发表于2017-03-06 22:09 被阅读0次

    1. 运行结果

    ==============================
    1.
    0 10 20 30 1 0
    ==============================
    2.
            num.push_back(1);
            num.push_back(2);
    Sum = 64
    ==============================
    3.
    Middle:20 10
    ==============================
    4.
    1 30 20 10 1 2
    ==============================
    请按任意键继续. . .
    

    2. 源代码

    #include <iostream>
    #include <numeric>
    #include <iterator>
    #include <algorithm>
    #include <functional>
    #include <list>
    #include <vector>
    #include <string>
    
    const std::string Cut_off = "==============================\n";
    
    int main()
    {
        std::cout << Cut_off << "1.\n";
        std::list<int> num{ 0,1,30,20,10,0 };
        for (std::list<int>::reverse_iterator tmp = num.rbegin();tmp!=num.rend();++tmp)
            std::cout << *tmp << " ";
        std::cout << std::endl;
    
        std::cout << Cut_off << "2.\n";
        num.push_back(1);
        num.push_back(2);
        std::cout << R"(    num.push_back(1);
        num.push_back(2);
    Sum = )" << accumulate(num.begin(), num.end(), 0) << std::endl;
    
        std::cout << Cut_off << "3.\n";
        if (num.size() % 2)
        {
            std::list<int>::iterator tmp = num.begin();
            std::advance(tmp, num.size() / 2);
            std::cout << *tmp << std::endl;
        } 
        else
        {
            std::list<int>::iterator tmp = num.begin();
            std::advance(tmp, num.size() / 2 - 1);
            std::cout << "Middle:" << *tmp << " ";
            std::cout << *(++tmp) << std::endl;
        }
    
        std::cout << Cut_off << "4.\n";
        std::vector<int> vec;
        copy_if(num.begin(), num.end(), back_inserter(vec),
            std::bind1st(std::not_equal_to<int>(), 0));         // 抱大腿学来的,别打我
        for (int i : vec)
            std::cout << i << " ";
        std::cout << std::endl;
    
        std::cout << Cut_off;
        system("pause");
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:[GeekBand][STL与泛型编程]第一周作业

          本文链接:https://www.haomeiwen.com/subject/atqpgttx.html