频道栏目
首页 > 资讯 > C++ > 正文

C++的STL算法

16-11-03        来源:[db:作者]  
收藏   我要投稿

cpp的STL算法

什么是stl算法

操作stl集合的一堆方法。很方便,他们就是一堆工具。

你只要熟悉stl就可以很方便的使用他们啦。

先明白算法有哪些,有什么作用,然后熟悉一部分常用的,其他的在实践中时候用

stl算法的分类

非变序算法

计数算法
    count,count_if
搜索算法
    search,seach_n
    find,find_if,find_end,find_first_of,adjacent_find
比较算法
    equal,mismatch,lexicographical_compare

变序算法

初始化算法
    fill,fill_n,generate,generate_n
修改算法
    for_each,transform
复制算法
    copy,copy_backward
删除算法
    remove,reomve_if,remove_copy,remove_copy_if
    unique,unique_copy
替换算法
    replace,replace_if
排序算法
    sort,stab_sort,partial_sort

分区算法
    partion,stable_partition
可用于排序容器的算法
binary_search,lower_bound,upper_bound

stl算法的应用

计算元素的个数
查找元素
在集合中搜索元素或序列
将容器中的元素初始化为指定的值
使用for_each处理范围内的元素
使用transtrom 对范围进行编号
复制和删除操作
替换值以及满足给定条件的元素
排序,在有序集合中搜索以及删除重复元素
将范围分区

在有序集合中插入元素
//
//  main.cpp
//  use_stl_algorithm
//
//  Created by bikang on 16/11/1.
//  Copyright (c) 2016年 bikang. All rights reserved.
//

#include 
#include 
#include 
#include 

using namespace std;

template 
class PrintClass {
public:
    void operator()(const T& elem)const{
        cout << elem << " ";
    }
};


template 
bool isEven(const T& number) {
    return ((number%2) == 0);
}

template 
T addData(const T & v1,const T & v2) {
    return (v1+v2);
}


//在容器中计算元素个数和查找元素
void tstlalg1();
//查找序列
void tsearch();
//容器初始化fill 偏移范围 fill_n 开始位置,个数,数值
//generate随机初始化
void tfill();
//transform 容器的内容的变换
void ttransform();
//测试复制和删除
//copy,copy_backward,remove,remove_if
//replace
//partition  stable_partition
void tdelCopy();

//sort binary_search unique 排序 查找 去重复
void  tsort();



int main(int argc, const char * argv[]) {
    //tstlalg1();
    //tsearch();
    //tfill();
    //ttransform();
    //tdelCopy();
    tsort();
    return 0;
}

void  tsort(){
    vector vec1;
    vec1.push_back("tom");
    vec1.push_back("tim");
    vec1.push_back("tim");
    vec1.push_back("tam");
    vec1.push_back("tam");
    vec1.push_back("kim");
    vec1.push_back("kim");
    for_each(vec1.begin(), vec1.end(), PrintClass());cout << endl;

    //排序
    sort(vec1.begin(), vec1.end());
    for_each(vec1.begin(), vec1.end(), PrintClass());cout << endl;

    //查找
    bool searchRes = binary_search(vec1.begin(), vec1.end(), "tom");
    if(searchRes){
        cout << "find ok"<());cout << endl;
    vec1.erase(ite, vec1.end());
    for_each(vec1.begin(), vec1.end(), PrintClass());cout << endl;


    //有序插入,优选的最前和最靠后的位置
    string lowerStr = "ad";
    ite = lower_bound(vec1.begin(), vec1.end(),lowerStr);
    vec1.insert(ite, lowerStr);
    for_each(vec1.begin(), vec1.end(), PrintClass());cout << endl;

    string upperStr = "ss";
    ite = upper_bound(vec1.begin(), vec1.end(),upperStr);
    vec1.insert(ite, upperStr);
    for_each(vec1.begin(), vec1.end(), PrintClass());cout << endl;


}

void tdelCopy(){

    list l1;
    for(int j =0;j<10;++j){
        l1.push_back(j);
    }
    for_each(l1.begin(), l1.end(), PrintClass());cout << endl;

    //copy
    vector vec1(l1.size() *2);
    vector::iterator ite;
    ite = copy(l1.begin(), l1.end(),vec1.begin());
    for_each(vec1.begin(), vec1.end(), PrintClass());cout << endl;
    copy_backward(l1.begin(), l1.end(), vec1.end());
    for_each(vec1.begin(), vec1.end(), PrintClass());cout << endl;

    //remove
    remove(vec1.begin(), vec1.end(), 0);
    for_each(vec1.begin(), vec1.end(), PrintClass());cout << endl;
    //remove if

    remove_if(vec1.begin(), vec1.end(), [](int i){return i%2==0;});
    for_each(vec1.begin(), vec1.end(), PrintClass());cout << endl;

    //replace
    replace(vec1.begin(), vec1.end(), 1, 21);
    for_each(vec1.begin(), vec1.end(), PrintClass());cout << endl;
    //replace if
    replace_if(vec1.begin(), vec1.end(), [](int i){return i%2==1;}, 55);
    for_each(vec1.begin(), vec1.end(), PrintClass());cout << endl;

    //partition
    partition(l1.begin(), l1.end(),[](int i){return i%2==1;});
    for_each(l1.begin(), l1.end(), PrintClass());cout << endl;
    //stable_partition
    stable_partition(l1.begin(), l1.end(),[](int i){return i%2==1;});
    for_each(l1.begin(), l1.end(), PrintClass());cout << endl;


}

void ttransform(){

    string str1 = "this is a simple language!";
    string str1low;
    str1low.resize(str1.size());
    transform(str1.begin(), str1.end(), str1low.begin(), (int(*)(int))toupper);
    cout << str1low << endl;

    //测试加法
    vector vec1;
    for (int i =0; i<10; ++i) {
        vec1.push_back(i);
    }

    vector vec2;
    for (int i =20; i<30; ++i) {
        vec2.push_back(i);
    }

    vector res;
    res.resize(10);

    //两个容器的内容相加
    transform(vec1.begin(), vec1.end(), vec2.begin(), res.begin(), addData);
    for_each(vec1.begin(), vec1.end(), PrintClass());cout << endl;
    for_each(vec2.begin(), vec2.end(), PrintClass());cout << endl;
    for_each(res.begin(), res.end(), PrintClass());cout << endl;

}

void tfill(){
    cout << "tfill" << endl;
    vector vec1(3);
    fill(vec1.begin(), vec1.end(), 9);
    for_each(vec1.begin(), vec1.end(), PrintClass());cout << endl;

    vec1.resize(6);
    fill_n(vec1.begin()+3, 3, 3);
    for_each(vec1.begin(), vec1.end(), PrintClass());cout << endl;
    vec1.resize(10);
    generate(vec1.begin()+6, vec1.end(), rand);
    for_each(vec1.begin(), vec1.end(), PrintClass());cout << endl;
    vec1.resize(12);
    generate_n(vec1.begin()+10, 2, rand);
    for_each(vec1.begin(), vec1.end(), PrintClass());cout << endl;

}

void tsearch(){
    cout << "tsearch" << endl;
    vector vec1;
    for (int i =0; i<10; ++i) {
        vec1.push_back(i);
    }
    for_each(vec1.begin(), vec1.end(), PrintClass());cout << endl;

    list l1;
    for(int j =3;j<5;++j){
        l1.push_back(j);
    }

    //search查找
    vector::iterator ite;
    ite = search(vec1.begin(), vec1.end(),l1.begin(), l1.end());
    if(ite != vec1.end()){
        cout << "pos=" << distance(vec1.begin(), ite);
    }else{
        cout << " search faild";
    }


}

void tstlalg1(){
    cout << "start" << endl;
    vector vec1;
    for (int i =0; i<10; ++i) {
        vec1.push_back(i);
    }
    for_each(vec1.begin(), vec1.end(), PrintClass());cout << endl;
    //查找 数据数量
    long numCount = 0;
    int find_data = 3;
    numCount = count(vec1.begin(), vec1.end(), (int)find_data);
    cout << "numCount=" <);
    cout << "npos=" <::iterator ite;
    ite = find(vec1.begin(), vec1.end(), find_data);
    if(ite != vec1.end()){
        cout << find="" ok="" ite="" pre="">
相关TAG标签
上一篇:计算机图形学(三)图元的属性15字符属性
下一篇:MySQL / MariaDB / PerconaDB - 提权/条件竞争漏洞(附POC)
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站