1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- #include <iostream>
- #include <vector>
- //#include <iterator>
- using namespace std;
- template <typename T>
- void display(const vector<T>& vec, ostream& os=cout) {
- //const_iterator 敲错了
- // 编译错误need 'typename' before *** because *** is a dependent scope; https://blog.csdn.net/pb1995/article/details/49532285/
- typename vector<T>::const_iterator it = vec.begin();
- typename vector<T>::const_iterator it_end = vec.end();
- for (; it != it_end; ++it) {
- os << *it << ' ';
- }
- os << endl;
- }
- template <typename IteratorType, typename elemType>
- IteratorType myfind(IteratorType first, IteratorType last, const elemType& value) {
- for (; first != last; ++first) {
- if (value == *first)
- return first;
- }
- return last;
- }
- /*
- template <typename elemType>
- void display_vector(const vector<elemType>& vec, ostream& os = cout, int len = 8)
- {
- typename vector<elemType>::const_iterator
- iter = vec.begin(),
- end_it = vec.end();
- int elem_cnt = 1;
- while (iter != end_it) {
- os << *iter++
- << (!(elem_cnt++ % len) ? '\n' : ' '); // 一行8个字符串
- os << endl;
- }
- }
- */
- int main() {
- vector<int> vec = { 12,3,4,5 };
- display(vec);
- vector<int>::iterator it;
- it = myfind(vec.begin(), vec.end(), 3);
- cout << *it << endl;
- return 0;
- }
|