STL迭代器与部分算法学习笔记
来源:互联网 发布:网红拍照软件 编辑:程序博客网 时间:2024/06/06 14:18
迭代器是类似指针的对象,分为5种,输入,输出,前向,双向和随机访问
输入迭代器(InputIterator)
输入迭代器并不是指某种类型,而是指一系列类型
举例
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;}
非可变序列算法(不改变容器内容);find, adjacent_find, count, for_each, midmatch, equal, search
find_if,查找序列中第1个使给定的判断函数(函数对象)返回真的元素
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;}
adjacent_find算法在序列中查找相邻且相等的两个元素(规则由函数对象决定,默认为相等),当找到这样的两个元素时,该算法返回指向两个元素中第一个元素的迭代器
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;}
count是一种非可变序列算法,其功能是在序列中查找等于某个给定值的元素的个数
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;}count_if:
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;}
for_each 对序列中的每个元素施加由函数f指定的操作
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;}
可变序列算法(可以修改容器的内容)copy fill generate partition shuffle remove replace reverse swap transform
copy将容器中的元素从一个区间复制到另一个区间
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;}
partition
对于给定区间[first, last)和一个一元判断函数pred, 类属算法partition可以对该区间内的元素重新排列,以使所有满足判断函数pred的元素排在所有不满足pred的元素前面,该算法还有一个版本stable_partition,能保证分割后的每一组元素的相对位置保持不变,返回值都是一个迭代器,该迭代器代表第一组数据的结尾,同时也是第二组数据的开头
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;}
- STL迭代器与部分算法学习笔记
- Primer STL笔记第二部分容器与算法
- STL相关算法部分源代码学习
- 泛型编程与STL学习笔记之算法
- c++学习笔记之复制控制与stl算法
- 《Effective STL》学习笔记(第一部分)
- 《Effective STL》学习笔记(第二部分)
- 《Effective STL》学习笔记(第三部分)
- 《Effective STL》学习笔记(第四部分)
- 《Effective STL》学习笔记(第一部分)
- 《Effective STL》学习笔记(第二部分)
- 《Effective STL》学习笔记(第三部分)
- 《Effective STL》学习笔记(第四部分)
- STL学习笔记--排序算法
- c++与stl入门部分学习
- 算法学习笔记----第一部分
- STL部分算法
- STL学习笔记----11.STL算法之 (变动性算法)
- 对九个超级程序员的采访
- 可能要离开人人网了
- 0 1 背包问题 learn2
- linux中的top指令作用
- Nutch学习笔记二
- STL迭代器与部分算法学习笔记
- C# base和this
- POJ Genealogical tree(拓扑排序)
- oracle sql日期
- getOutputStream() has already been called for this response异常的原因和解决方法
- 编程之美 2013 全国挑战赛 资格赛 题目三 树上的三角形
- C++ 异常处理(基础篇)
- 电容充放电
- 二叉树的遍历(更新中)