当前位置: 首页 > news >正文

(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;
	}
}

 

相关文章:

  • linux中实现线程同步的6种方法
  • Linux TCP通信示例
  • QtTCP通信示例
  • (5)STL算法之复制
  • (6)STL算法之转换
  • (7)STL算法之交换赋值
  • 菱形继承问题
  • 改善程序与设计的N个做法
  • C++数据结构之顺序表
  • C++数据结构之单链表
  • (8)STL算法之替换
  • (9)STL算法之逆转旋转
  • NFS安装使用
  • STL之map(关联式容器)
  • STL之unordered_map
  • 网络传输文件的问题
  • 【RocksDB】TransactionDB源码分析
  • 【vuex入门系列02】mutation接收单个参数和多个参数
  • Brief introduction of how to 'Call, Apply and Bind'
  • C++类中的特殊成员函数
  • co.js - 让异步代码同步化
  • JAVA SE 6 GC调优笔记
  • Javascript 原型链
  • Java多线程(4):使用线程池执行定时任务
  • Linux后台研发超实用命令总结
  • overflow: hidden IE7无效
  • TypeScript实现数据结构(一)栈,队列,链表
  • 半理解系列--Promise的进化史
  • 后端_MYSQL
  • 每天一个设计模式之命令模式
  • 区块链技术特点之去中心化特性
  • 让你成为前端,后端或全栈开发程序员的进阶指南,一门学到老的技术
  • 如何在 Tornado 中实现 Middleware
  • 十年未变!安全,谁之责?(下)
  • 小程序上传图片到七牛云(支持多张上传,预览,删除)
  • 学习使用ExpressJS 4.0中的新Router
  • 由插件封装引出的一丢丢思考
  • 再次简单明了总结flex布局,一看就懂...
  • ​香农与信息论三大定律
  • #162 (Div. 2)
  • (iPhone/iPad开发)在UIWebView中自定义菜单栏
  • (Redis使用系列) Springboot 实现Redis 同数据源动态切换db 八
  • (Spark3.2.0)Spark SQL 初探: 使用大数据分析2000万KF数据
  • (vue)el-checkbox 实现展示区分 label 和 value(展示值与选中获取值需不同)
  • (差分)胡桃爱原石
  • (二)【Jmeter】专栏实战项目靶场drupal部署
  • (附源码)计算机毕业设计ssm基于B_S的汽车售后服务管理系统
  • (淘宝无限适配)手机端rem布局详解(转载非原创)
  • (一)80c52学习之旅-起始篇
  • (一)Thymeleaf用法——Thymeleaf简介
  • (转)Sublime Text3配置Lua运行环境
  • (轉貼) 資訊相關科系畢業的學生,未來會是什麼樣子?(Misc)
  • ***监测系统的构建(chkrootkit )
  • ***原理与防范
  • .NET Core WebAPI中封装Swagger配置