C++ Primer 学习中。。

简单记录下我的学习过程 (代码为主)

//全部容器适用


equal(b,e,b2)       //用来比較第一个容器[b,e)和第二个容器b2开头,是否相等
equal(b,e,b2,p)


mismatch(b,e,b2)    //用来查找两个容器中第一个不相等的数据,返回迭代器
mismatch(b,e,b2,p)


lexicographical_compare(b,e,b2,e2)      //用来比較第一个区间是否比第二个区间小
lexicographical_compare(b,e,b2,e2,p)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<list>
#include<algorithm>
using namespace std;/*****************************************
//全部容器适用
equal(b,e,b2)       //用来比較第一个容器[b,e)和第二个容器b2开头。是否相等
equal(b,e,b2,p)
mismatch(b,e,b2)    //用来查找两个容器中第一个不相等的数据,返回迭代器
mismatch(b,e,b2,p)
lexicographical_compare(b,e,b2,e2)      //用来比較第一个区间是否比第二个区间小
lexicographical_compare(b,e,b2,e2,p)
*****************************************//*************************************************************************************
std::equal                     全部排序容器适用                           algorithm
--------------------------------------------------------------------------------------
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 );//eg:
template <class InputIterator1, class InputIterator2>bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
{while ( first1!=last1 ){if (!(*first1 == *first2))   // or: if (!pred(*first1,*first2)), for pred versionreturn false;++first1; ++first2;}return true;
}
*************************************************************************************//*************************************************************************************
std::mismatch                   全部排序容器适用                           algorithm
--------------------------------------------------------------------------------------
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 );//eg:
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 the pred version{ ++first1; ++first2; }return make_pair(first1,first2);
}
*************************************************************************************/
/*************************************************************************************
std::lexicographical_compare               全部排序容器适用                 algorithm
--------------------------------------------------------------------------------------
template <class InputIterator1, class InputIterator2>bool lexicographical_compare ( InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2 );template <class InputIterator1, class InputIterator2, class Compare>bool lexicographical_compare ( InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,Compare comp );
//eg:
template <class InputIterator1, class InputIterator2>bool lexicographical_compare ( InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2 )
{while (first1!=last1){if (first2==last2 || *first2<*first1) return false;else if (*first1<*first2) return true;first1++; first2++;}return (first2!=last2);
}
*************************************************************************************/bool mypredicate (int i, int j)
{return (i==j);
}bool mycomp (char c1, char c2)
{return tolower(c1)<tolower(c2);
}int main()
{//equal(b,e,b2)       //用来比較第一个容器[b,e)和第二个容器b2开头,是否相等//equal(b,e,b2,p)int myints[] = {20,40,60,80,100};          //   myints: 20 40 60 80 100vector<int>myvector (myints,myints+5);     // myvector: 20 40 60 80 100// using default comparison:if (equal (myvector.begin(), myvector.end(), myints))cout << "The contents of both sequences are equal." << endl;elsecout << "The contents of both sequences differ." << endl;myvector[3]=81;                            // myvector: 20 40 60 81 100// using predicate comparison:if (equal (myvector.begin(), myvector.end(), myints, mypredicate))cout << "The contents of both sequences are equal." << endl;elsecout << "The contents of both sequences differ." << endl;cout<<endl;/**-----------------------------------------------------------------------------Output:The contents of both sequences are equal.The contents of both sequences differ.-----------------------------------------------------------------------------**///mismatch(b,e,b2)    //用来查找两个容器中第一个不相等的数据,返回迭代器//mismatch(b,e,b2,p)list<int> mylist;for (int i=1; i<6; i++) mylist.push_back (i*10);   // mylist: 10 20 30 40 50int myints2[] = {10,20,80,40,1024};              // myints2: 10 20 80 40 1024pair<list<int>::iterator,int*> mypair;// using default comparison:mypair = mismatch (mylist.begin(), mylist.end(), myints2);cout << "First mismatching elements: " << *mypair.first;cout << " and " << *mypair.second << endl;mypair.first++;mypair.second++;// using predicate comparison:mypair = mismatch (mypair.first, mylist.end(), mypair.second, mypredicate);cout << "Second mismatching elements: " << *mypair.first;cout << " and " << *mypair.second << endl;cout <<endl;/**-----------------------------------------------------------------------------Output:First mismatching elements: 30 and 80Second mismatching elements: 50 and 1024-----------------------------------------------------------------------------**///lexicographical_compare(b,e,b2,e2)      //用来比較第一个区间是否比第二个区间小  (长度和ASCII码)//lexicographical_compare(b,e,b2,e2,p)char first[]="apple";         // 5 letterschar second[]="apart";        // 5 letters   eg:apartmentcout << "Using default comparison (operator<): ";if (lexicographical_compare(first,first+5,second,second+5))cout << first << " is less than " << second << endl;else if (lexicographical_compare(second,second+5,first,first+5))cout << first << " is greater than " << second << endl;elsecout << first << " and " << second << " are equivalent\n";cout << "Using mycomp as comparison object: ";if (lexicographical_compare(first,first+5,second,second+5,mycomp))cout << first << " is less than " << second << endl;else if (lexicographical_compare(second,second+5,first,first+5,mycomp))cout << first << " is greater than " << second << endl;elsecout << first << " and " << second << " are equivalent\n";/**-----------------------------------------------------------------------------Output:Using default comparison (operator<): apple is greater than apartUsing mycomp as comparison object: apple is greater than apart-----------------------------------------------------------------------------**/return 0;
}

转载于:https://www.cnblogs.com/cxchanpin/p/7289702.html

STL_算法_区间的比較(equal、mismatch、 lexicographical_compare)相关推荐

  1. STL_算法_元素计数(count、count_if)

    C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) count . count_if #include<iostream> #include<cstdio&g ...

  2. STL_算法(25)_逆转和旋转 revers() 、reverse_copy()、rotate()、rotate_copy()、

    STL_算法(25)_逆转和旋转 revers() . reverse_copy(). rotate(). rotate_copy(). STL变序性算法 revers() . reverse_cop ...

  3. cb32a_c++_STL_算法_查找算法_(5)adjacent_find

    cb32a_c++_STL_算法_查找算法_(5)adjacent_find adjacent_find(b,e),b,begin(),e,end() adjacent_find(b,e,p),p-p ...

  4. 【Matlab】智能优化算法_蜻蜓优化算法DA

    [Matlab]智能优化算法_蜻蜓优化算法DA 1.背景介绍 2.灵感 3.公式推导 3.1 勘探和开发操作 4.算法流程图 5.文件结构 6.伪代码 7.详细代码及注释 7.1 DA.m 7.2 d ...

  5. Python_机器学习_算法_第1章_K-近邻算法

    Python_机器学习_算法_第1章_K-近邻算法 文章目录 Python_机器学习_算法_第1章_K-近邻算法 K-近邻算法 学习目标 1.1 K-近邻算法简介 学习目标 1 什么是K-近邻算法 1 ...

  6. JS数据结构与算法_链表

    上一篇:JS数据结构与算法_栈&队列 下一篇:JS数据结构与算法_集合&字典 写在前面 说明:JS数据结构与算法 系列文章的代码和示例均可在此找到 上一篇博客发布以后,仅几天的时间竟然 ...

  7. 深度学习算法和机器学习算法_啊哈! 4种流行的机器学习算法的片刻

    深度学习算法和机器学习算法 Most people are either in two camps: 大多数人都在两个营地中: I don't understand these machine lea ...

  8. java 蓝桥杯 算法训练 区间k大数查询(题解)

    试题 算法训练 区间k大数查询 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个数n,表示 ...

  9. 算法_深度LSTM笔记[博]

    原创博客链接:算法_深度LSTM笔记 本文适合有一定基础同学的复习使用,不适合小白入门,入门参考本文参考文献第一篇 结构_静态综合图 结构_分步动图 进一步,向量化参数和引入问题 1, cell 的状 ...

最新文章

  1. JavaScript对象this指向(普通键this指向 非指向函数的键)
  2. bzoj3786: 星系探索
  3. Android实现连续并排的若干个TextView单击改变背景颜色达到选项卡Tab栏切换效果...
  4. 【翻译】C#编程语言和JAVA编程语言的比较(下)
  5. 干货 | C语言系列3——常量,运算符,常用数学函数......
  6. miniuidatagrid只允许修改某一列_State Processor API:如何读取,写入和修改 Flink 应用程序的状态...
  7. 有功功率 无功功率 功率因数
  8. IE FF css兼容
  9. Google 网站打不开
  10. 2013年3月份计算机二级c语言最新上机题库(搜索关键字版),20133月份计算机二级C语言上机题库(十分强大).doc...
  11. 微信小程序点播音频服务器,微信小程序无法播放本地音频
  12. postgres 判断null_postgresql基础:null的那些坑
  13. 7岁儿童智力检测题_7岁-11岁儿童智商测试题
  14. python去掉最高分和最低分求平均飞_Excel里去掉最高分最低分再求平均分教程
  15. java调起本地摄像头,利用openCV进行人脸识别(一)
  16. 网易云轻舟设计理念与技术选型
  17. 华为认证专用模拟器 企业内部业务网络设计
  18. java lua_请问该如何在Java中使用Lua脚本语言?
  19. 12月世界燕窝滋补品展|上海燕博会|冻干燕窝展联合滋补生态大会,共赴新未来
  20. Excel批量去除绿三角的2种技巧,方法1可以一劳永逸【附视频操作】

热门文章

  1. Python笔记-BeautifulSoup基本用法(通过标签及class定位元素)
  2. Java笔记-RabbitMQ中生产者端confirm模式
  3. Qt工作笔记-设置窗体底纹以及控件透明度
  4. Qt-IP地址查询工具(使用HTTP GET方法)
  5. C++|Qt最简单的http的get请求
  6. Java基础入门笔记-算术运算符
  7. matlab 同一坐标系 散点图 t,matlab上机练习
  8. 51单片机扩展io口实验c语言,【51单片机】普通I/O口模拟SPI口C语言程序
  9. SQLite允许向一个integer型字段中插入字符串
  10. html5如何实现播放下一首,使用HTML5 SoundCloud播放器小部件如何以编程方式跳到另一首曲目而不会导致播放第二不需要的曲目?...