C++ STL 中的非修改算法
原文:https://www.studytonight.com/cpp/stl/stl-non-modifying-algorithms
以下是我们将介绍的标准模板库中的一些非修改算法:
- 数数
- 平等的
- 错配
- 搜索
- 搜索
count
方法
count()
返回给定范围内等于给定值的元素个数。计数的语法是:
count(first ,last ,value)
: This will return number of the element in range defined by iterators first and last ( excluded ) which are equal ( == ) the value .
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main ()
{
int values[] = {5,1,6,9,10,1,12,5,5,5,1,8,9,7,46};
int count_5 = **count**(values, values+15, 5);
*/* now count_5 is equal to 4 */*
vector<int> v(values, values+15);
int count_1 = **count**(v.begin(), v.end(), 1);
*/* now count_1 is equal to */*
return 0;
}
equal
方法
equal()
比较两个范围内的元素,如果一个范围内的所有元素与另一个范围内对应的元素比较相等,则返回布尔值 true,否则返回布尔值 false。它有两种变体:
equal(first1, last1, first2)
:该函数比较 first1 和 last1(不包括)所指范围内的元素与起始位置 first2 的范围内的元素是否相等。如果所有元素都相等,则返回 true,否则返回 false。equal(first1 ,last1 ,first2 ,cmp_function)
:这里 cmp_function 用来决定如何检查两个元素是否相等,对于字符串、对象等非数字元素很有用。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool **cmp_string**(string i, string j)
{
return (i.size() == j.size());
}
int main()
{
int inputs1[] = { 1,2,3,4,5,6,7,8};
int inputs2[] = { -1,2,1,2,3,4,6,7,8,9};
vector<int> v1(inputs1 , inputs1+9 );
vector<int> v2(inputs2 , inputs2+10 );
cout<<**equal**(v1.begin(), v1.end(), v2.begin()+2 ) ; *// prints 0 , boolean false*
*/* use of compare function */*
string s1[] = { "abc" , "def" , "temp" , "testing" };
string s2[] = { "xyz" , "emp" , "resr" , "testing" };
cout<<equal( s1 , s1+4 , s2 , cmp_string); // prints 1
*/* note that the stings in s1 and s2 are not actually
equal but still equal() returns 1 , beacause we are defining
equality of two string by their length in cmp_function */*
}
mismatch
方法
这个方法返回一对迭代器,其中第一个迭代器指向第一个容器中的元素,第二个迭代器指向第二个容器中出现不匹配的元素。mismatch()
有两种变体。
mismatch(first1, last1, first2)
:这里第一个 1 和最后一个 1 是指定范围的第一个容器的迭代器和第一个 2 是指定位置的第二个容器的迭代器,开始比较。默认情况下,检查元素是否相等==
,返回迭代器对,给出发生不匹配的元素的位置。mismatch(first1, last1, first2, compare_function)
:除了 compare_function 用于检查元素是否被认为相等之外,本版本的工作原理与上一版本相同。
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
bool **cmp_string**(string i , string j)
{
return ( i.size() == j.size() );
}
int **main**()
{
int inputs1[] = {1,2,3,4,5,6,7,8};
int inputs1[] = {-1,2,1,2,3,4,6,7,8,9};
vector<int> **v1**(inputs1 ,inputs1+9);
vector<int> **v2**(inputs2 ,inputs2+9);
pair<vector<int<::iterartor, vector<int>::iterator> **position**;
*/* defining a pair of iterator to the vector of integer */*
position = **mismatch**(**v1**.begin(), **v1**.end(), **v2**.begin()+2) ;
*/* now position.first is an iterator pointing
to the 5th element in the vector v1 and position.second
points to the 7th element in the vector v2 */*
*/* use of compare function */*
string s1[] = {"abc", "def", "temp", "testing"};
string s2[] = {"xyz", "emp", "res", "testing"};
pair<string::iterator, string::iterator> position2;
position2 = **mismatch**( s1, s1+4, s2, cmp_string);
*/* now position2.first is an iterator pointing
to the 3rd element in s1 and position2.second points
to the 3rd element in the s2 */*
}
search
方法
该功能用于在给定的范围内搜索给定的序列。search()
有两种变体:
search(first1 ,last1 ,first2 ,last2)
:该功能在第一次 1 和最后 1 范围内搜索第一次 2 和最后 2 定义的顺序(其中不包括最后 1)。如果匹配,则返回序列中第一个元素的迭代器[first1,last1],否则返回 last1 的迭代器。search(first1 ,last1 ,first2 ,last2 ,cmp_functions)
:这里 cmp_function 用来决定如何检查两个元素的相等性,对于字符串、对象等非数字元素很有用。
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
int inputs1[] = { 1,2,3,4,5,6,7,8};
int inputs2[] = { 2,3,4};
vector<int> **v1**(inputs1, inputs1+9);
vector<int> **v2**(inputs2, inputs2+3);
vector<int>::iterator i ,j;
i = **search**(v1.begin(), v1.end(), v2.begin(), v2.end());
*/* now i points to the second element in v1 */*
j = **search**(v1.begin()+2, v1.end(), v2.begin(), v2.end());
*/* j now points to the end of v1 as no sequence is equal to 2,3,4 in
[v1.begin()+2 ,v1.end()] */*
}
search_n
方法
此方法在给定范围内搜索计数值序列。search()
有两种变体:
search(first1, last1, count, value)
:该方法在迭代器 first1 和 last1 定义的范围内搜索计数和值的序列(不包括 last1)。如果匹配,则返回序列中第一个元素的迭代器[first1,last1],否则返回 last1 的迭代器。search(first1, last1, count, value, cmp_functions)
:这里 cmp_function 用来决定如何检查两个元素的相等性,对于字符串、对象等非数字元素很有用。
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
int inputs1[] = {1,2,3,4,4,4,8,5,6,7,8};
vector<int> **v1**(inputs1, inputs1+11);
vector<int>::iterator I;
**i** = **search_n**(v1.begin(), v1.end(), 3, 4);
*/* now i points to the 4th element in v1 */*
**j** = **search**(v1.begin()+2, v1.end(), 2, 5);
*/* j now points to the end of v1 as no sequence is equal to 5,5 in
[v1.begin()+2 ,v1.end() ). */*
}