C++学习笔记——STL常用算法

常用遍历算法:

for_each

功能:实现遍历容器

函数原型:

for_each(iterator beg, iterator end, _func);
// 遍历算法 遍历容器元素
// beg 开始迭代器
// end 结束迭代器
// _func 函数或者函数对象

例子:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;


//普通函数
void print01(int a){
    cout<<a<<" ";
}

//函数对象
class print02{
public:
    void operator()(int a){
        cout<<a<<" ";
    }
};

void test01(){
    vector<int> v;
    for(int i=0;i<20;i++){
        v.push_back(i);
    }

//    遍历
    for_each(v.begin(),v.end(),print01);
    cout<<endl;

    for_each(v.begin(),v.end(),print02());
    cout<<endl;
}

int main() {
    test01();
    return 0;
}


transform

功能:搬运容器到另一个容器中

函数原型:

transform(iterator beg1, iterator end1, iterator beg2, _func);
//beg1 源容器开始迭代器
//end1 源容器结束迭代器
//beg2 目标容器开始迭代器
//_func 函数或者函数对象

例子:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;


//函数对象
class print01{
public:
    void operator()(int a){
        cout<<a<<" ";
    }
};

class Transform{
public:
    int operator()(int value){
        return value;
    }
};

void test01(){
    vector<int> v;
    for(int i=0;i<20;i++){
        v.push_back(i);
    }

//    目标容器
    vector<int> v_target;
//    目标容器需要提前开辟空间
    v_target.resize(v.size());

    transform(v.begin(),v.end(),v_target.begin(),Transform());

    for_each(v_target.begin(),v_target.end(),print01());
    cout<<endl;
}

int main() {
    test01();
    return 0;
}


常用查找算法:

find

功能:查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器

函数原型:

find(iterator beg, iterator end, value);
// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
// value 查找的元素

例子:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;


class Student{
public:
    Student(string name,int age){
        this->m_name=name;
        this->m_age=age;
    }

//    查找自定义数据类型时需要重载==运算符
    bool operator==(const Student& student){
        if(this->m_name==student.m_name&&this->m_age==student.m_age){
            return true;
        }
        return false;
    }

public:
    string m_name;
    int m_age;
};

void test01(){
    Student student1("li",20);
    Student student2("wang",16);
    Student student3("liu",18);
    Student student4("tian",16);

    vector<Student> v;

    v.push_back(student1);
    v.push_back(student2);
    v.push_back(student3);
    v.push_back(student4);

//    查找指定元素
    vector<Student>::iterator  it=find(v.begin(),v.end(),student1);
    if(it!=v.end()){
        cout<<"找到学生"<<endl;
    }else{
        cout<<"未找到"<<endl;
    }
}

int main() {
    test01();
    return 0;
}

find_if

功能:按照条件查找第一个满足条件的元素

函数原型:

find_if(iterator beg, iterator end, _Pred); 
// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值