代码改变世界

adjacent_find,equal

2012-07-29 15:47  java线程例子  阅读(253)  评论(0编辑  收藏  举报

代码如下:

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>

using namespace std;
int main(){
	int a[]={1,2,3,4,5,6,6,8,9,10};
	vector<int>v(a,a+10);
	vector<int>vv(a+3,a+8);
	
	vector<int>::iterator it;
	it=adjacent_find(v.begin(),v.end());//寻找两个连续且相等的元素,在这一点上,有一点像search_n(v.begin(),v.end(),2,X);
	if(it!=v.end()){
		cout<<"found the two number equals"<<endl;
		cout<<*it<<endl;
	}else{
		cout<<"none"<<endl;
	}
	system("pause");
	return 0;
}
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
bool liangbei(int a,int b){
	return 2*a==b;
}
using namespace std;
int main(){
	int a[]={1,2,3,4,5,6,6,8,9,10};
	vector<int>v(a,a+10);
	vector<int>vv(a+3,a+8);
	
	vector<int>::iterator it;
	it=adjacent_find(v.begin(),v.end(),liangbei);
	if(it!=v.end()){
		cout<<"找到了第一个符合条件的两个连续的元素,其中后一个是前一个元素的两倍"<<endl;
		cout<<*it<<" "<<*(it+1)<<endl;
	}else{
		cout<<"没有找到"<<endl;
	}
	system("pause");
	return 0;
}
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
int main(){
	int a[]={1,2,3,4,5,6,7,8,9,10};
	int b[]={2,3,4,5,6,7,8,9,10,11};
	vector<int>v(a,a+10);
	vector<int>vv(b,b+10);
	vector<int>vvv(a,a+10);
	if(equal(v.begin(),v.end(),vv.begin())){
		cout<<"v=vv"<<endl;
	}else{
		cout<<"v!=vv"<<endl;
	}
	if(equal(v.begin(),v.end(),vvv.begin())){
		cout<<"v=vvv"<<endl;
	}else{
		cout<<"v!=vvv"<<endl;
	}
	system("pause");
	return 0;
}
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
bool jianyi(int a,int b){
	return a==b-1;
}
int main(){
	int a[]={1,2,3,4,5,6,7,8,9,10};
	int b[]={2,3,4,5,6,7,8,9,10,11};
	vector<int>v(a,a+10);
	vector<int>vv(b,b+10);
	vector<int>vvv(a,a+10);
	
	if(equal(v.begin(),v.end(),vv.begin(),jianyi)){
		cout<<"vv中的每个元素减去一 与V的元素相等"<<endl;
	}else{
		cout<<"还是不相等"<<endl;
	}
	system("pause");
	return 0;
}