// #include <iostream>
// #include <algorithm>
// #include <vector>
// #include <list>
// #include <time.h>
// #include<numeric>
#include<bits/stdc++.h>//万能头文件
using namespace std;
void print(int x)
{
cout << x << " ";
}
bool ifgreater(int x)
{
return x > 3;
}
class demo
{
public:
bool operator()(int x, int y)
{
return x > y;
}
bool operator()(int x)
{
return x > 5;
}
};
template <typename myiterator>
myiterator Max_element(myiterator myfirst, myiterator myend)
{
if (myfirst == myend)
{
return myfirst;
}
myiterator temp = myfirst;
while (myfirst != myend)
{
if (myfirst > temp)
{
temp = myfirst;
}
myfirst++;
}
return temp;
}
template <typename iterator1, typename iterator2>
pair<iterator1, iterator2> Mismatch(iterator1 f1, iterator1 l1, iterator2 f2, iterator2 l2)
{
while (f1 != l1 && f2 != l2 && *f1 == *f2)
{
f1++;
f2++;
}
return pair<iterator1, iterator2>(f1, f2);
}
class demo2
{
public:
int operator()()
{
return rand() % 100 + 1;
}
};
int main()
{
// vector<int> v = {1,2,3,4,4,8};
// vector<int> v1 = {1,2,4};
// for_each(v.begin(),v.end(),print);//将区间内的元素放到print里面执行
// cout<<endl;
// cout<<count(v.begin(),v.end(),4)<<endl;//计算区间内元素个数
// cout<<count_if(v.begin(),v.end(),ifgreater)<<endl;//条件计算元素个数
// //vector<int>::iterator it = Max_element(v.begin(),v.end());
// auto it = Max_element(v.begin(),v.end());
// cout<<"最大值:"<<*it<<endl;
// cout<<*find_if(v.begin(),v.end(),ifgreater)<<endl;
// cout<<*search(v.begin(),v.end(),v1.begin(),v1.end())<<endl;//查找字串
// auto it_s = search_n(v.begin(),v.end(),2,4);//没有找到的话,迭代器是v.end
// if(it_s == v.end())
// {
// cout<<"没有找到"<<endl;
// }
// else
// {
// cout<<"找到了"<<endl;
// }
// it_s = find_first_of(v.begin(),v.end(),v1.begin(),v1.end());//找到v1元素第一个出现在v的位置
// cout<<*it_s<<endl;
// auto pair = Mismatch(v.begin(),v.end(),v1.begin(),v1.end());//查找第一个不匹配的位置
// cout<<*pair.first<<endl;
// cout<<*pair.second<<endl;
// v1.resize(10,0);
// for_each(v1.begin(),v1.end(),print);
// cout<<endl;
// transform(v.begin(),v.end(),v1.begin(),ifgreater);//将数列变动但是不会自动扩容,小心越界问题
// for_each(v1.begin(),v1.end(),print);
// cout<<endl;
// for_each(v.begin(),v.end(),print);
// cout<<endl;
vector<int> v1 = {1, 3, 5, 7, 9, 11};
vector<int> v2 = {2, 4, 6, 8, 10};
vector<int> v3;
for_each(v1.begin(), v1.end(), print);
cout << endl;
// v3.resize(v1.size() + v2.size());
// merge(v1.begin(),v1.end(),v2.begin(),v2.end(),v3.begin());//demo()合并序列;归并排序
// for_each(v3.begin(),v3.end(),print);
// swap_ranges(v1.begin(),v1.end(),v2.begin());//要扩容
// for_each(v1.begin(),v1.end(),print);
// for_each(v2.begin(),v2.end(),print);
// cout<<endl;
// fill(v1.begin(),v1.begin() + 3,99);//区间填充数据
// for_each(v1.begin(),v1.end(),print);
// cout<<endl;
// fill_n(v1.begin(),3,10);
// for_each(v1.begin(),v1.end(),print);
// cout<<endl;
// srand(time(NULL));
// generate(v1.begin(),v1.end(),demo2());
//random_shuffle(v1.begin(),v1.end());
// replace(v1.begin(),v1.end(),3,99);//替换
// replace_if(v1.begin(),v1.end(),demo(),100);//条件替换
// reverse(v1.begin(),v1.end());
// reverse_copy(v1.begin(),v1.end(),v2.begin());
//rotate(v1.begin(),v1.begin() + 2,v1.end());//前几个放后面
//partition(v1.begin(),v1.end(),demo());//符合条件放前面
//stable_partition(v1.begin(), v1.end(), demo()); //划分区域,相对位置不变
// for_each(v1.begin(), v1.end(), print);
// cout << endl;
// partial_sort(v1.begin(),v1.begin() +3,v1.end());//前几个排序
// nth_element(v1.begin(), v1.begin() + 4, v1.end()); //将第N个数据找到并放在应该在的位置
// make_heap(v1.begin(),v1.end());
// for_each(v1.begin(), v1.end(), print);
// cout << endl;
// for_each(v2.begin(), v2.end(), print);
// cout << endl;
//string s1 = "abcdhjdsjskfjkshfsfsf";
// string s2 = "adbhadhssdf";
// string s3;
// s3.resize(30);
// set_difference(s1.begin(),s1.end(),s2.begin(),s2.end(),s3.begin());//找到不相同的地方输出
// cout<<s3<<endl;
int sum = accumulate(v1.begin(),v1.end(),0);//求和
cout<<sum<<endl;
return 0;
}
C++ algorithm部分实现
最新推荐文章于 2023-03-01 12:48:55 发布