vgghhhgllljh 2022-05-22 21:38 采纳率: 50%
浏览 85
已结题

C++函数模板的问题

问题遇到的现象和发生背景

刚学习C++,对它的理解还停留在数据成员和成员函数,这里遇到的函数模板问题,解决不了

问题相关代码,请勿粘贴截图
The function template min_elem() finds the smallest element in the range [first, last) and returns the iterator to the smallest element. The way how smallest is understood can be customized.

 

#include <functional>
#include <iostream>
#include <iterator>
#include <string>
#include <list>
#include <vector>

template<class ForwardIt, class Compare>
ForwardIt min_elem(ForwardIt first, ForwardIt last, 
 comp)
{
  if (first == last)
    return 
;

  ForwardIt smallest = first++;
  for (; 
; ++first) {
    if (comp(*first, *smallest)) {
      smallest = first;
    }
  }
  return 
;
}

template<class ForwardIt>
ForwardIt min_elem(ForwardIt first, ForwardIt last)
{
  return min_elem(first, last, std::less<
 std::iterator_traits<ForwardIt>::value_type>());
}

struct Student {
  int id;
  std::string name;
  bool 
(const Student& s2) const {
    return id < s2.id;
  }
};

std::ostream& operator<<(std::ostream& out, const Student& s)
{
  out << s.name << s.id;
  return out;
}

int main()
{
  std::vector<int> v = { 3, 1, 4, 2, 5, 9 };
  std::cout << "min element is: " << *min_elem(std::begin(v), std::end(v)) << std::endl;
  
  std::list<double> l = { 3.1, 1.2, 4.3, 2.4, 5.5, 9.7 };
  std::cout << "min element is: " << *min_elem(std::begin(l), std::end(l), std::greater<double>()) << std::endl;
  
  Student s[] = { {30, "Curry"}, {23, "Lebron"}, {35, "Durant"} };
  std::cout << "min element is: " << *min_elem(std::begin(s), std::end(s)) << std::endl;
}

运行结果及报错内容

img

我的解答思路和尝试过的方法

还不太明白函数模板的原理

我想要达到的结果

希望给出题目解答的同时,给出一些思路上的指点

  • 写回答

3条回答 默认 最新

  • 502203305 2022-05-22 22:18
    关注
    获得3.75元问题酬金
    
    // The function template min_elem() finds the smallest element in the range [first, last) and returns the iterator to the smallest element. The way how smallest is understood can be customized.
     
     
     
    #include <functional>
    #include <iostream>
    #include <iterator>
    #include <string>
    #include <list>
    #include <vector>
     
    template<class ForwardIt, class Compare>
    ForwardIt min_elem(ForwardIt first, ForwardIt last, 
    Compare // 比较类实例
    comp)
    {
      if (first == last)
        return first; // 返回第一个.
     
      ForwardIt smallest = first++;
      for (; first != last // 当前不是迭代器末尾.
        ; ++first) {
        if (comp(*first, *smallest)) {
          smallest = first;
        }
      }
      return smallest // smallest保存着最小值。
    ;
    }
     
    template<class ForwardIt>
    ForwardIt min_elem(ForwardIt first, ForwardIt last)
    {
      return min_elem(first, last, std::less< typename // 去除语义二义性。
     std::iterator_traits<ForwardIt>::value_type>());
    }
     
    struct Student {
      int id;
      std::string name;
      bool operator< //运算符重载
    (const Student& s2) const {
        return id < s2.id;
      }
    };
     
    std::ostream& operator<<(std::ostream& out, const Student& s)
    {
      out << s.name << s.id;
      return out;
    }
     
    int main()
    {
      std::vector<int> v = { 3, 1, 4, 2, 5, 9 };
      std::cout << "min element is: " << *min_elem(std::begin(v), std::end(v)) << std::endl;
      
      std::list<double> l = { 3.1, 1.2, 4.3, 2.4, 5.5, 9.7 };
      std::cout << "min element is: " << *min_elem(std::begin(l), std::end(l), std::greater<double>()) << std::endl;
      
      Student s[] = { {30, "Curry"}, {23, "Lebron"}, {35, "Durant"} };
      std::cout << "min element is: " << *min_elem(std::begin(s), std::end(s)) << std::endl;
    }
     
    
    评论

报告相同问题?

问题事件

  • 系统已结题 5月30日
  • 创建了问题 5月22日

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况