forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
16.4.cpp
40 lines (36 loc) · 1023 Bytes
/
16.4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <vector>
#include <list>
#include <string>
#include <iostream>
template <typename InIter, typename Val>
InIter find_element(InIter bg, InIter ed, const Val &v) {
for (; bg != ed; ++bg)
if (*bg == v)
return bg;
return bg;
}
int main() {
std::vector<int> vi { 1, 2, 3, 4, 5 };
auto it1 = find_element(vi.begin(), vi.end(), 3);
if (it1 != vi.end())
std::cout << *it1 << std::endl;
else
std::cout << "not found" << std::endl;
auto it2 = find_element(vi.begin(), vi.end(), 8);
if (it2 != vi.end())
std::cout << *it2 << std::endl;
else
std::cout << "not found" << std::endl;
std::list<std::string> ls { "abc", "def", "gh" };
auto it3 = find_element(ls.begin(), ls.end(), "gh");
if (it3 != ls.end())
std::cout << *it3 << std::endl;
else
std::cout << "not found" << std::endl;
auto it4 = find_element(ls.begin(), ls.end(), "hello");
if (it4 != ls.end())
std::cout << *it4 << std::endl;
else
std::cout << "not found" << std::endl;
return 0;
}