C++ STL 计算问题

时间:2022-02-16 13:55:55

#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
#include <list>
#include <set>
#include <cstdlib>
using namespace std;

inline bool eq_nocase(char c1,char c2)
{
 return toupper(c1)==toupper(c2);
}

inline bool eq_int(int a1,int a2)
{
 return a1==a2;
}

int main()
{
 int A1[]={3,1,4,1,5,9,2};
 int A2[]={3,1,4,1,5,9,2};

 const int N=sizeof(A1)/sizeof(int);

 if(equal(A1,A1+N,A2))
  cout<<"Equal"<<endl;
 else
  cout<<"Not equal"<<endl;
 //////////////////////////////////////////////////////////
 const char* s1="This is a Test";
 const char* s2="This is a Test";
 const int M=strlen(s1);

 if(equal(s1,s1+M,s2,eq_nocase))//带条件的比较,equal函数自己调用多次
  cout<<"equal"<<endl;
 else
  cout<<"Not equal"<<endl;

 int a[]={1,2,3};
 int b[]={1,2,3};
 const int M2=sizeof(a)/sizeof(int);

 if(equal(a,a+M2,b,eq_int)) //带条件的比较,equal函数自己调用多次
  cout<<"int equal"<<endl;
 else
  cout<<"int not equal"<<endl;

 ///////////////////////mismatch/////////////////////////////////////////
 pair<int*,int*> resu=mismatch(A1,A1+N,A2);
 if(resu.first==A1+N)
  cout<<"not mismatch"<<endl;
 else
  cout<<"first mismatch:"<<resu.first-A1<<endl;
 cout<<"Value:"<<*(resu.first)<<","<<*(resu.second)<<endl;

 //////////////////////////lexicograhical_compare()/////////
 int B1[]={3,1,4,1,5,9,3};
 int B2[]={3,1,4,2,5,9,3};
 int B3[]={1,2,3,4};
 int B4[]={1,2,3,4,5,5};

 const int N1=sizeof(B1)/sizeof(int);
 const int N2=sizeof(B2)/sizeof(int);
 const int N3=sizeof(B3)/sizeof(int);
 const int N4=sizeof(B4)/sizeof(int);

 bool c1=(bool)lexicographical_compare(B1,B1+N1,B2,B2+N2);
 bool c2=(bool)lexicographical_compare(B4,B4+N2,B3,B3+N1);

 cout<<c1<<" "<<c2<<endl;

 return 0;
}