【C++】 STL – 算法(二)
文章目录
- 【C++】 STL -- 算法(二)
- 前言
- 一、常用遍历算法
- 二、常用查找算法
- 三、常用排序算法
- 四、常用拷贝和替换算法
- 五、常用算数生成算法
- 六、常用集合算法
- 总结
前言
本片文章将讲到常用遍历算法,常用查找算法,常用排序算法,常用拷贝和替换算法,常用算数生成算法,常用集合算法。
一、常用遍历算法
for_each用于遍历
//有返回值的
for_each可以绑定参数输出
class MyPrint
{
public:void operator()(int val){cout << val << endl;m_Count++;}int m_Count = 0;
};//for_each 用于遍历
//有返回值的
void test01()
{vector<int>v;for (int i = 0; i < 10;i++){v.push_back(i);}MyPrint print = for_each(v.begin(), v.end(), MyPrint());cout << "print.count = " << print.m_Count << endl;}//for_each可以绑定参数输出
class MyPrint2 :public binary_function<int,int,void>
{
public:void operator()(int val , int start) const{cout << val << endl;}};void test02()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}for_each(v.begin(), v.end(), bind2nd( MyPrint2(), 1000));}
transform算法 将指定容器区间元素搬运到另一容器中
class MyTransform
{
public:int operator()(int val){ return val + 10000;}
};
void test03()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}vector<int>v2;v2.resize(v.size());transform(v.begin(), v.end(), v2.begin(), MyTransform());for_each(v2.begin(), v2.end(), [](int val){cout << val << " "; });}
二、常用查找算法
find算法 查找元素
@param beg 容器开始迭代器
@param end 容器结束迭代器
@param value 查找的元素
@return 返回查找元素的位置
find_if算法 条件查找
@param beg 容器开始迭代器
@param end 容器结束迭代器
@param callback 回调函数或者谓词(返回bool类型的函数对象)
@return bool 查找返回true 否则false
void test01()
{vector<int>v;for (int i = 0; i < 10;i++){v.push_back(i);}vector<int>::iterator pos = find(v.begin(), v.end(), 5);if (pos != v.end()){cout << "找到了元素:" << *pos << endl;}else{cout << "未找到" << endl;}
}
class Person
{
public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}bool operator==(const Person & p){return this->m_Name == p.m_Name && this->m_Age == p.m_Age;}string m_Name;int m_Age;
};void test02()
{vector<Person> v;Person p1("aaa", 10);Person p2("bbb", 20);Person p3("ccc", 30);Person p4("ddd", 40);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);vector<Person>::iterator pos = find(v.begin(), v.end(), p2);if (pos != v.end()){cout << "找到了元素 姓名: " << (*pos).m_Name << " 年龄: " << (*pos).m_Age << endl;}}class MyComparePerson :public binary_function< Person *, Person *, bool>
{
public:bool operator()( Person * p1 , Person *p2 ) const{return p1->m_Name == p2->m_Name && p1->m_Age == p2->m_Age; }
};void test03()
{vector<Person *> v;Person p1("aaa", 10);Person p2("bbb", 20);Person p3("ccc", 30);Person p4("ddd", 40);v.push_back(&p1);v.push_back(&p2);v.push_back(&p3);v.push_back(&p4);Person * p = new Person("bbb", 20);vector<Person *>::iterator pos = find_if(v.begin(), v.end(), bind2nd( MyComparePerson() ,p) );if (pos != v.end()){cout << "找到了元素--- 姓名: " << (*pos)->m_Name << " 年龄: " << (*pos)->m_Age << endl;}else{cout << "未找到" << endl;}
}
adjacent_find算法 查找相邻重复元素
void test04()
{vector<int>v;v.push_back(3);v.push_back(2);v.push_back(300);v.push_back(300);v.push_back(6);v.push_back(3);vector<int>::iterator ret = adjacent_find(v.begin(), v.end());if (ret != v.end()){cout << "找到了相邻的重复元素: " << *ret << endl;}else{cout << "未找到" << endl;}}
binary_search算法 二分查找法
注意: 在无序序列中不可用
void test05()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}//v.push_back(4); 必须是有序序列,如果无效 结果未知bool ret=binary_search(v.begin(), v.end(), 2);if (ret){cout << "查到了数据2" << endl;}else{cout << "未找到数据2" << endl;}}
count算法 统计元素出现次数
@param beg 容器开始迭代器
@param end 容器结束迭代器
@param value回调函数或者谓词(返回bool类型的函数对象)
@return int返回元素个数
class GreaterThan3
{
public:bool operator()(int val){return val >= 3;}
};void test06()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}v.push_back(3);v.push_back(3);v.push_back(3);int num = count(v.begin(), v.end(), 3);cout << "3的个数为: " << num << endl;//统计大于等于3的个数num = count_if(v.begin(), v.end(), GreaterThan3());// 0 1 2 3 4 5 6 7 8 9 3 3 3 cout << "大于等于3的个数为: " << num << endl;}
三、常用排序算法
merge算法 容器元素合并,并存储到另一容器中
注意 : 两个容器必须是有序的,顺序要一致
void test01()
{vector<int>v1;vector<int>v2;for (int i = 0; i < 10;i++){v1.push_back(i);v2.push_back(i + 1);}vector<int>vTarget; //目标容器vTarget.resize(v1.size() + v2.size());merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());for_each(vTarget.begin(), vTarget.end(), [](int val){cout << val << " "; });}
sort
void test02()
{vector<int>v;for (int i = 0; i < 10;i++){v.push_back(i);}//降序排序sort(v.begin(), v.end(), greater<int>());for_each(v.begin(), v.end(), [](int val){cout << val << " "; });cout << endl;
}
random_shuffle算法 对指定范围内的元素随机调整次序
void test03()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}random_shuffle(v.begin(), v.end());for_each(v.begin(), v.end(), [](int val){cout << val << " "; });cout << endl;
}
reverse算法 反转指定范围的元素
void test04()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}cout << "反转前打印:" << endl;for_each(v.begin(), v.end(), [](int val){cout << val << " "; });cout << endl;reverse(v.begin(), v.end());cout << "反转后打印: " << endl;for_each(v.begin(), v.end(), [](int val){cout << val << " "; });cout << endl;
}
四、常用拷贝和替换算法
copy算法 将容器内指定范围的元素拷贝到另一容器中
void test01()
{vector<int>v;for (int i = 0; i < 10;i++){v.push_back(i);}vector<int>v2;v2.resize(v.size());copy(v.begin(), v.end(), v2.begin());//for_each(v2.begin(), v2.end(), [](int val){cout << val << " "; });//cout << endl;copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));cout << endl;
}
replace算法 将容器内指定范围的旧元素修改为新元素
replace_if(iterator beg, iterator end, _callback, newvalue) 按条件替换
class MyReplace
{
public:bool operator()(int val){return val > 3;}
};
void test02()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}//将容器中的3替换为 3000replace(v.begin(), v.end(), 3, 3000);copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));cout << endl;//将容器中所有大于3的都替换为 30000;replace_if(v.begin(), v.end(), MyReplace() , 30000);// 0 1 2 30000 ...copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));cout << endl;
}//swap交换
void test03()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}vector<int>v2(10, 100);cout << "交换数据前:" << endl;copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));cout << endl;copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));cout << endl;cout << "交换数据后:" << endl;swap(v, v2);copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));cout << endl;copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));cout << endl;}
五、常用算数生成算法
#include //accumulate算法头文件
//accumulate算法 计算容器元素累计总和
void test01()
{vector<int>v;for (int i = 0; i <= 100;i++){v.push_back(i);}int num = accumulate(v.begin(), v.end(),1000); // 参数3代表 累加起始值cout << "num = " << num << endl;
}
fill算法 向容器中添加元素
void test02()
{vector<int>v;v.resize(10);fill(v.begin(), v.end(), 100);for_each(v.begin(), v.end(), [](int val){cout << val << " "; });cout << endl;}
六、常用集合算法
set_intersection算法 求两个set集合的交集
注意:两个集合必须是有序序列
@param beg1 容器1开始迭代器
@param end1 容器1结束迭代器
@param beg2 容器2开始迭代器
@param end2 容器2结束迭代器
@param dest 目标容器开始迭代器
@return 目标容器的最后一个元素的迭代器地址
void test01()
{vector<int>v1;vector<int>v2;for (int i = 0; i < 10;i++){v1.push_back(i);v2.push_back(i + 5);}vector<int>vTarget;vTarget.resize(min(v1.size(), v2.size()));vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());for_each(vTarget.begin(), itEnd, [](int val){cout << val << " "; });cout << endl;
}
set_union算法 求两个set集合的并集
注意:两个集合必须是有序序列
@param beg1 容器1开始迭代器
@param end1 容器1结束迭代器
@param beg2 容器2开始迭代器
@param end2 容器2结束迭代器
@param dest 目标容器开始迭代器
@return 目标容器的最后一个元素的迭代器地址
void test02()
{vector<int>v1;vector<int>v2;for (int i = 0; i < 10; i++){v1.push_back(i);v2.push_back(i + 5);}vector<int>vTarget;vTarget.resize(v1.size() + v2.size());vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());for_each(vTarget.begin(), itEnd , [](int val){cout << val << " "; });cout << endl;
}
set_difference算法 求两个set集合的差集
注意:两个集合必须是有序序列
@param beg1 容器1开始迭代器
@param end1 容器1结束迭代器
@param beg2 容器2开始迭代器
@param end2 容器2结束迭代器
@param dest 目标容器开始迭代器
@return 目标容器的最后一个元素的迭代器地址
void test03()
{vector<int>v1;vector<int>v2;for (int i = 0; i < 10; i++){v1.push_back(i);v2.push_back(i + 5);}vector<int>vTarget;vTarget.resize( max(v1.size(),v2.size()) );// A 与 B 差集//vector<int>::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());// B 与 A 差集vector<int>::iterator itEnd = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vTarget.begin());for_each(vTarget.begin(), itEnd, [](int val){cout << val << " "; });cout << endl;}
总结
到这里这篇文章的内容就结束了,谢谢大家的观看,如果有好的建议可以留言喔,谢谢大家啦!