STL中算法锦集(二)
文章目录
- STL中算法锦集(二)
- 一、< algorithm >
- 1.std::equal
- 2.std::equal_range
- 3.std::fill
- 4.std::fill_n
- 5.std::find
- 6.std::find_end
- 7.std::find_first_of
一、< algorithm >
1.std::equal
template <class InputIterator1, class InputIterator2>
bool equal (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2);
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
bool equal (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, BinaryPredicate pred);
- InputIterator1 first1:第一个区间的开始
- InputIterator1 last1:第一个区间的结束
- InputIterator2 first2:第二个区间的开始
- BinaryPredicate pred:
自定义比较规则
,可以是函数指针或者仿函数对象、lambda表达式
- 注意第一个区间的长度如果比第二个区间的长度长,那么肯定返回false;
- 反之
第一个区间的长度如果比第二个区间的长度短,程序正常判断的
- 作用:
[first1,last1)区间中的每个元素和first2区间中的对应元素是否满足pred条件
- 返回值:如果第一个区间中的每个元素和第二个区间中的对应元素都满足pred条件,返回true,反之返回false
- 实现:
template <class InputIterator1, class InputIterator2>
bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
{
while (first1!=last1) {
if (!(*first1 == *first2))
return false;
++first1;
++first2;
}
return true;
}
#include
#include
#include
bool mypredicate (int i, int j) {
return (i==j);
}
int main () {
int myints[] = {20,40,60,80,100};
std::vector<int>myvector (myints,myints+5);
if ( std::equal (myvector.begin(), myvector.end(), myints) )
std::cout << "The contents of both sequences are equal.\n";
else
std::cout << "The contents of both sequences differ.\n";
myvector[3]=81;
if ( std::equal (myvector.begin(), myvector.end(), myints, mypredicate) )
std::cout << "The contents of both sequences are equal.\n";
else
std::cout << "The contents of both sequences differ.\n";
return 0;
}
Output:
The contents of both sequences are equal.
The contents of both sequence differ.
2.std::equal_range
template <class ForwardIterator, class T>
pair<ForwardIterator,ForwardIterator>
equal_range (ForwardIterator first, ForwardIterator last, const T& val);
template <class ForwardIterator, class T, class Compare>
pair<ForwardIterator,ForwardIterator>
equal_range (ForwardIterator first, ForwardIterator last, const T& val,Compare comp);
- ForwardIterator first:区间的开始
- ForwardIterator last:区间的结束
- const T& val:待比较的值
- Compare comp:自定义比较规则
- 需要
排序
- 作用:求[first,last)区间内值为val的范围,左闭右开区间
- 返回值:
返回值为一个pair的键值对,每个元素都是迭代器,[begin,end):代表值为val的开始位置和结束位置的下一个位置
- 实现:
template <class ForwardIterator, class T>
pair<ForwardIterator,ForwardIterator>
equal_range (ForwardIterator first, ForwardIterator last, const T& val)
{
ForwardIterator it = std::lower_bound (first,last,val);
return std::make_pair ( it, std::upper_bound(it,last,val) );
}
#include
#include
#include
bool mygreater (int i,int j) { return (i>j); }
int main () {
int myints[] = {10,20,30,30,20,10,10,20};
std::vector<int> v(myints,myints+8);
std::pair<std::vector<int>::iterator,std::vector<int>::iterator> bounds;
std::sort (v.begin(), v.end());
bounds=std::equal_range (v.begin(), v.end(), 20);
std::sort (v.begin(), v.end(), mygreater);
bounds=std::equal_range (v.begin(), v.end(), 20, mygreater);
std::cout << "bounds at positions " << (bounds.first - v.begin());
std::cout << " and " << (bounds.second - v.begin()) << '\n';
return 0;
}
Output:
bounds at positions 2 and 5
3.std::fill
template <class ForwardIterator, class T>
void fill (ForwardIterator first, ForwardIterator last, const T& val);
- ForwardIterator first:区间的开始
- ForwardIterator last:区间的结束
- const T& val:填充的val
- 作用:把[first,last)区间用val值填充
- 返回值:没有返回值
- 实现:
template <class ForwardIterator, class T>
void fill (ForwardIterator first, ForwardIterator last, const T& val)
{
while (first != last) {
*first = val;
++first;
}
}
#include
#include
#include
int main () {
std::vector<int> myvector (8);
std::fill (myvector.begin(),myvector.begin()+4,5);
std::fill (myvector.begin()+3,myvector.end()-2,8);
std::cout << "myvector contains:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
Output:
myvector contains: 5 5 5 8 8 8 0 0
4.std::fill_n
template <class OutputIterator, class Size, class T>
void fill_n (OutputIterator first, Size n, const T& val);
template <class OutputIterator, class Size, class T>
OutputIterator fill_n (OutputIterator first, Size n, const T& val);
- OutputIterator first:区间的开始
- Size n
- const T& val
- 作用:从first位置开始填充n个val值
- 返回值:
98:没有返回值。11:返回填充完成后位置的下一个位置
- 实现:
template <class OutputIterator, class Size, class T>
OutputIterator fill_n (OutputIterator first, Size n, const T& val)
{
while (n > 0) {
*first = val;
++first;
--n;
}
return first;
}
#include
#include
#include
int main () {
std::vector<int> myvector (8,10);
std::fill_n (myvector.begin(),4,20);
std::fill_n (myvector.begin()+3,3,33);
std::cout << "myvector contains:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
Output:
myvector contains: 20 20 20 33 33 33 10 10
5.std::find
template <class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val);
- InputIterator first:区间的开始
- InputIterator last:区间的结束
- const T& val:待查找的值val
- 作用:
[first,last)区间内查找第一个值为val的元素的位置
- 返回值:返回第一个值为val的元素的位置
- 实现:
template<class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val)
{
while (first!=last) {
if (*first==val) return first;
++first;
}
return last;
}
#include
#include
#include
int main () {
int myints[] = { 10, 20, 30, 40 };
int * p;
p = std::find (myints, myints+4, 30);
if (p != myints+4)
std::cout << "Element found in myints: " << *p << '\n';
else
std::cout << "Element not found in myints\n";
std::vector<int> myvector (myints,myints+4);
std::vector<int>::iterator it;
it = find (myvector.begin(), myvector.end(), 30);
if (it != myvector.end())
std::cout << "Element found in myvector: " << *it << '\n';
else
std::cout << "Element not found in myvector\n";
return 0;
}
Output:
Element found in myints: 30
Element found in myvector: 30
6.std::find_end
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2, ForwardIterator2 last2);
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
- ForwardIterator1 first1:区间1的开始
- ForwardIterator1 last1:区间1的结束
- ForwardIterator2 first2:区间2的开始
- ForwardIterator2 last2:区间2的结束
- BinaryPredicate pred:
自定义比较规则
,可以是函数指针或者仿函数对象、lambda表达式
- 作用:
在[first1,last1)区间的查找满足[first2,last2)区间中的所有元素
- 返回值:
如果[first1,last1)区间查找到[first2,last2)区间中的所有元素,返回[first2,last2)区间在[first1,last1)区间最后一个满足条件的开始位置
- 实现:
template<class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2)
{
if (first2==last2) return last1;
ForwardIterator1 ret = last1;
while (first1 != last1)
{
ForwardIterator1 it1 = first1;
ForwardIterator2 it2 = first2;
while (*it1 == *it2) {
++it1; ++it2;
if (it2==last2) { ret=first1; break; }
if (it1==last1) return ret;
}
++first1;
}
return ret;
}
#include
#include
#include
bool myfunction (int i, int j) {
return (i==j);
}
int main () {
int myints[] = {1,2,3,4,5,1,2,3,4,5};
std::vector<int> haystack (myints,myints+10);
int needle1[] = {1,2,3};
std::vector<int>::iterator it;
it = std::find_end (haystack.begin(), haystack.end(), needle1, needle1+3);
if (it!=haystack.end())
std::cout << "needle1 last found at position " << (it-haystack.begin()) << '\n';
int needle2[] = {4,5,1};
it = std::find_end (haystack.begin(), haystack.end(), needle2, needle2+3, myfunction);
if (it!=haystack.end())
std::cout << "needle2 last found at position " << (it-haystack.begin()) << '\n';
return 0;
}
Output:
needle1 found at position 5
needle2 found at position 3
7.std::find_first_of
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2, ForwardIterator2 last2);
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2, ForwardIterator2 last2,BinaryPredicate pred);
template <class InputIterator, class ForwardIterator>
InputIterator find_first_of (InputIterator first1, InputIterator last1,ForwardIterator first2, ForwardIterator last2);
template <class InputIterator, class ForwardIterator, class BinaryPredicate>
InputIterator find_first_of (InputIterator first1, InputIterator last1,ForwardIterator first2, ForwardIterator last2,BinaryPredicate pred);
- InputIterator first1:区间的开始
- InputIterator last1:区间的结束
- ForwardIterator first2:区间的开始
- ForwardIterator last2:区间的结束
- BinaryPredicate pred:
自定义比较规则
,可以是函数指针或者仿函数对象、lambda表达式
- 作用:在[first1,last1)区间内找[first2,last2)区间内任意一个元素出现的位置
- 返回值:如果在[first1,last1)区间内找[first2,last2)区间内
任意一个元素
返回该位置的迭代器
- 实现:
template<class InputIterator, class ForwardIterator>
InputIterator find_first_of ( InputIterator first1, InputIterator last1,ForwardIterator first2, ForwardIterator last2)
{
while (first1!=last1) {
for (ForwardIterator it=first2; it!=last2; ++it) {
if (*it==*first1)
return first1;
}
++first1;
}
return last1;
}
#include
#include
#include
#include
bool comp_case_insensitive (char c1, char c2) {
return (std::tolower(c1)==std::tolower(c2));
}
int main () {
int mychars[] = {'a','b','c','A','B','C'};
std::vector<char> haystack (mychars,mychars+6);
std::vector<char>::iterator it;
int needle[] = {'A','B','C'};
it = find_first_of (haystack.begin(), haystack.end(), needle, needle+3);
if (it!=haystack.end())
std::cout << "The first match is: " << *it << '\n';
it = find_first_of (haystack.begin(), haystack.end(),
needle, needle+3, comp_case_insensitive);
if (it!=haystack.end())
std::cout << "The first match is: " << *it << '\n';
return 0;
}
Output:
The first match is: A
The first match is: a