非变动性算法: 既不改变元素次序,也不改变元素值
For_each() | 对每个元素执行操作(也可做变动型算法) |
Count | 返回元素个数 |
Count_if | 返回满足条件的元素个数 |
Min_element | 返回最小元素,以一个迭代器表示 |
Max_element | 返回最大元素,以一个迭代器表示 |
Find | 搜索等于某一个值得第一个元素 |
Find_if | 搜索满足条件的第一元素 |
Search | 搜索某一区间第一次出现的位置 |
Search_n | 搜索具有某特性的第一段n个连续元素 |
Find_end | 搜索某个区间最后一次出现的元素 |
Find_first_of | 搜索等于某个值得第一元素 |
Adjacent_find | 搜索特定规则的元素 |
Equal | 判断两个区间是否相等 |
Mismatch | 返回两个序列的各组对应元素中,第一对不相等元素组 |
Lexicographical_compare | 判断某一序列字典顺序下是否小于另一序列 |
--
#include <iostream>
#include <vector>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
template<class T>
class FunctionObjectType
{
private:
T theValue;
public:
FunctionObjectType(const T& v):theValue(v)
{
}
public:
void operator()(T &i)
{
i += theValue;
}
};
/*--------------打印元素-------------*/
template<class T>
inline void PrintElements(const T& coll,const char* optcstr="")
{
typename T::const_iterator pos;
std::cout<<optcstr;
for(pos=coll.begin();pos != coll.end(); ++pos)
std::cout<<*pos<<" ";
std::cout<<endl;
}
template<class T>
inline void PrintElementsPos(const T& coll,const char* optcstr="")
{
typename T::const_iterator pos;
std::cout<<optcstr;
for(pos=coll.begin();pos != coll.end(); ++pos)
std::cout<<&*pos<<" ";
std::cout<<endl;
}
/*--------------插入int型数据------------------*/
template<class T>
inline void InsertElements(T& coll,int first,int last)
{
for(int i = first; i<=last; ++i)
coll.insert(coll.end(),i);
}
void print(int elem)
{
std::cout<< elem << " ";
}
//for_each对每个元素执行操作
void Learn_for_each()
{
std::cout<<endl<<"--------------------in Learn for_each()"<<endl;
vector<int> coll;
InsertElements(coll,1,9);
PrintElements(coll,"coll : ");
std::cout<<"for_each print : ";
for_each(coll.begin(),coll.end(),
print);
std::cout<<endl;
std::cout<<"每个元素与第一个元素求和"<<endl;
for_each(coll.begin(),coll.end(),
FunctionObjectType<int>(*coll.begin()));
PrintElements(coll,"coll : ");
std::cout<<endl;
}
bool isEven(int elem)
{
return elem%2 == 0;
}
void Learn_count()
{
std::cout<<endl<<"--------------------in Learn count()"<<endl;
vector<int> coll;
InsertElements(coll,1,9);
PrintElements(coll,"coll : ");
std::cout<<"值为2的个数 : ";
int num = 0;
num = count(coll.begin(),coll.end(),
2);
std::cout<<num<<endl;
std::cout<<"偶数的个数 : ";
num = 0;
num = count_if(coll.begin(),coll.end(),
isEven);
std::cout<<num<<endl;
}
bool absLess(int elem1,int elem2)
{
return abs(elem1) < abs(elem2);
}
void Learn_mxx_element()
{
std::cout<<endl<<"--------------------in Learn min_element() max_element()"<<endl;
vector<int> coll;
InsertElements(coll,1,9);
InsertElements(coll,-4,3);
PrintElements(coll,"coll : ");
std::cout<<"最小元素 : ";
std::cout<<*min_element(coll.begin(),coll.end())<<endl;
std::cout<<"最大元素 : ";
std::cout<<*max_element(coll.begin(),coll.end())<<endl;
std::cout<<"绝对值最小元素 : ";
std::cout<<*min_element(coll.begin(),coll.end(),absLess)<<endl;
std::cout<<"绝对值最大元素 : ";
std::cout<<*max_element(coll.begin(),coll.end(),absLess)<<endl;
std::cout<<endl;
}
template<class T>
class FunctionFindType
{
private:
T theValue;
public:
FunctionFindType(const T& v):theValue(v)
{
}
public:
bool operator()(T &i)
{
return theValue == abs(i);
}
};
void Learn_find()
{
std::cout<<endl<<"--------------------in Learn find() find_if() find_end() "<<endl;
vector<int> coll;
InsertElements(coll,1,9);
InsertElements(coll,-4,3);
PrintElements(coll,"coll : ");
PrintElementsPos(coll,"coll pos : ");
vector<int>::iterator pos;
std::cout<<"查找值为2的第一元素 : "<<endl;
pos = find(coll.begin(),coll.end(),
2);
std::cout<<"pos : "<<&*pos<<endl<<"value: "<<*pos<<endl;
std::cout<<"查找值为2的第二元素 : "<<endl;
pos++;
pos = find(pos,coll.end(),
2);
std::cout<<"pos : "<<&*pos<<endl<<"value: "<<*pos<<endl;
std::cout<<"查找绝对值相当的第一元素 : "<<endl;
pos = find_if(coll.begin(),coll.end(),
FunctionFindType<int>(2));
std::cout<<"pos : "<<&*pos<<endl<<"value: "<<*pos<<endl;
std::cout<<"查找绝对值相当的第二元素 : "<<endl;
pos++;
pos = find_if(pos,coll.end(),
FunctionFindType<int>(2));
std::cout<<"pos : "<<&*pos<<endl<<"value: "<<*pos<<endl;
}
bool checkEven(int elem,bool even)
{
if(even)
{
return elem % 2 == 0;//判断是不是偶数
}
else
{
return elem % 2 == 1;//判断是不是奇数
}
}
void Learn_search()
{
std::cout<<endl<<"--------------------in Learn search_n() search(),find_end()"<<endl;
vector<int> coll;
InsertElements(coll,1,1);
InsertElements(coll,1,1);
InsertElements(coll,-2,1);
InsertElements(coll,1,9);
InsertElements(coll,-4,3);
PrintElements(coll,"coll : ");
PrintElementsPos(coll,"coll pos : ");
vector<int>::iterator pos;
std::cout<<"查找第一个连续2个1的位置 : "<<endl;
pos = search_n(coll.begin(),coll.end(),
2,
1);
std::cout<<"pos : "<<&*pos<<endl;
std::cout<<"查找第二个连续2个1的位置 : "<<endl;
pos++;
pos = search_n(pos,coll.end(),
2,
1);
std::cout<<"pos : "<<&*pos<<endl;
vector<int> subcoll;
InsertElements(subcoll,1,3);
std::cout<<"查找第一次出现 1,2,3的位置 :"<<endl;
pos = search(coll.begin(),coll.end(),
subcoll.begin(),subcoll.end());
std::cout<<"pos : "<<&*pos<<" value: "<<*pos<<endl;
std::cout<<"查找第二次出现 1,2,3的位置 :"<<endl;
pos++;
pos = search(pos,coll.end(),
subcoll.begin(),subcoll.end());
std::cout<<"pos : "<<&*pos<<" value: "<<*pos<<endl;
std::cout<<"找出满足‘偶,奇,偶’条件的子序列位置 :"<<endl;
bool checkEvenArgs[3] = {true,false,true};
pos = search(coll.begin(),coll.end(),
checkEvenArgs,checkEvenArgs + 3,
checkEven);
while(pos != coll.end())
{
std::cout<<"pos : "<<&*pos<< " start value :"<<*pos <<endl;
pos = search(++pos,coll.end(),
checkEvenArgs,checkEvenArgs + 3,
checkEven);
}
std::cout<<"find_end查找最后一次出现 1,2,3的位置 :"<<endl;
pos = find_end(coll.begin(),coll.end(),
subcoll.begin(),subcoll.end());
std::cout<<"pos : "<<&*pos<<" value: "<<*pos<<endl;
std::cout<<"find_end查找倒数第二次出现 1,2,3的位置 :"<<endl;
pos = find_end(coll.begin(),pos,
subcoll.begin(),subcoll.end());
std::cout<<"pos : "<<&*pos<<" value: "<<*pos<<endl;
subcoll.clear();
InsertElements(subcoll,3,3);
InsertElements(subcoll,2,2);
PrintElements(coll,"coll : ");
PrintElementsPos(coll,"coll pos : ");
PrintElements(subcoll,"subcoll : ");
std::cout<<"find_first_of查找第一个既在coll也在subcoll中出现的元素在coll中的位置 :"<<endl;
pos = find_first_of(coll.begin(),coll.end(),
subcoll.begin(),subcoll.end());
std::cout<<"pos : "<<&*pos<<" value: "<<*pos<<endl;
std::cout<<"adjacent_find查找连续2个元素相等的第1个元素的位置 : "<<endl;
pos = adjacent_find(coll.begin(),coll.end());
std::cout<<"pos : "<<&*pos<<" value: "<<*pos<<endl;
}
void Learn_equal()
{
std::cout<<endl<<"--------------------in Learn equal() "<<endl;
vector<int> coll1;
vector<int> coll2;
vector<int> coll3;
InsertElements(coll1,1,9);
InsertElements(coll2,1,9);
InsertElements(coll3,1,4);
PrintElements(coll1,"coll 1 : ");
PrintElements(coll2,"coll 2 : ");
if(equal(coll1.begin(),coll1.end(),
coll2.begin()))
{
cout << "coll1 == coll2"<<endl;
}
else
{
cout<<"coll1 != coll2"<<endl;
}
PrintElements(coll1,"coll 1 : ");
PrintElements(coll3,"coll 3 : ");
if(equal(coll1.begin(),coll1.end(),
coll3.begin()))
{
cout << "coll1 == coll3"<<endl;
}
else
{
cout<<"coll1 != coll3"<<endl;
}
}
void Learn_mismatch()
{
std::cout<<endl<<"--------------------in Learn mismatch() "<<endl;
vector<int> coll1;
vector<int> coll2;
vector<int> coll3;
InsertElements(coll1,1,9);
InsertElements(coll2,1,3);
InsertElements(coll2,6,6);
PrintElements(coll1,"coll 1 : ");
PrintElements(coll2,"coll 2 : ");
pair<vector<int>::iterator,vector<int>::iterator> values;
std::cout<<"返回第一组两两相异的对应元素:"<<endl;
values = mismatch(coll1.begin(),coll1.end(),
coll2.begin());
if(values.first == coll1.end())
{
cout<<"no mismatch"<<endl;
}
else
{
cout <<"first mismatch : "
<<*values.first << " and "
<<*values.second <<endl;
}
}
int main()
{
cout<<"hello world"<<endl;
//--------------------算法--------begin
//for_each
Learn_for_each();
//count,count_if
Learn_count();
//min_element,max_element
Learn_mxx_element();
//find find
Learn_find();
//search
Learn_search();
//equal
Learn_equal();
//mismatch
Learn_mismatch();
cin.get();
return 0;
}
#include <vector>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
template<class T>
class FunctionObjectType
{
private:
T theValue;
public:
FunctionObjectType(const T& v):theValue(v)
{
}
public:
void operator()(T &i)
{
i += theValue;
}
};
/*--------------打印元素-------------*/
template<class T>
inline void PrintElements(const T& coll,const char* optcstr="")
{
typename T::const_iterator pos;
std::cout<<optcstr;
for(pos=coll.begin();pos != coll.end(); ++pos)
std::cout<<*pos<<" ";
std::cout<<endl;
}
template<class T>
inline void PrintElementsPos(const T& coll,const char* optcstr="")
{
typename T::const_iterator pos;
std::cout<<optcstr;
for(pos=coll.begin();pos != coll.end(); ++pos)
std::cout<<&*pos<<" ";
std::cout<<endl;
}
/*--------------插入int型数据------------------*/
template<class T>
inline void InsertElements(T& coll,int first,int last)
{
for(int i = first; i<=last; ++i)
coll.insert(coll.end(),i);
}
void print(int elem)
{
std::cout<< elem << " ";
}
//for_each对每个元素执行操作
void Learn_for_each()
{
std::cout<<endl<<"--------------------in Learn for_each()"<<endl;
vector<int> coll;
InsertElements(coll,1,9);
PrintElements(coll,"coll : ");
std::cout<<"for_each print : ";
for_each(coll.begin(),coll.end(),
print);
std::cout<<endl;
std::cout<<"每个元素与第一个元素求和"<<endl;
for_each(coll.begin(),coll.end(),
FunctionObjectType<int>(*coll.begin()));
PrintElements(coll,"coll : ");
std::cout<<endl;
}
bool isEven(int elem)
{
return elem%2 == 0;
}
void Learn_count()
{
std::cout<<endl<<"--------------------in Learn count()"<<endl;
vector<int> coll;
InsertElements(coll,1,9);
PrintElements(coll,"coll : ");
std::cout<<"值为2的个数 : ";
int num = 0;
num = count(coll.begin(),coll.end(),
2);
std::cout<<num<<endl;
std::cout<<"偶数的个数 : ";
num = 0;
num = count_if(coll.begin(),coll.end(),
isEven);
std::cout<<num<<endl;
}
bool absLess(int elem1,int elem2)
{
return abs(elem1) < abs(elem2);
}
void Learn_mxx_element()
{
std::cout<<endl<<"--------------------in Learn min_element() max_element()"<<endl;
vector<int> coll;
InsertElements(coll,1,9);
InsertElements(coll,-4,3);
PrintElements(coll,"coll : ");
std::cout<<"最小元素 : ";
std::cout<<*min_element(coll.begin(),coll.end())<<endl;
std::cout<<"最大元素 : ";
std::cout<<*max_element(coll.begin(),coll.end())<<endl;
std::cout<<"绝对值最小元素 : ";
std::cout<<*min_element(coll.begin(),coll.end(),absLess)<<endl;
std::cout<<"绝对值最大元素 : ";
std::cout<<*max_element(coll.begin(),coll.end(),absLess)<<endl;
std::cout<<endl;
}
template<class T>
class FunctionFindType
{
private:
T theValue;
public:
FunctionFindType(const T& v):theValue(v)
{
}
public:
bool operator()(T &i)
{
return theValue == abs(i);
}
};
void Learn_find()
{
std::cout<<endl<<"--------------------in Learn find() find_if() find_end() "<<endl;
vector<int> coll;
InsertElements(coll,1,9);
InsertElements(coll,-4,3);
PrintElements(coll,"coll : ");
PrintElementsPos(coll,"coll pos : ");
vector<int>::iterator pos;
std::cout<<"查找值为2的第一元素 : "<<endl;
pos = find(coll.begin(),coll.end(),
2);
std::cout<<"pos : "<<&*pos<<endl<<"value: "<<*pos<<endl;
std::cout<<"查找值为2的第二元素 : "<<endl;
pos++;
pos = find(pos,coll.end(),
2);
std::cout<<"pos : "<<&*pos<<endl<<"value: "<<*pos<<endl;
std::cout<<"查找绝对值相当的第一元素 : "<<endl;
pos = find_if(coll.begin(),coll.end(),
FunctionFindType<int>(2));
std::cout<<"pos : "<<&*pos<<endl<<"value: "<<*pos<<endl;
std::cout<<"查找绝对值相当的第二元素 : "<<endl;
pos++;
pos = find_if(pos,coll.end(),
FunctionFindType<int>(2));
std::cout<<"pos : "<<&*pos<<endl<<"value: "<<*pos<<endl;
}
bool checkEven(int elem,bool even)
{
if(even)
{
return elem % 2 == 0;//判断是不是偶数
}
else
{
return elem % 2 == 1;//判断是不是奇数
}
}
void Learn_search()
{
std::cout<<endl<<"--------------------in Learn search_n() search(),find_end()"<<endl;
vector<int> coll;
InsertElements(coll,1,1);
InsertElements(coll,1,1);
InsertElements(coll,-2,1);
InsertElements(coll,1,9);
InsertElements(coll,-4,3);
PrintElements(coll,"coll : ");
PrintElementsPos(coll,"coll pos : ");
vector<int>::iterator pos;
std::cout<<"查找第一个连续2个1的位置 : "<<endl;
pos = search_n(coll.begin(),coll.end(),
2,
1);
std::cout<<"pos : "<<&*pos<<endl;
std::cout<<"查找第二个连续2个1的位置 : "<<endl;
pos++;
pos = search_n(pos,coll.end(),
2,
1);
std::cout<<"pos : "<<&*pos<<endl;
vector<int> subcoll;
InsertElements(subcoll,1,3);
std::cout<<"查找第一次出现 1,2,3的位置 :"<<endl;
pos = search(coll.begin(),coll.end(),
subcoll.begin(),subcoll.end());
std::cout<<"pos : "<<&*pos<<" value: "<<*pos<<endl;
std::cout<<"查找第二次出现 1,2,3的位置 :"<<endl;
pos++;
pos = search(pos,coll.end(),
subcoll.begin(),subcoll.end());
std::cout<<"pos : "<<&*pos<<" value: "<<*pos<<endl;
std::cout<<"找出满足‘偶,奇,偶’条件的子序列位置 :"<<endl;
bool checkEvenArgs[3] = {true,false,true};
pos = search(coll.begin(),coll.end(),
checkEvenArgs,checkEvenArgs + 3,
checkEven);
while(pos != coll.end())
{
std::cout<<"pos : "<<&*pos<< " start value :"<<*pos <<endl;
pos = search(++pos,coll.end(),
checkEvenArgs,checkEvenArgs + 3,
checkEven);
}
std::cout<<"find_end查找最后一次出现 1,2,3的位置 :"<<endl;
pos = find_end(coll.begin(),coll.end(),
subcoll.begin(),subcoll.end());
std::cout<<"pos : "<<&*pos<<" value: "<<*pos<<endl;
std::cout<<"find_end查找倒数第二次出现 1,2,3的位置 :"<<endl;
pos = find_end(coll.begin(),pos,
subcoll.begin(),subcoll.end());
std::cout<<"pos : "<<&*pos<<" value: "<<*pos<<endl;
subcoll.clear();
InsertElements(subcoll,3,3);
InsertElements(subcoll,2,2);
PrintElements(coll,"coll : ");
PrintElementsPos(coll,"coll pos : ");
PrintElements(subcoll,"subcoll : ");
std::cout<<"find_first_of查找第一个既在coll也在subcoll中出现的元素在coll中的位置 :"<<endl;
pos = find_first_of(coll.begin(),coll.end(),
subcoll.begin(),subcoll.end());
std::cout<<"pos : "<<&*pos<<" value: "<<*pos<<endl;
std::cout<<"adjacent_find查找连续2个元素相等的第1个元素的位置 : "<<endl;
pos = adjacent_find(coll.begin(),coll.end());
std::cout<<"pos : "<<&*pos<<" value: "<<*pos<<endl;
}
void Learn_equal()
{
std::cout<<endl<<"--------------------in Learn equal() "<<endl;
vector<int> coll1;
vector<int> coll2;
vector<int> coll3;
InsertElements(coll1,1,9);
InsertElements(coll2,1,9);
InsertElements(coll3,1,4);
PrintElements(coll1,"coll 1 : ");
PrintElements(coll2,"coll 2 : ");
if(equal(coll1.begin(),coll1.end(),
coll2.begin()))
{
cout << "coll1 == coll2"<<endl;
}
else
{
cout<<"coll1 != coll2"<<endl;
}
PrintElements(coll1,"coll 1 : ");
PrintElements(coll3,"coll 3 : ");
if(equal(coll1.begin(),coll1.end(),
coll3.begin()))
{
cout << "coll1 == coll3"<<endl;
}
else
{
cout<<"coll1 != coll3"<<endl;
}
}
void Learn_mismatch()
{
std::cout<<endl<<"--------------------in Learn mismatch() "<<endl;
vector<int> coll1;
vector<int> coll2;
vector<int> coll3;
InsertElements(coll1,1,9);
InsertElements(coll2,1,3);
InsertElements(coll2,6,6);
PrintElements(coll1,"coll 1 : ");
PrintElements(coll2,"coll 2 : ");
pair<vector<int>::iterator,vector<int>::iterator> values;
std::cout<<"返回第一组两两相异的对应元素:"<<endl;
values = mismatch(coll1.begin(),coll1.end(),
coll2.begin());
if(values.first == coll1.end())
{
cout<<"no mismatch"<<endl;
}
else
{
cout <<"first mismatch : "
<<*values.first << " and "
<<*values.second <<endl;
}
}
int main()
{
cout<<"hello world"<<endl;
//--------------------算法--------begin
//for_each
Learn_for_each();
//count,count_if
Learn_count();
//min_element,max_element
Learn_mxx_element();
//find find
Learn_find();
//search
Learn_search();
//equal
Learn_equal();
//mismatch
Learn_mismatch();
cin.get();
return 0;
}