(4)STL算法之比较

区间比较算法

(1)equal()

该函数可以实现两个容器对象的比较,如果两个对象相等,函数返回true,只能比较属于同一类型的不同变量。

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);

template <class InputIterator1, class InputIterator2>
bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
{
    while (first1!=last1)
    {
        if ( ! (*first1 == *first2) )   // or:   if( !pred(*first1,*first2) )    version 2
            return false;
        ++first1; 
        ++first2;
    }
    return true;
}

用法示例:

#include <iostream>     
#include <algorithm>    // std::equal
#include <vector>     

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);     // myvector: 20 40 60 80 100

    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;                                 // myvector: 20 40 60 81 100
    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;
}
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool relationship(int e1, int e2)
{
	int tmp = e1 + 2;
	return  e2 == tmp;
}
void Print(vector<int>& v)
{
	vector<int>::iterator it;
	for (it = v.begin(); it != v.end(); it++)
		cout << *it << ",  ";
	cout << endl;
}
void main()
{
	int arr1[] = { 1,2,3,4,5,6,7 };
	int arr2[] = { 3,4,5,6,7,8,9 };
	vector<int> v1(arr1, arr1 + 7);
	vector<int> v2(arr2, arr2 + 7);
	cout << "vector v1: " << endl;
	Print(v1);
	cout << "vector v2: " << endl;
	Print(v2);
	bool eq = equal(v1.begin(), v1.end(), v2.begin());
	if (eq)
	{
		cout << "v1 == v2" << endl;
	}
	else
	{
		cout << "v1 != to v2" << endl;
	}
	bool eq2 = equal(v1.begin(), v1.end(), v2.begin(), relationship);
	if (eq2)
	{
		cout << "v2[i]==v1[i]+2" << endl;
	}
	else
	{
		cout << "v2[i]!=v1[i]+2" << endl;
	}
}

(2)mismatch()用于寻找两个容器对象之间两两相异的对应元素,如果没有找到相异点,也并不代表两个对象完全相同,因为两个对象的容量不一定相同。要判断相同,就要使用equal算法。如果没有找到相异点,函数返回一个pair,以第一个对象的end元素和第二个对象的对应元素组成。如果找到相异点,则相异点的对应元素组成一个pair,并返回

template <class InputIterator1, class InputIterator2>
pair<InputIterator1, InputIterator2>  mismatch (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2);

template <class InputIterator1, class InputIterator2, class BinaryPredicate>
pair<InputIterator1, InputIterator2>  mismatch (InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, BinaryPredicate pred);

template <class InputIterator1, class InputIterator2>
pair<InputIterator1, InputIterator2>  mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
{
    while ( (first1!=last1) && (*first1==*first2) )  // or: pred(*first1,*first2), for version 2
    { 
        ++first1; 
        ++first2; 
    }
    return std::make_pair(first1,first2);
}

用法示例:

#include <iostream>
#include <algorithm>
#include <list>
#include <vector>
using namespace std;
void Print(int& Ele)
{
	cout << Ele << ", ";
}
void main()
{
	int arr1[] = { 1,2,3,4,7,5 };
	int arr2[] = { 1,2,3,5,6,4 };
	list<int> l1(arr1,arr1+6);
	vector<int> l2(arr2,arr2+6);
	pair<list<int>::iterator, vector<int>::iterator> p1;
	
	for_each(l1.begin(), l1.end(), Print);
	cout << endl;
	for_each(l2.begin(), l2.end(), Print);
	cout << endl;
	p1 = mismatch(l1.begin(), l1.end(), l2.begin());
	if (p1.first == l1.end())
	{
		cout << "no mismatch." << endl;
	}
	else
	{
		cout << "The first mismatch: ";
		cout << *p1.first << ",  " << *p1.second << endl;
	}
	p1 = mismatch(l1.begin(), l1.end(), l2.begin(), less_equal<int>());
	if (p1.first == l1.end())
	{
		cout << "no mismatch." << endl;
	}
	else
	{
		cout << "The first mismatch: ";
		cout << *p1.first << ",  " << *p1.second << endl;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值