STL 算法

一,算法的泛化过程

算法的泛型化过程,考虑的是如何将算法独立于要处理的数据结构,即如何设计一个算法,可以把它用于任何数据结构(vector,list,set,map)要做到这一点,把要操作的数据结构的类别加以抽象化,模板提供了存储在容器中的数据类型的通用表示,迭代器提供了遍历容器中的值的通用表示,整个过程称为算法的泛型化实现算法泛型化的关键就是使用迭代器,迭代器作为算法与容器之间的一个粘合剂。

 

二,遍历容器中的元素

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

class Func{
public:
    void operator()(int x){
        cout<<x<<" ";
    }
};

void print(int x){
    cout<<x<<" ";
}

int main(){
    int nums[] = {1, 2, 3, 4, 5};
    vector<int> vect(nums, nums + 5);

    //使用函数指针作为参数
    for_each(vect.begin(), vect.end(), print);

    //使用函数对象作为参数
    for_each(vect.begin(), vect.end(), Func());
    return 0;
}

 

三,在指定的区间查找元素

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

int main(){
    int nums[] = {1, 2, 3, 4, 5};
    vector<int> vect(nums, nums + 5);
    //在指定的区间查找某个元素
    vector<int>::iterator iter = find(vect.begin(), vect.end(), 5);
    if(iter != vect.end()){
        cout<<*iter<<endl;
    }
    return 0;
}

 

四,对指定的区间进行排序

(1)使用sort对vector中的元素进行排序

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    int numArray[] = {1, 4, 3, 2, 8, 6, 7, 5, 10, 9};
    vector<int> vect(numArray, numArray + 10);

    sort(vect.begin(), vect.end());

    for (vector<int>::iterator iter = vect.begin(); iter != vect.end(); iter ++) {
        cout << *iter << " ";
    }

    return 0;
}

输出结果

1 2 3 4 5 6 7 8 9 10

从输出结果来看,sort默认进行从小到大的排序

(2)自定义排序算法

sort函数的第三个参数是一个函数指针,可以指定排序方法,它默认是进行从小到大的排序,因此,可以自定义"<"操作

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool comp(int x, int y){
    return y < x;
}

int main()
{
    int numArray[] = {1, 4, 3, 2, 8, 6, 7, 5, 10, 9};
    vector<int> vect(numArray, numArray + 10);

    sort(vect.begin(), vect.end(), comp);

    for (vector<int>::iterator iter = vect.begin(); iter != vect.end(); iter ++) {
        cout << *iter << " ";
    }

    return 0;
}

comp函数有两个参数,可以理解成前后两个数,如果y < x为真,说明后面的数小于前面的数为真,进行从大到小的排序

(3)对自定义结构体进行排序

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef struct {
    string name;
    double grade;
} Student;

bool comp(const Student &a, const Student &b){
    return b.grade < a.grade;
}

int main()
{
    vector<Student> studentsVect;
    for (int i = 0; i < 6; i ++) {
        Student obj;
        obj.grade = i * 10;
        obj.name = "jack";

        studentsVect.push_back(obj);
    }

    sort(studentsVect.begin(), studentsVect.end(), comp);

    for (vector<Student>::iterator iter = studentsVect.begin(); iter != studentsVect.end(); iter ++) {
        cout << (*iter).grade << " ";
    }

    return 0;
}

 

五、移动指定区间的元素

下面是remove这个函数的声明,如下:

template<class ForwardIterator, class T>
ForwardIterator remove(ForwardIterator first, ForwardIterator last, const T& value);

这个函数的功能是将指定区间的待删除元素,都移动到这个区间的末尾,函数的返回值是一个迭代器,指向最后一个非待删除元素后面的位置。这个函数只是将待删除元素之后的元素移动到vector的前端,并没有真正删除元素,如果要真正删除元素需要使用erase函数。例如:

int numArray[] = {1, 4, 3, 4, 5, 4, 7, 8, 9, 10};
vector<int> vect(numArray, numArray + 10);

// 移动容器里面值为4的元素
vector<int>::iterator retIter = remove(vect.begin(), vect.end(), 4);
cout << *retIter << endl;

for (vector<int>::iterator iter = vect.begin(); iter != vect.end(); iter ++) {
    cout << *iter << " ";
}

输出结果

8
1 3 5 7 8 9 10 8 9 10

从输出结果来看,remove只是移动元素并没有删除元素,在指定的区间最后一个非待删除元素是10,因此remove返回的迭代器指向10的后面就是8。

remove跟erase一起使用,删除指定区间的指定元素,例如:

int numArray[] = {1, 4, 3, 4, 5, 4, 7, 8, 9, 10};
vector<int> vect(numArray, numArray + 10);

// 删除容器里面的4
vect.erase(remove(vect.begin(), vect.end(), 4), vect.end());

for (vector<int>::iterator iter = vect.begin(); iter != vect.end(); iter ++) {
    cout << *iter << " ";
}

六,删除容器中的元素

erase的函数原型有两种形式,如下:

iterator erase(iterator position); //删除单个元素
 
iterator erase(iterator first, iterator last); //删除指定区间的元素

(1)在进行单个元素删除时,传入的迭代器指向不变,仍然指向被删除元素的位置,而被删除元素之后的所有元素都向前移动一位,函数的返回值是一个迭代器,指向了原来被删除元素的下一个元素。

(2)删除指定区间的元素时,后面的元素会被复制到被删除元素段开始的地方,而vector.end()也根据删除的元素个数往前移动函,数的返回值是一个迭代器,指向了原来被删除区间的下一个元素。

下面是一段错误的删除指定元素的代码,如下:

int main()
{
    int numArray[] = {1, 4, 3, 4, 5, 4, 7, 8, 9, 10};
    vector<int> vect(numArray, numArray + 10);


    for (vector<int>::iterator iter = vect.begin(); iter != vect.end(); iter ++) {
        if (*iter == 4) {
            vect.erase(iter);
        }
    }

    return 0;
}

注意:当vect.erase(iter)之后,iter就变成了一个野指针,对一个野指针进行 iter++ 是肯定会出错的。下面是正确的做法,例如:

int main()
{
    int numArray[] = {1, 4, 3, 4, 5, 4, 7, 8, 9, 10};
    vector<int> vect(numArray, numArray + 10);


    for (vector<int>::iterator iter = vect.begin(); iter != vect.end(); ) {
        if (*iter == 4) {
            iter = vect.erase(iter);
        } else {
            iter ++;
        }
    }

    return 0;
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值