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