前田稔(Maeda Minoru)の超初心者のプログラム入門
/*★ Algorithm Step6 前田 稔 ★*/ #include <iostream> #include <vector> using namespace std; void main() { int a[] = { 3, 3, 3, 3, 3, 3, 3, 3, 3, 5 }; // int a[]から3でない要素を見つける int* i = find_not(a, a+10, 3); if (i != a+10 ) cout << "見つかった! " << *i << endl; } |
template<class InputIterator, class T> InputIterator find_not(InputIterator first, InputIterator last, const T& x) find_not(Iterat first, Iterat last, const T& x) { while(first != last && *first == x) ++first; return first; } |
int t[] = { 3, 3, 3, 3, 3, 3, 7, 3, 3, 3 }; vector<int> v(t, t+10); // vector v[]から3でない要素を見つける vector<int>::iterator p = find_not(v.begin(), v.end(), 3); if (p != v.end()) cout << "見つかった! " << *p << endl; |
int b[] = { 8, 3, 3, 3, 3, 3, 3, 3, 3, 3 }; list<int> lst(b, b+10); // list lst[]から3でない要素を見つける list<int>::const_iterator q = find_not(lst.begin(), lst.end(), 3); if (q != lst.end()) cout << "見つかった! " << *q << endl; |