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 version
return 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 100
vector<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;
else
cout << "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;
else
cout << "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 50 int myints2[] = {10,20,80,40,1024}; // myints2: 10 20 80 40 1024 pair<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 80
Second mismatching elements: 50 and 1024
-----------------------------------------------------------------------------**/ //lexicographical_compare(b,e,b2,e2) //用来比較第一个区间是否比第二个区间小 (长度和ASCII码)
//lexicographical_compare(b,e,b2,e2,p) char first[]="apple"; // 5 letters
char second[]="apart"; // 5 letters eg:apartment cout << "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;
else
cout << 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;
else
cout << first << " and " << second << " are equivalent\n";
/**-----------------------------------------------------------------------------
Output:
Using default comparison (operator<): apple is greater than apart
Using mycomp as comparison object: apple is greater than apart
-----------------------------------------------------------------------------**/ return 0;
}

STL_算法_区间的比較(equal、mismatch、 lexicographical_compare)的更多相关文章

  1. cb36a_c++_STL_算法_区间的比较equal_mismatch_lexicographical_compare

    *cb36a_c++_STL_算法_区间的比较equal_mismatch_lexicographical_compare 区间:容器中的全部数据或者部分数据,都叫做区间 equal(b,e,b2), ...

  2. STL_算法_查找算法(lower_bound、upper_bound、equal_range)

    C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) //全部容器适用(O(log(n)))    已序区间查找算法 lower_bound()        //找第一个符合的 ...

  3. STL_算法_查找算法(find、find_if)

    C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) find . find_if /**********************线性查找O(n) find(); find_if ...

  4. STL_算法_逆转(reverse,reverse_copy)

    C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) //全部容器适用 reverse(b,e)        //逆转区间数据 reverse_copy(b,e,b2) /** ...

  5. STL_算法_查找算法(binary_search、includes)

    C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) 全部容器适用(O(log(n)))     已序区间查找算法 binary_search             //二分查 ...

  6. STL_算法_中使用的函数对象

    写在前面: STL算法中的 函数对象的功能: (1).都是提供一种比较的函数,比较相邻的左右两个值的 相等/大小 等的关系, (2).返回值都是bool :该返回值 貌似是指明 遍历元素是否还要继续往 ...

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

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

  8. STL_算法_依据第n个元素排序(nth_element)

    C++ Primer 学习中... 简单记录下我的学习过程 (代码为主) //全部容器适用 nth_element(b,n,e) nth_element(b,n,e,p) 对照:partition() ...

  9. STL_算法_局部排序(partial_sort、partial_sort_copy)

    C++ Primer 学习中. . . 简单记录下我的学习过程 (代码为主) /***************************************** // partial_sort(b, ...

随机推荐

  1. Java容器类解析

    1:集合 Collection(单列集合) List(有序,可重复) ArrayList 底层数据结构是数组,默认长度是十 查询快,增删慢 add()时判断是否数组越界,数组扩容为原来的1.5倍 线程 ...

  2. 【剑指offer】(第 2 版)Java 题解

    [剑指offer](第 2 版)Java 题解 第一章 面试的流程 略... 第二章 面试需要的基础知识 面试题 1. 赋值运算符函数 面试题 2. 实现 Singleton 模式 Solution ...

  3. Comet OJ CCPC-Wannafly Winter Camp Day1 (Div2, online mirror) F.爬爬爬山-最短路(Dijkstra)(两个板子)+思维(mdzz...) zhixincode

    爬爬爬山 已经提交 已经通过 9.83% Total Submission:417 Total Accepted:41 题目描述 爬山是wlswls最喜欢的活动之一. 在一个神奇的世界里,一共有nn座 ...

  4. Poj2482 Stars in Your Window(扫描线)

    题面 Poj 题解 下面内容引用自"李煜东 <算法竞赛进阶指南>"(对原文略有缩减,侵删): 因为矩形的大小固定,所以矩形可以由它的任意一个顶点唯一确定.我们可以考虑把 ...

  5. Linux基础系列-Day1

    Linux发展简史 Unix:1969年由美国电话电报公司(AT&T)贝尔实验室的两个工程师所创造的操作系统,它允许计算机同时处理多用户和程序. BSD:重要的Unix分支,1977年由加州大 ...

  6. 【转载】Virtual Box下配置Host-Only联网方式详解

    其实网络这类相关的文章很多,我只是想结合自己的实际情况,把我的经验写下来,给那些需要的人们吧. 主机:windows 7 虚拟机:CentOS6.0 VirtualBox:4.2.0 虚拟机在安装好之 ...

  7. 【数论】【二次剩余】【map】hdu6128 Inverse of sum

    部分引用自:http://blog.csdn.net/v5zsq/article/details/77255048 所以假设方程 x^2+x+1=0 在模p意义下的解为d,则答案就是满足(ai/aj) ...

  8. 【二分】【半平面交】Gym - 101309J - Jungle Outpost

    发现炸毁的瞭望塔必然是连续的,其余下的部分是一个半平面. 二分答案,枚举所有可能的炸毁情况,做个半平面交,如果交出来面积是0,就可以保证不存在安全区域. #include<cstdio> ...

  9. 【数论】nefu119 组合素数

    算组合数中的素因子p的个数,基本同这题 http://www.cnblogs.com/autsky-jadek/p/6592194.html #include<cstdio> using ...

  10. 慢查询(找出mysql中超时的select语句)

    第一步:进入mysql界面 //查询多少秒 才属于慢查询. show variables like ‘long_query_time’ ; 第二步: //更改这个时间值  如:select语句执行超过 ...