代码实践
#include <iostream>
using namespace std;
#include <vector>
#include <list>
#include <string>
template<typename IteratorType, typename ElemType>
IteratorType Find(IteratorType begin, IteratorType end, ElemType elem)
{
for (; begin != end; begin++)
{
if (*begin == elem)
{
return begin;
}
}
return end;
}
int main()
{
const int size = 5;
int a[size] = { 1,2,3,4,5 };
int *p = Find(a, a + size, 4);
if (p != a + size)
{
cout << *p << endl;
}
vector<int> v1(a, a + size);
vector<int>::iterator it = Find(v1.begin(), v1.end(), 3);
if (it != v1.end())
{
cout << *it << endl;
}
list<int> L(a, a + size);
list<int>::iterator iter = Find(L.begin(), L.end(), 3);
if (iter != L.end())
{
cout << *iter << endl;
}
string str[] = { "Hello","S","T","L","!" };
const vector<string> vstr(str, str + size);
vector<string>::const_iterator c_it = Find(vstr.begin(), vstr.end(), "!");
if (c_it != vstr.end())
{
cout << *c_it << endl;
}
system("pause");
return 0;
}
今天还是20200307 膜拜这些设计者!👋👋👋