c++非变易算法-stl算法_C 语言

C++ STL标准模板库在数据结构和算法的实践领域发挥着重要作用,极大的提高了开发效率。STL的三大组成部分为容器、迭代器、算法,本文主要讲解STL算法中的非变易算法。本文从实践的角度简单介绍了一下相关函数的使用。

C++ STL的非变易算法(Non-mutating algorithms)是一组不破坏函数数据的模板函数,用来对序列数据进行逐个处理、元素查找、子序列搜索、统计和匹配,基本上可用于各种容器。下面的叙述中迭代器区间默认为[first, last),迭代器具有“++”迭代和“*”访问操作。

逐个处理算法

for_each函数
该函数对迭代器区间的每个元素,执行单参数函数对象定义的操作。

下面的实例程序,将打印容器vector中的每个元素。

复制代码 代码如下:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void print(int x) {
    cout << x << " ";
}
int main(void) {
    vector<int> v;
    for(int i = 0; i < 10; i++) {
        v.push_back(i * 2);
    }
    for_each(v.begin(), v.end(), print);
    return 0;
}

结果输出为:

复制代码 代码如下:

0 2 4 6 8 10 12 14 16 18

元素查找算法

find函数
该函数用于查找等于某值的元素。如果迭代器i所指的元素满足*i == value,则返回迭代器i。未找到满足条件的元素,返回last。只要找到第一个满足条件的元素就返回迭代器位置,不再继续查找。

下面的示例程序查找容器vector中,第一个值为6的元素,打印元素位置及其前一元素。

复制代码 代码如下:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(void) {
    vector<int> v;
    for(int i = 0; i < 10; i++) {
        v.push_back(i * 2);
    }
    vector<int>::iterator iv = find(v.begin(), v.end(), 6);
    if(iv == v.end()) {
        cout << "Find nothing." << endl;
    } else {
        cout << "The postion of " << *iv << " is " << iv - v.begin() << endl;
        cout << "The previous element of it is " << *(--iv) << endl;
    }
    return 0;
}

结果输出为:

复制代码 代码如下:

The postion of 6 is 3
The previous element of it is 4

find_if函数

该函数是find的一个谓词判断版本,查找满足谓词判断函数的元素。

下面的实例程序将寻找容器vector中第一个能被3整除的元素。

复制代码 代码如下:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int divBy3(int x) {
    return x % 3 ? 0 : 1;
}
int main(void) {
    vector<int> v;
    for(int i = 1; i < 10; i++) {
        v.push_back(i * 2);
    }
    vector<int>::iterator iv = find_if(v.begin(), v.end(), divBy3);
    if(iv == v.end()) {
        cout << "None could be divided by 3 with no remaineder." << endl;
    } else {
        cout << *iv << " could be divided by 3 with no remainder." << endl;
    }
    return 0;
}

结果输出为:

复制代码 代码如下:

6 could be divided by 3 with no remainder.

adjacent_find函数

该函数用于查找相等或满足条件的邻近元素对。它有两个使用原型,一个用于查找相等的两个连续元素,另一个使用二元谓词判断,查找满足条件的邻近元素对。

下面的实例程序用于寻找容器中相等的元素和奇偶性相同的元素。

复制代码 代码如下:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int parity_equal(int x, int y) {
    return (x - y) % 2 == 0 ? 1 : 0;
}
int main(void) {
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(5);
    v.push_back(5);
    v.push_back(7);
    vector<int>::iterator iv = adjacent_find(v.begin(), v.end());
    if(iv != v.end()) {
        cout << "There are two equal elements." << endl;
        cout << "It is " << *iv << endl;
    }
    iv = adjacent_find(v.begin(), v.end(), parity_equal);
    if(iv != v.end()) {
        cout << "There are two parity euqal elements." << endl;
        cout << "They are " << *iv << " and ";
        iv++;
        cout << *iv << endl;
    }
    return 0;
}

输出结果为:

复制代码 代码如下:

There are two equal elements.
It is 5
There are two parity euqal elements.
They are 3 and 5

find_first_of函数

该函数用于查找某个范围之内的元素。它有两个使用原型,一个是相等,另一个是二元谓词判断。元素找到则返回迭代器,否则返回末位置。

下面的实例程序用于计算容器v2中元素在容器v中重合出现的首位置。

复制代码 代码如下:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(void) {
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(2);
    v.push_back(3);
    v.push_back(5);
    v.push_back(5);
    v.push_back(7);
    vector<int> v2;
    v2.push_back(3);
    v2.push_back(3);
    v2.push_back(5);
    vector<int>::iterator iv = find_first_of(v.begin(), v.end(), v2.begin(), v2.end());
    cout << "The position of the first equal element is " << iv - v.begin() << endl;
    return 0;
}

输出结果为:

复制代码 代码如下:

The position of the first equal element is 3

元素统计算法

count函数
该函数用于计算容器中某个给定值的出现次数。它有两个使用原型,区别在于计数是直接返回还是引用返回。

下面的实例程序计算了容器中5的出现次数,结果直接返回。

复制代码 代码如下:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(void) {
    vector<int> v;
    for(int i = 0; i < 17; i++) {
        v.push_back(i % 6);
    }
    int num = count(v.begin(), v.end(), 5);
    cout << "The number of 5 is " << num << endl;
    return 0;
}

输出结果为:

复制代码 代码如下:

The number of 5 is 2

count_if函数

该函数使用谓词判断函数,统计迭代器区间上满足条件的元素个数。它有两个使用原型,区别在与计数是直接返回还是引用返回。

下面的实例程序统计了容器中大于10的数字的出现次数,结果直接返回。

复制代码 代码如下:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int greaterThan10(int x) {
    return x > 10 ? 1 : 0;
}
int main(void) {
    vector<int> v;
    for(int i = 0; i < 17; i++) {
        v.push_back(i);
    }
    int num = count_if(v.begin(), v.end(), greaterThan10);
    cout << "The number of the figure that greater than 10 is " << num << endl;
    return 0;
}

输出结果为:

复制代码 代码如下:

The number of the figure that greater than 10 is 6

序列匹配算法

mismatch函数
该函数用于比较两个序列,找出首个不匹配元素的位置。它有两个使用原型,分别为不相等和不满足二元谓词条件。

该函数还涉及到pair的使用。

下面的实例程序比较两个整型容器,并找出不匹配的数字。

复制代码 代码如下:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(void) {
    vector<int> v1, v2;
    v1.push_back(3);
    v1.push_back(5);
    v1.push_back(5);
    v2.push_back(3);
    v2.push_back(5);
    v2.push_back(7);
    pair<vector<int>::iterator, vector<int>::iterator> result = mismatch(v1.begin(), v1.end(), v2.begin());
    if(result.first == v1.end() && result.second == v2.end()) {
        cout << "v1 is same as v2." << endl;
    } else {
        cout << "The dismatching figure are "
             << *(result.first) << " and "
             << *(result.second) << endl;
    }
    return 0;
}

输出结果为:

复制代码 代码如下:

The dismatching figure are 5 and 7

equal函数

该函数逐一比较两个序列的元素是否相等,返回值为true/false,不返回迭代器值。它有两个使用原型,分别为元素相等和二元谓词判断条件。

下面的实例程序用于比较两容器中数字绝对值是否相等。

复制代码 代码如下:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int absEqual(int x, int y) {
    //return (x == abs(y) || y == abs(x)) ? 1 : 0;
    return abs(x) == abs(y) ? 1 : 0;
}
int main(void) {
    vector<int> v1, v2;
    v1.push_back(3);
    v1.push_back(5);
    v1.push_back(5);
    v2.push_back(3);
    v2.push_back(5);
    v2.push_back(-5);
    if(equal(v1.begin(), v1.end(), v2.begin(), absEqual)) {
        cout << "The elements of v1 and v2 are equal in abosolute value." << endl;
    } else {
        cout << "The elements of v1 and v2 are not equal in abosolute value." << endl;
    }
    return 0;
}

输出结果为:

复制代码 代码如下:

The elements of v1 and v2 are equal in abosolute value.

子序列搜索算法

search函数

该函数在一个序列中搜索与另一序列匹配的子序列。它有两个使用原型,分别为完全匹配和二元谓词判断。匹配成功则返回子序列的首个元素的迭代器值。

search函数与find_first_of函数形似,但不相同。search找的是一块相同的区域,要求这块区域与后面列表的元素及其顺序相同;find_first_of找的是一个元素,只要这个元素是后面一个列表的任意一个就行。

下面的实例程序说明了search与find_first_of的不同。

复制代码 代码如下:

#include <iostream>
#include <algorithm>
#include <vector>
int main(void) {
    vector<int> v1, v2;
    v1.push_back(1);
    v1.push_back(4);
    v1.push_back(2);
    v1.push_back(3);
    v1.push_back(4);
    v2.push_back(2);
    v2.push_back(3);
    v2.push_back(4);
    vector<int>::iterator ivSearch, ivFind;
    ivSearch = search(v1.begin(), v1.end(), v2.begin(), v2.end());
    ivFind = find_first_of(v1.begin(), v1.end(), v2.begin(), v2.end());
    cout << "Position of search: " << ivSearch - v1.begin() << endl;
    cout << "Position of find_first_of: " << ivFind - v1.begin() << endl;
    return 0;
}

输出结果为:

复制代码 代码如下:

Position of search: 2
Position of find_first_of: 1

search_n函数

该函数用于搜索序列中是否有一系列元素值均为某个给定值的子序列。它有两个使用原型,分别为值相等和满足谓词判断条件。

下面的实例程序展示了寻找3个连续的数字8的过程。

复制代码 代码如下:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(void) {
    vector<int> v;
    v.push_back(3);
    v.push_back(8);
    v.push_back(8);
    v.push_back(8);
    v.push_back(4);
    vector<int>::iterator iv = search_n(v.begin(), v.end(), 3, 8);
    if(iv == v.end()) {
        cout << "There are no three consecutive 8." << endl;
    } else {
        cout << "Three consecutive 8 is founded." << endl;
    }
    return 0;
}

结果输出为:

复制代码 代码如下:

Three consecutive 8 is founded.

find_end函数
该函数用于在一个序列中搜索出最后一个与另一序列匹配的子序列。用search函数作用相似,方向相反。

下面的实例程序,展示了搜索容器v中最后一个与v1匹配的子序列的过程。

复制代码 代码如下:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(void) {
    vector<int> v, v2;
    v.push_back(1);
    v.push_back(3);
    v.push_back(5);
    v.push_back(3);
    v.push_back(5);
    v.push_back(7);
    v2.push_back(3);
    v2.push_back(5);
    vector<int>::iterator iv = find_end(v.begin(), v.end(), v2.begin(), v2.end());
    if(iv != v.end()) {
        cout << "The position of last matching subsequence is " << iv - v.begin() << endl;
    }
    return 0;
}

输出结果为:

复制代码 代码如下:

The position of last matching subsequence is 3

时间: 2024-07-31 23:35:59

c++非变易算法-stl算法_C 语言的相关文章

算法之排列算法与组合算法详解_C 语言

1. 前言 本文介绍了常用的排列组合算法,包括全排列算法,全组合算法,m个数选n个组合算法等. 2. 排列算法 常见的排列算法有: (A)字典序法 (B)递增进位制数法 (C)递减进位制数法 (D)邻位对换法 (E)递归法 介绍常用的两种: (1) 字典序法 对给定的字符集中的字符规定了一个先后关系,在此基础上按照顺序依次产生每个排列. [例]字符集{1,2,3},较小的数字较先,这样按字典序生成的全排列是:123,132,213,231,312,321. 生成给定全排列的下一个排列 所谓一个的

C语言实现的PNPoly算法代码例子_C 语言

写C语言的实验用到的一个算法,判断一个点是否在多边形的内部.C的代码如下: int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy) { int i, j, c = 0; for (i = 0, j = nvert-1; i < nvert; j = i++) { if ( ((verty[i]>testy) != (verty[j]>testy)) && (testx <

C语言的冒泡排序和快速排序算法使用实例_C 语言

冒泡排序法 题目描述:     用一维数组存储学号和成绩,然后,按成绩排序输出. 输入:     输入第一行包括一个整数N(1<=N<=100),代表学生的个数.     接下来的N行每行包括两个整数p和q,分别代表每个学生的学号和成绩. 输出:     按照学生的成绩从小到大进行排序,并将排序后的学生信息打印出来.     如果学生的成绩相同,则按照学号的大小进行从小到大排序. 样例输入:     3     1 90     2 87     3 92 样例输出:     2 87    

C++非递归建立二叉树实例_C 语言

本文实例讲述了C++非递归建立二叉树的方法.分享给大家供大家参考.具体分析如下: 思路: 设置一个标记变量flag并初始化为1. flag = 1表示现在需要创建当前结点的左孩子,2表示需要创建右孩子,3则表示当前结点的左右孩子都已经创建完毕,需要执行出栈操作,直到当前结点不是父结点的右孩子为止. 以先序创建如图所示二杈树: 实现代码: PBTree create() { char ch[20]; scanf("%s",ch); int len = strlen(ch); PBTree

stl常用算法(Algorithms)介绍(stl排序算法、非变序型队列)_C 语言

算法:用来处理群集内的元素.它们可以出于不同的目的而搜寻,排序,修改,使用那些元素.是一种应用在容器上以各种方法处理其内存的行为或功能,如sort(排序),copy(拷贝)- 算法由模板函数体现,这些函数不是容器类的成员函数,是独立的函数,它们可以用于STL容器,也可以用于普通的C++数组等. 头文件:#include<algorithm> 在STL的泛型算法中有4类基本的算法: 1)变序型队列算法: 可以改变容器内的数据: 2)非变序型队列算法:处理容器内的数据而不改变他们: 3)排序值算法

C++实现汉诺塔算法经典实例_C 语言

本文所述为汉诺塔算法的C++代码的经典实现方法. 汉诺塔问题描述:3个柱为a.b.c,圆盘最初在a柱,借助b柱移到c柱.需要你指定圆盘数. 具体实现代码如下: #include <iostream> using namespace std; int times = 0; //全局变量,搬动次数 //第n个圆盘从x柱搬到z柱 void move(int n, char x, char z) { cout << "第" << ++times <&l

VC++实现选择排序算法简单示例_C 语言

本文以一个非常简单的实例说明VC++选择排序算法的实现方法,对n个记录进行n-1趟简单选择排序,在无序区中选取最小记录. 具体实现代码如下: #include<iostream> using namespace std; //简单选择排序 void SelectSort(int r[ ], int n) { int i; int j; int index; int temp; for (i=0; i<n-1; i++) //对n个记录进行n-1趟简单选择排序 { index=i; for

c++11新增的便利算法实例分析_C 语言

C++是一门应用非常广泛的程序设计语言,而c++11则新增加了一些便利的算法,这些新增的算法使我们的代码写起来更简洁方便,本文列举一些常用的新增算法,算是做个总结分析,更多的新增算法读者可以参考:http://en.cppreference.com/w/cpp/algorithm. 算法库新增了三个用于判断的算法all_of.any_of和none_of,定义如下: template< class InputIt, class UnaryPredicate > bool all_of( Inp

C++用Dijkstra(迪杰斯特拉)算法求最短路径_C 语言

算法介绍 迪杰斯特拉算法是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中最短路径问题.迪杰斯特拉算法主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Dijkstra算法能得出最短路径的最优解,但由于它遍历计算的节点很多,所以效率低. 算法思想 按路径长度递增次序产生算法: 把顶点集合V分成两组: (1)S:已求出的顶点的集合(初始时只含有源点V0) (2)V-S=T:尚未确定的顶点集合 将T中顶点按递增