zoukankan      html  css  js  c++  java
  • [C++] algorithm Know unknown

    std::max_element

    #include <algorithm>
    #include <iostream>
    #include <vector>
    #include <cmath>
     
    static bool abs_compare(int a, int b)
    {
        return (std::abs(a) < std::abs(b));
    }
     
    int main()
    {
        std::vector<int> v{ 3, 1, -14, 1, 5, 9 }; 
        std::vector<int>::iterator result;
     
        result = std::max_element(v.begin(), v.end());
        std::cout << "max element at: " << std::distance(v.begin(), result) << '
    ';
     
        result = std::max_element(v.begin(), v.end(), abs_compare);
        std::cout << "max element (absolute) at: " << std::distance(v.begin(), result);
    }


    max element at: 5
    max element (absolute) at: 2


    std::distance

    #include <iostream>
    #include <iterator>
    #include <vector>
     
    int main() 
    {
        std::vector<int> v{ 3, 1, 4 };
        std::cout << "distance(first, last) = "
                  << std::distance(v.begin(), v.end()) << '
    '
                  << "distance(last, first) = "
                  << std::distance(v.end(), v.begin()) << '
    ';
    }

    distance(first, last) = 3
    distance(last, first) = -3

    std::reverse

    #include <vector>
    #include <iostream>
    #include <iterator>
    #include <algorithm>
     
    int main()
    {
        std::vector<int> v({1,2,3});
        std::reverse(std::begin(v), std::end(v));
        std::cout << v[0] << v[1] << v[2] << '
    ';
     
        int a[] = {4, 5, 6, 7};
        std::reverse(std::begin(a), std::end(a));
        std::cout << a[0] << a[1] << a[2] << a[3] << '
    ';
    }
  • 相关阅读:
    2019-05-07
    2019-04-28 问题记录
    2019-04-15 python深浅复制
    2019-04-11 统计日志重复数量
    2019-03-23 shell练习,日志统计
    问题记录2019-03-12
    问题记录2019-03-06(todo)
    回归
    Mac进行 usr/bin 目录下修改权限问题,operation not permitted
    个人的随笔心情
  • 原文地址:https://www.cnblogs.com/zhanxiage1994/p/6963814.html
Copyright © 2011-2022 走看看