未能推断出模板参数,求调试成功!

majiayefengkuanghaha majiayefengkuanghaha 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include <iostream>
#include <vector>
#include <functional>
#include <string>

using namespace std;

class A
{
public:
string name;
void print(ostream& out) const
{
out << name << endl;
}
};

ostream& operator<<(ostream& out, const A& a)
{
a.print(out);
return out;
}

template <typename T, typename T1>
const T& findMax(const vector<T> & a, T1 b = less<T1>)
{
int maxIndex = 0;
for(int i = 1; i != a.size(); ++i)
{
if(b(a[maxIndex], a[i]))
{
maxIndex = i;
}
}
return a[maxIndex];
}

class B
{
public:
bool operator()(const string& a, const string& b) const
{
return _stricmp(a.c_str(), b.c_str()) < 0;
}
};

int main()
{
vector<A> a(3);
a[0].name = "A";
a[1].name = "a";
a[2].name = "c";
cout << findMax(a) << endl;
cout << findMax(a,B()) << endl;
}
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
majiayefengkuanghaha
C++ 语言 65,214 成员 250,514 内容