Stl
#include<bits/stdc++.h> #include <algorithm> #define f(a,b) for(int i=a;i<=b;i++) #define pb(a) push_back(a) using namespace std; vector<int>ap; int cmp(int i){ return i%2==0; } int a[10]= {1,2,3,23,4,4,45,7,21,9}; int main() { f(0,9)ap.pb(a[i]); cout<<*adjacent_find(a,a+9)<<endl; sort(a,a+10); sort(ap.begin(),ap.end()); f(0,9) { cout<<a[i]<<' '; } cout<<endl; f(0,9) { cout<<&a[i]<<' '; } cout<<endl; cout<<*adjacent_find(ap.begin(),ap.end()-1)<<endl; f(0,9) { cout<<ap[i]<<' '; } cout<<endl; f(0,9) { cout<<&ap[i]<<' '; } cout<<endl; // binary_search:查找某个元素是否出现 cout<<"binary_search(a,a+10,9):"<<binary_search(a,a+10,9)<<endl;//binary_search(arr[],arr[]+size , indx); cout<<"binary_search(ap.begin(),ap.end(),9):"<<binary_search(ap.begin(),ap.end(),9)<<endl;//binary_search(arr[],arr[]+size , indx); int b[20]; memset(b,0,sizeof(b)); //copy 函数 copy(a,a+10,b); f(0,9)cout<<b[i]<<' '; cout<<endl; copy(b,b+5,b+6); f(0,15)cout<<b[i]<<' '; cout<<endl; f(0,9)cout<<a[i]<<' '; cout<<endl; //copy_backward函数 区别 在于 (b+10还是b) memset(b,0,sizeof(b)); copy_backward(a,a+10,b+10); f(0,15)cout<<b[i]<<' '; cout<<endl; // count_if()函数 cout<<"ap中的偶数有"<<count_if(ap.begin(),ap.end(),cmp)<<endl; cout<<"a中的偶数有"<<count_if(a,a+10,cmp)<<endl; //count 计数函数 cout<<"a中的4的个数有"<<count(a,a+10,4)<<endl; }
用来查找数组中是否存在相邻间的元素相同 相同就返回第一个相同元素的地址 否则返回最后一个元素的值