C++之STL算法使用参考

  • 算法(algorithm)是用于模板技术实现的适用于各种容器的通用程序
  • 算法常常通过迭代器间接地操作容器元素,而且通常会返回迭代器作为算法运算的结果
  • STL大约提供了70个算法,每个算法都是一个模板函数或者一组模板函数,能够在许多不同类型的容器上进行操作,各个容器则可能包含着不同类型的数据元素
  • STL中的算法覆盖了在容器上实施的各种常见操作,如遍历、排序、检索、插入及删除元素等操作。STL中许多算法不仅适用于系统提供的容器类,而且适用于普通的C++数组或自定义容器

find和count

  • find用于查找指定数据在某个区间中是否存在,该函数返回等于指定值的第一个元素位置,如果没找到就返回最后元素位置
  • count用于统计某个值在指定区间出现的次数
cpp 复制代码
find(beg, end, value);
count(beg, end, value);
cpp 复制代码
#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
int main(void){
    int a1[] = {100,200,300,400,500,600,700,800,900,1000};
    int *ptr = find(a1, a1+10, 800);
    cout << "400出现在数组a1的位置: " << ptr - a1 << endl;
    list<int> L1;
    int a2[] = {20,30,40,50,60,60,70,60,80};
    for(int i=0; i<sizeof(a2)/sizeof(a2[0]); i++){
        L1.push_back(a2[i]);
    }
    
    list<int>::iterator it;
    it = find(L1.begin(), L1.end(), 80);
    if(it != L1.end()){
        cout << "L1链表中存在元素: " << *it << endl;
        cout << "它是链表中第: " << distance(L1.begin(), it) + 1 << "个节点"<< endl;
    }
    int n1 = count(a1, a1+10, 500);
    cout << "a1 数组中 500出现了: "<<n1<< "次"<<endl;
    int n2 = count(L1.begin(), L1.end(), 60);
    cout << "L1 容器中 60出现了: " <<n2 <<"次"<< endl;
    return 0;
}
  • find算法从一个容器中查找指定的值,search算法则是从一个容器查找由另一个容器所指定的顺序值
cpp 复制代码
search(beg1, end1, beg2, end2);//左闭右开
  • search将在[beg1, end1)区间查找有无与[beg2, end2)相同的子区间,如果找到就返回[beg1, end1)内第一个相同元素的位置,如果没有找到返回end1
cpp 复制代码
#include <iostream>
#include <algorithm>
#include <list>
#include <vector>
using namespace std;
int main(void){
    int a1[] ={10,20,30,40,50,60,70,80,90};
    int a2[]= {70,80,90};
    int *ptr = search(a1, a1+9, a2, a2+3);
    if(ptr == a1+9)
        cout << "not match" << endl;
    else
        cout << "math: " << ptr - a1 << endl;
    
    list<int> L;
    vector<int> V;
    for(int i=0; i<9; i++){
        L.push_back(a1[i]);
    }
    for(int i=0; i<3; i++){
        V.push_back(a2[i]);
    }
    
    list<int>::iterator pos;
    pos = search(L.begin(), L.end(), V.begin(), V.end());
    cout << distance(L.begin(), pos) << endl;
    return 0;
}:

merge

  • merge可对两个容器进行合并,将结果存放在第3个容器中,其用法如下:
cpp 复制代码
merge(beg1, end1, beg2, end2, dest)
  • merge将[beg1,end1)与[beg2, end2)区间合并,把结果存放在dest容器中。如果参与合并的两个容器中的元素是有序的,则合并的结果也是有序的。
  • list链表也提供了一个merge成员函数, 它能够把两个list类型的链表合并在一起。同样地,如果合并前的链表是有序的,则合并后的链表仍然有序
cpp 复制代码
#include <iostream>
#include <algorithm>
#include <list>
#include <vector>
using namespace std;
int main(void){
    int a1[] ={10,20,30,40,50,60,70,80,90};
    int a2[]= {70,80,90, 100};
    int a3[13] = {0};
    merge(a1, a1+9, a2, a2+4, a3);
    for(int i=0; i<13; i++){
        cout << a3[i] << "\t" ;
    }
    cout << endl;
    
    list<int> L1;
    list<int> L2;
    for(int i=0; i<9; i++){
        L1.push_back(a1[i]);
    }
    for(int i=0; i<4; i++){
        L2.push_back(a2[i]);
    }
    L1.merge(L2);
    list<int>::iterator it;
    for(it=L1.begin(); it!=L1.end(); it++){
        cout << *it << "\t";
    }
    cout << endl;
    return 0;
}

sort

  • sort可对指定容器区间内的元素进行排序,默认排序的方式为从小到大,其用法入下:
cpp 复制代码
sort(beg, end);
  • beg, end)是要排序的区间,sort将按从小到大的顺序对该区间的元素进行排序

int main(void){
int a1[] ={10,2,-30, 840,550,6,120};
sort(a1, a1+7);
for(int i=0; i<7; i++){
cout << a1[i] << "\t";
}
cout << endl;
int a2[] ={10,2,-30, 840,550,6,120};
vector<int> V;
vector<int>::iterator it;
for(int i=0; i<7; i++){
V.push_back(a2[i]);
}
sort(V.begin(), V.end());
for(it=V.begin(); it!=V.end(); it++){
cout << *it << "\t";
}
cout << endl;
return 0;
}

复制代码
相关推荐
寻寻觅觅☆6 小时前
东华OJ-基础题-106-大整数相加(C++)
开发语言·c++·算法
fpcc6 小时前
并行编程实战——CUDA编程的Parallel Task类型
c++·cuda
ceclar1238 小时前
C++使用format
开发语言·c++·算法
lanhuazui108 小时前
C++ 中什么时候用::(作用域解析运算符)
c++
charlee448 小时前
从零实现一个生产级 RAG 语义搜索系统:C++ + ONNX + FAISS 实战
c++·faiss·onnx·rag·语义搜索
老约家的可汗8 小时前
初识C++
开发语言·c++
crescent_悦9 小时前
C++:Product of Polynomials
开发语言·c++
小坏坏的大世界9 小时前
CMakeList.txt模板与 Visual Studio IDE 操作对比表
c++·visual studio
乐观勇敢坚强的老彭9 小时前
c++寒假营day03
java·开发语言·c++
愚者游世10 小时前
brace-or-equal initializers(花括号或等号初始化器)各版本异同
开发语言·c++·程序人生·面试·visual studio