非修改性序列算法
非修改性序列算法不会改变容器中的元素值,也不会改变元素的次序。
void OutToScreen(double& Fli) {
cout.width(1);
cout.precision(3);
cout << fixed<<Fli << " ";
}
template<class T>void FillValue(T& vec, int first, int last) {
if (last > first) {
for(int i=first;i<last;i++)
vec.insert(vec.end(), i);
}
else
{
cout << "错误!!" << endl;
}
}
bool relationship(double t1, double t2) {
double temp = 2 * t1;
return(t2 == temp);
}
bool AbsLess(double t1, double t2) {
return(abs(t1) < abs(t2));
}
bool isEven(int elem) {
return(elem % 2 == 0);
}
#pragma region 非修改性序列算法
//for_each的使用方法
vector<double>myvector;
FillValue(myvector, 0, 10);
for_each(myvector.begin(), myvector.end(), OutToScreen);
cout << endl;
for_each(myvector.begin(), myvector.end(), Multiple<double>(2));//仿函数排序
for_each(myvector.begin(), myvector.end(), OutToScreen);
cout << endl;
double sum = for_each(myvector.begin(), myvector.end(), SUM());//仿函数求和
cout << "sum=" << sum << endl;
//count统计数目使用方法
bool isEven(int elem);
int ct = count(myvector.begin(), myvector.end(), 2);//2的个数
cout << "2的个数为:" << ct << endl;
ct = count_if(myvector.begin(), myvector.end(),isEven);
cout << "偶数的个数为:" << ct << endl;
ct = count_if(myvector.begin(), myvector.end(), bind2nd(greater<double>(),2));
cout << "大于的个数为:" << ct << endl;
//最大最小值算法
bool AbsLess(double t1, double t2);
cout << "min=" << *min_element(myvector.begin(), myvector.end()) << endl;
cout << "max=" << *max_element(myvector.begin(), myvector.end()) << endl;
cout << "absmin=" << *min_element(myvector.begin(), myvector.end(),AbsLess) << endl;
cout << "absmax=" << *max_element(myvector.begin(), myvector.end(), AbsLess) << endl;
//pair<vector<double>::iterator, vector<double>::iterator>k;
auto k=minmax_element(myvector.begin(), myvector.end());
cout <<"min=" << *k.first << endl;
cout << "max=" << *k.second << endl;
//搜索算法
myvector.push_back(3);
myvector.push_back(5);
myvector.push_back(6);
myvector.push_back(7);
myvector.push_back(8);
myvector.push_back(2);
myvector.push_back(2);
myvector.push_back(5);
for_each(myvector.begin(), myvector.end(), OutToScreen);
cout << endl;
vector<double>::iterator pos1;
pos1 = find(myvector.begin(), myvector.end(), 3);
cout << "第一个3的位置:" << *pos1 << endl;
pos1 = find_if(myvector.begin(), myvector.end(), bind2nd(greater<int>(), 3));
cout << "第一个大于3的元素的位置:" << distance(myvector.begin(), pos1) << endl;
pos1 = search_n(myvector.begin(), myvector.end(), 4, 3, greater<int>());
if (pos1 != myvector.end()) {
cout << "连续4个大于3的元素的起始位置:" << distance(myvector.begin(), pos1) << endl;
}
else
{
cout << "没有合适的元素" << endl;
}
vector<double>myvector1;
myvector1.push_back(5);
myvector1.push_back(6);
myvector1.push_back(7);
myvector1.push_back(8);
for_each(myvector1.begin(), myvector1.end(), OutToScreen);
cout << endl;
pos1 = search(myvector.begin(), myvector.end(), myvector1.begin(), myvector1.end());
cout << "myvector1在myvector中的第一个的起始位置:" << distance(myvector.begin(), pos1) << endl;
pos1 = find_end(myvector.begin(), myvector.end(), myvector1.begin(), myvector1.end());
cout << "myvector1在myvector中的最后一个序列的起始位置:" << distance(myvector.begin(), pos1) << endl;
pos1 = find_first_of(myvector.begin(), myvector.end(), myvector1.begin(), myvector1.end());
cout << "myvector1在myvector中的第一个的起始位置:" << distance(myvector.begin(), pos1) << endl;
vector<double>::reverse_iterator pos2;//反向迭代器
pos2 = find_first_of(myvector.rbegin(), myvector.rend(), myvector1.begin(), myvector1.end());
cout << "myvector1在myvector中的最后一个序列的起始位置:" << distance(myvector.begin(), pos2.base()) << endl;
pos1 = adjacent_find(myvector.begin(), myvector.end());
cout << "第一组相等元素的起始位置:" << distance(myvector.begin(), pos1) << endl;
//区间比较算法
bool relationship(double t1, double t2);
list<double>L1, L2,L3;
cout << "L1" << endl;
L1.push_back(1);
L1.push_back(2);
L1.push_back(3);
L1.push_back(4);
L1.push_back(5);
for_each(L1.begin(), L1.end(), OutToScreen);
cout << endl;
cout << "L2" << endl;
L2.push_back(2);
L2.push_back(4);
L2.push_back(6);
L2.push_back(8);
L2.push_back(10);
for_each(L2.begin(), L2.end(), OutToScreen);
cout << endl;
cout << "L3" << endl;
L3.push_back(1);
L3.push_back(2);
L3.push_back(4);
L3.push_back(0);
L3.push_back(8);
for_each(L3.begin(), L3.end(), OutToScreen);
cout << endl;
bool equ = equal(L1.begin(),L1.end(),L2.begin());
if (equ) {
cout << "L1==L2" << endl;
}
else
{
cout << "L1!=L2" << endl;
}
equ = equal(L1.begin(), L1.end(), L2.begin(),greater<double>());
if (equ) {
cout << "L1>L2" << endl;
}
else
{
cout << "L1<L2" << endl;
}
//equ = equal(L1.begin(), L1.end(), L2.begin(),relationship);//无法运行
//if (equ) {
// cout << "L2=2*L1" << endl;
//}
//else
//{
// cout << "L2!=2*L1" << endl;
//}
pair<list<double>::iterator, list<double>::iterator>p1;
p1 = mismatch(L1.begin(), L1.end(), L3.begin(), L3.end());//不相等的地方
cout << *p1.first << " " << *p1.second << endl;
p1 = mismatch(L1.begin(), L1.end(), L3.begin(), L3.end(),less_equal<double>());//不小于的地方
cout << *p1.first << " " << *p1.second << endl;
bool b = lexicographical_compare(L1.begin(), L1.end(), L3.begin(), L3.end());//返回是bool
if (b) {
cout << "L1<L3" << endl;//依据第一个不相等的地方进行判断
}
else
{
cout << "L1>L3" << endl;
}
#pragma endregion