迭代器是类似指针的对象,分为5种,输入,输出,前向,双向和随机访问
template<class InputIterator, class T> InputIterator find (InputIterator first, InputIterator last, const T& val) { while (first!=last) { if (*first==val) return first; ++first; } return last; }
template<class InputIterator, class UnaryPredicate> InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred) { while (first!=last) { if (pred(*first)) return first; ++first; } return last; }
template <class ForwardIterator> ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last) { if (first != last) { ForwardIterator next=first; ++next; while (next != last) { if (*first == *next) // or: if (pred(*first,*next)), for version (2) return first; ++first; ++next; } } return last; }
template <class InputIterator, class UnaryPredicate> typename iterator_traits<InputIterator>::difference_type count (InputIterator first, InputIterator last, UnaryPredicate pred) { typename iterator_traits<InputIterator>::difference_type ret = 0; while (first!=last) { if (pred(*first)) ++ret; ++first; } return ret; }
template <class InputIterator, class UnaryPredicate>
typename iterator_traits<InputIterator>::difference_type
count (InputIterator first, InputIterator last, UnaryPredicate pred)
{
typename iterator_traits<InputIterator>::difference_type ret = 0;
while (first!=last) {
if (pred(*first)) ++ret;
++first;
}
return ret;
}
例子:
#include <algorithm> #include <iostream> #include <vector> #include <functional> using namespace std; int main(void) { int arr[] = {1, 3, 2, 1, 4, 1, 7, 1, 10}; vector<int> v(&arr[0], &arr[9]); int cnt1 = count(v.begin(), v.end(), 1); int cnt2 = count_if(v.begin(), v.end(), bind2nd(not_equal_to<int>(), 1));//bind2nd是函数适配器。可将函数对象作为参数 cout << "the count of 1 in v is " << cnt1 << endl; cout << "the cnt of not equaling 1 is " << cnt2 << endl; return 0; }
template<class InputIterator, class Function> Function for_each(InputIterator first, InputIterator last, Function fn) { while (first!=last) { fn (*first); ++first; } return fn; // or, since C++11: return move(fn); }例子:
#include <algorithm> #include <iostream> #include <vector> using namespace std; void print(string &s) { cout << s << endl; } int main(void) { vector<string> str_v; str_v.push_back("Clark"); str_v.push_back("Rindt"); str_v.push_back("Senna"); for_each(str_v.begin(), str_v.end(), print); return 0; }
template<class InputIterator, class OutputIterator> OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result) { while (first!=last) { *result = *first; ++result; ++first; } return result; }
#include <iostream> #include <algorithm> #include <iterator> #include <vector> using namespace std; int main(void) { int arr[] = {1, 2, 3, 4, 5}; vector<int> v(&arr[0], &arr[5]); ostream_iterator<int> out(cout, "\n"); copy(v.begin(), v.end(), out); return 0; }
template <class BidirectionalIterator, class UnaryPredicate> BidirectionalIterator partition (BidirectionalIterator first, BidirectionalIterator last, UnaryPredicate pred) { while (first!=last) { while (pred(*first)) { ++first; if (first==last) return first; } do { --last; if (first==last) return first; } while (!pred(*last)); swap (*first,*last); ++first; } return first; }例子
#include <iostream> #include <algorithm> #include <ctime> #include <iterator> #include <algorithm> #include <sys/time.h> #include <cstdlib> using namespace std; struct IsEven //函数对象 { bool operator()(int x) { return (x&0x01)==1; } }; int myrandom(int i) { return rand()%i; } int main(void) { srand((unsigned)time(NULL)); int arr[10]; struct timeval start, end; for(int i=0; i<10; i++) arr[i] = i; random_shuffle(&arr[0], &arr[10], myrandom); //将数组元素打乱 gettimeofday(&start, NULL); cout << *(partition(&arr[0], &arr[10], IsEven())) << endl; //重排数组,将奇偶数分开 //计算运行时间
gettimeofday(&end, NULL); float t_time = end.tv_sec-start.tv_sec+(end.tv_usec-start.tv_usec)/1000000.0; cout << "time used " << t_time << endl; ostream_iterator<int> out(cout, " "); copy(&arr[0], &arr[10], out); cout << endl; return 0; }