大家好,这里是国中之林!
❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←
问题:
解答:
#include <iostream>
#include <list>
using namespace std;
template<typename T>
int reduce(T ar[], int n)
{
list<T>ls;
ls.insert(ls.end(), ar, ar + n);
ls.sort();
ls.unique();
auto pd = ls.begin();
for (int i = 0; i < ls.size(); i++, pd++)
{
*(ar + i) = *pd;
}
return ls.size();
}
int main()
{
long ar[5] = { 45000,3400,45000,100000,2500 };
int resize = reduce(ar, 5);
cout << "array: \n";
for (int i = 0; i < resize; i++)
{
cout << ar[i] << " ";
}
string ar2[6] = { "it","aboard","it","zone","quit","aa" };
resize = reduce(ar2, 6);
cout << "\narray2: \n";
for (int i = 0; i < resize; i++)
{
cout << ar2[i] << " ";
}
return 0;
}
运行结果:
考查点:
- 模板函数
- list容器
2024年9月17日20:34:04
标签:cout,int,reduce,ar,Plus,ls,pd,习题,Primer From: https://blog.csdn.net/qq_74047911/article/details/142318417