2017-08-20 17:26:07
writer:pprp
1、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; }
测试:
/* name : STL的其他用法 writer : pprp declare : null date ; 2017/8/20 */ #include <bits/stdc++.h> using namespace std; typedef list<int> LIST; bool Equal(int a, int b) { return (a - b)%2 == 0?1:0; } void print(LIST&q) { LIST::iterator it; for(it = q.begin() ; it != q.end(); it++) { cout << *it << " "; } cout << endl; } int main() { //adjacent_find 查找相邻元素 LIST l; for(int i = 0 ; i < 10 ; i++) { l.push_back(i); l.push_back(i+1); l.push_back(i+1); l.push_back(i+2); } l.push_back(93); l.push_back(5); l.push_back(5); l.push_back(2); l.push_back(2); l.push_back(33); l.push_back(52); l.push_back(12); l.push_back(18); print(l); LIST::iterator it = adjacent_find(l.begin(),l.end()); if(it != l.end()) { cout << "两个相邻元素相等" << *it << " "; it++; cout << *it << endl; } list <int>::iterator ii = adjacent_find(l.begin(), l.end(), Equal); if(ii != l.end()) { cout <<"找出首个两个相邻元素相同"<< *ii << " "; ii++; cout << *ii << endl; } return 0; }
2、find_first_of查找第一个匹配字符串(不推荐使用,查看源代码采用最高复杂度的算法)
/* name : STL的其他用法:find_first_of writer : pprp declare : null date ; 2017/8/20 */ #include <bits/stdc++.h> using namespace std; int main() { char * s1 = "abcdefu7ghijklmn"; char * s2 = "zyx3yu7ys"; char * i = find_first_of(s1,s1 + strlen(s1),s2,s2 + strlen(s2)); //第一个出现在s2中的字符为 *i cout << * i << endl; return 0; }
3、堆排序(有点慢)
/* name : STL中的堆排序 writer : pprp declare : 复杂度为 nlogn date ; 2017/8/20 */ #include <bits/stdc++.h> #include <cstdio> #include <cstdlib> #include <ctime> #include <vector> using namespace std; void print(int x) { cout << x << " "; } int main() { vector<int> v; int tmp; //每次执行的种子不同,生成的随机数才不相同 srand((int)time(NULL)); //产生10个随机数,记录在vector中 for(int i = 0 ; i < 10 ; i++) { tmp = rand() % 100; v.push_back(tmp); } for_each(v.begin(), v.end(),print); cout << endl; make_heap(v.begin(),v.end()); sort_heap(v.begin(),v.end()); for_each(v.begin(),v.end(),print); cout << endl; return 0; }
4、归并算法(合并两个有序的序列)
/* name : 归并排序,合并两个有序序列 writer : pprp declare : 复杂度为 nlogn date ; 2017/8/20 */ #include <bits/stdc++.h> #include <cstdio> #include <cstdlib> #include <ctime> using namespace std; const int maxn = 100; int a[maxn]; int b[maxn]; void print(int *x,int n) { for(int i = 0 ; i < n ; i++) { cout << x[i] << " "; } cout << endl; } int main() { srand((int)time(NULL)); //产生10个随机数,记录在vector中 for(int i = 0 ; i < 6 ; i++) { a[i] = rand()%100; b[i] = rand()%100; } //升序 sort(a,a+6); sort(b,b+6); print(a,6); print(b,6); int result[maxn*2]; merge(a,a+6,b,b+6,result,less<int>()); print(result,12); //降序 sort(a,a+6,greater<int>()); sort(b,b+6,greater<int>()); merge(a,a+6,b,b+6,result,greater<int>()); print(result,12); return 0; }
5、binary_search折半查找(用在有序区间中)
bool binary_search(a,a+size,key)
6、includes判断集合包含关系
int a[5] = {0,2,5,12,54}; int b[10] = {12,32,34,54,6,34,54,23,2,24}; if(includes(a,a+5,b,b+5)) { cout << "yes" << endl; } else { cout << "no" << endl; }
7、最值
max(12,321); min(21,32); //查找线性容器中的最大最小 list <int> :: iterator it = min_element(l.begin(), l.end()); list <int> :: iterator it = max_element(l.begin(), l.end()); //字典序比较大小 lexicographical_compare(s1,s1+len1,s2,s2+len2);
8、组合数生成
/* name : STL中的堆排序 writer : pprp declare : 复杂度为 nlogn date ; 2017/8/20 */ #include <bits/stdc++.h> #include <cstdio> #include <cstdlib> #include <ctime> using namespace std; void print(int a[]) { for(int i = 0; i < 5 ; i++) { cout << a[i] << " "; } cout << endl; } int main() { int a[] = {3,5,6,7,9}; while(next_permutation(a,a+5)) { print(a); } cout << endl; while(prev_permutation(a,a+5)) { print(a); } return 0; }