网站开发微信支付详细教程,昆山建设工程安监站网站,百度排行榜前十名,彩票网站建设策划书文章目录 算法概述常用遍历算法for_each——实现遍历容器函数原型示例 transform——搬运容器到另一个容器中函数原型注意示例 常用查找算法find——查找指定元素函数原型示例 find_if—— 查找符合条件的元素函数原型示例 adjacent_find——查找相邻重复元素函数原型示例 bina… 文章目录 算法概述常用遍历算法for_each——实现遍历容器函数原型示例 transform——搬运容器到另一个容器中函数原型注意示例 常用查找算法find——查找指定元素函数原型示例 find_if—— 查找符合条件的元素函数原型示例 adjacent_find——查找相邻重复元素函数原型示例 binary_search——查找指定元素是否存在函数原型示例 count——统计元素个数函数原型示例 count_if——统计符合条件的元素个数函数原型示例 算法概述
算法主要是由头文件algorithm functional numeric组成。algorithm头文件是STL中最大的头文件之一它提供了一系列的算法操作包括比较、交换、查找、遍历、复制、修改等等。 排序算法sort()、stable_sort()、partial_sort()等。 查找算法find()、find_if()、binary_search()等。 数值算法accumulate()、inner_product()、adjacent_difference()等。 复制和移动算法copy()、move()、copy_if()等。 修改算法transform()、replace()、fill()、generate()等。 遍历算法for_each()、count()、count_if()等。 合并和重排算法merge()、reverse()、rotate()等。 numeric头文件相对较小主要包括一些简单的数学运算函数。这些函数通常应用于序列上例如数组或容器。 累加算法accumulate()实现对序列中元素进行累加求和。 内积算法inner_product()计算两个序列的内积。 部分和算法partial_sum()计算序列的部分和。 邻接差算法adjacent_difference()计算序列每对相邻元素的差值。 乘积算法reduce()计算序列中元素的乘积。 functional头文件定义了一些模板类用于声明函数对象。函数对象是一种类对象可以像函数一样调用。 算术类plus、minus、multiplies、divides等。 比较类equal_to、greater、less、not_equal_to等。 逻辑类logical_and、logical_or、logical_not等。 一元函数适配器negate、bind1st、bind2nd等。 二元函数适配器not1、not2、compose1、compose2等。 这些函数对象类可以与算法和其他STL组件一起使用以提供更灵活和可定制的功能。 常用遍历算法
for_each——实现遍历容器 实际开发中是最常用的遍历算法 函数原型
for_each(iterator beg, iterator end, _func);
参数说明
beg要遍历的容器的起始迭代器。end要遍历的容器的结束迭代器指向容器中最后一个元素的下一个位置。_func要在每个元素上执行的函数或函数对象。该函数或函数对象对传入的元素进行处理。
示例
#include iostream
#include vector
#include algorithmvoid printNum(int num) {std::cout num ;
}int main() {std::vectorint nums {1, 2, 3, 4, 5};// 使用 for_each() 遍历容器中的元素并调用 printNum() 函数输出每个元素std::for_each(nums.begin(), nums.end(), printNum);// 输出结果: 1 2 3 4 5return 0;
}
transform——搬运容器到另一个容器中 transform() 函数会遍历源容器中的元素并将每个元素传递给 _func 进行转换操作然后将转换结果存储到目标容器中。 函数原型
transform(iterator beg1, iterator end1, iterator beg2, _func);
//beg1 源容器开始迭代器
//end1 源容器结束迭代器
//beg2 目标容器开始迭代器
//_func 函数或者函数对象
注意 搬运的目标容器必须要提前开辟空间否则无法正常搬运 示例
#include iostream
#include vector
#include algorithmint square(int x) {return x * x;
}int main() {std::vectorint nums {1, 2, 3, 4, 5};std::vectorint squares(nums.size()); // 目标容器// 使用 transform() 算法将 nums 中的每个元素平方并将结果存储到 squares 容器中std::transform(nums.begin(), nums.end(), squares.begin(), square);// 输出转换后的结果for (int num : squares) {std::cout num ;}// 输出结果: 1 4 9 16 25return 0;
}
常用查找算法
算法简介
find //查找元素find_if //按条件查找元素adjacent_find //查找相邻重复元素binary_search //二分查找法count //统计元素个数count_if //按条件统计元素个数
find——查找指定元素 按值查找元素找到返回指定元素的迭代器找不到返回结束迭代器end() 函数原型 find(iterator beg, iterator end, value); // beg 开始迭代器 // end 结束迭代器 // value 查找的元素
示例
#include iostream
#include vector
#include algorithmint main() {std::vectorint nums {1, 2, 3, 4, 5};// 使用 find() 算法在 nums 容器中查找元素值为 3 的元素auto it std::find(nums.begin(), nums.end(), 3);// 判断是否找到了匹配的元素if (it nums.end() *it num) {std::cout 未找到元素值为 3 的元素 std::endl;} else {std::cout 找到了元素值为 3 的元素 std::endl;}return 0;
}
在上面的例子中我们定义了一个 nums 容器并使用 find() 算法在容器中查找值为 3 的元素。
如果找到了匹配的元素find() 函数将返回指向该元素的迭代器并将其赋值给变量 it。我们可以通过判断 it 是否等于容器的结束迭代器 nums.end() 来确定是否找到了匹配的元素。
最后根据判断结果输出相应的信息。
如果在上述示例中nums 容器中存在值为 3 的元素那么输出将是 “找到了元素值为 3 的元素”如果不存在值为 3 的元素输出将是 “未找到元素值为 3 的元素”。 find_if—— 查找符合条件的元素
函数原型 find_if(iterator beg, iterator end, _Pred); // 按值查找元素找到返回指定位置迭代器找不到返回结束迭代器位置 // beg 开始迭代器 // end 结束迭代器 // _Pred 函数或者谓词返回bool类型的仿函数(用于确定元素是否满足条件的谓词函数对象。)
示例
#include algorithm
#include vector
#include string
//内置数据类型
class GreaterFive
{
public:bool operator()(int val){return val 5;}
};void test01() {vectorint v;for (int i 0; i 10; i) {v.push_back(i 1);}vectorint::iterator it find_if(v.begin(), v.end(), GreaterFive());if (it v.end()) {cout 没有找到! endl;}else {cout 找到大于5的数字: *it endl;}
}//自定义数据类型
class Person {
public:Person(string name, int age){this-m_Name name;this-m_Age age;}
public:string m_Name;int m_Age;
};class Greater20
{
public:bool operator()(Person p){return p.m_Age 20;}};void test02() {vectorPerson 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);vectorPerson::iterator it find_if(v.begin(), v.end(), Greater20());if (it v.end()){cout 没有找到! endl;}else{cout 找到姓名: it-m_Name 年龄: it-m_Age endl;}
}int main() {//test01();test02();system(pause);return 0;
}adjacent_find——查找相邻重复元素
函数原型 adjacent_find(iterator beg, iterator end); // 查找相邻重复元素,返回相邻元素的第一个位置的迭代器 // 找不到则返回最后一个位置的迭代器 // beg 开始迭代器 // end 结束迭代器
注意
面试题中如果出现查找相邻重复元素记得用STL中的adjacent_find算法
示例
#include algorithm
#include vectorvoid test01()
{vectorint v;v.push_back(1);v.push_back(2);v.push_back(5);v.push_back(2);v.push_back(4);v.push_back(4);v.push_back(3);//查找相邻重复元素vectorint::iterator it adjacent_find(v.begin(), v.end());if (it v.end()) {cout 找不到! endl;}else {cout 找到相邻重复元素为: *it endl;}
}binary_search——查找指定元素是否存在
函数原型 bool binary_search(iterator beg, iterator end, value); // 查找指定的元素查到 返回true 否则false // 注意: 在无序序列中不可用 // beg 开始迭代器 // end 结束迭代器 // value 查找的元素
配合二分查找法查找效率很高值得注意的是查找的容器中元素必须的有序序列
示例
#include algorithm
#include vectorvoid test01()
{vectorintv;for (int i 0; i 10; i){v.push_back(i);}//二分查找bool ret binary_search(v.begin(), v.end(),2);if (ret){cout 找到了 endl;}else{cout 未找到 endl;}
}int main() {test01();system(pause);return 0;
}count——统计元素个数
函数原型 count(iterator beg, iterator end, value); // 统计元素出现次数 // beg 开始迭代器 // end 结束迭代器 // value 统计的元素
注意 统计自定义数据类型时候需要配合重载 operator
示例
#include iostream
#include algorithm
#include vectorstruct Person {std::string name;int age;// 重载 operator 运算符bool operator(const Person other) const {return name other.name age other.age;}
};int main() {std::vectorPerson people {{Alice, 25},{Bob, 30},{Alice, 25},{Charlie, 35},{Alice, 25}};Person target {Alice, 25};int count std::count(people.begin(), people.end(), target);std::cout The person target.name with age target.age appears count times. std::endl;return 0;
}
在这个例子中我们定义了一个结构体Person其中包含姓名name和年龄age两个成员变量。然后我们重载了operator运算符以便能够比较两个Person对象是否相等。
在main函数中我们创建了一个people向量其中包含了几个Person对象。我们想要统计具有姓名为Alice且年龄为25的人出现的次数。使用std::count函数我们传入了people的迭代器范围以及目标对象target并得到了出现次数。最后输出结果即为该人物出现的次数。
count_if——统计符合条件的元素个数
函数原型 count_if(iterator beg, iterator end, _Pred); // 按条件统计元素出现次数 // beg 开始迭代器 // end 结束迭代器 // _Pred 谓词
示例
#include iostream
#include algorithm
#include vectorint main() {std::vectorint numbers {5, 15, 8, 12, 20, 30, 7, 10};// 统计大于10的元素个数int count std::count_if(numbers.begin(), numbers.end(), [](int num) {return num 10;});std::cout The number of elements greater than 10: count std::endl;return 0;
}
输出 The number of elements greater than 10: 5 在上面的代码中我们定义了一个整数向量numbers其中包含了一些整数。然后我们使用std::count_if函数来统计大于10的元素个数。为了指定统计条件我们传递了一个匿名的Lambda表达式作为谓词参数。Lambda表达式的函数体是return num 10;它表示当传入的num大于10时返回true否则返回false。
总结按值统计用count按条件统计用count_if