C++ Primer 学习中。

。。

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



search          //从左往右找第一个符合条件的子区间    全部容器适用



find_end 
//从右往左找第一个符合条件的子区间    全部容器适用

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<list>
#include<deque>
#include<algorithm>
using namespace std;
/*************************************************************************************
std::search 从左往右找子区间 全部容器适用 algorithm
--------------------------------------------------------------------------------------
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2 ); template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2.
BinaryPredicate pred ); eg:
template<class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2)
{
if (first2==last2) return first1; // specified in C++11 while (first1!=last1)
{
ForwardIterator1 it1 = first1;
ForwardIterator2 it2 = first2;
while (*it1==*it2) // or: while (pred(*it1,*it2)) for the pred version
{
++it1;
++it2;
if (it2==last2) return first1;
if (it1==last1) return last1;
}
++first1;
}
return last1;
}
**************************************************************************************/ /*************************************************************************************
std::find_end 从右往左找子区间 全部容器适用 algorithm
--------------------------------------------------------------------------------------
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 ); eg:
template<class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 find_end ( ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2)
{
if (first2==last2) return last1; // specified in C++11 ForwardIterator1 ret = last1; while (first1!=last1)
{
ForwardIterator1 it1 = first1;
ForwardIterator2 it2 = first2;
while (*it1==*it2) // or: while (pred(*it1,*it2)) for the pred version
{
++it1;
++it2;
if (it2==last2)
{
ret=first1;
break;
}
if (it1==last1) return ret;
}
++first1;
}
return ret;
}
**************************************************************************************/ bool mypredicate (int i, int j)
{
return (i==j);
}
bool myfunction (int i, int j)
{
return (i==j);
}
bool check(int elem,bool bo)//二元谓词
{
if(bo) return !(elem&1);
else return elem&1;
} int main ()
{
vector<int> myvector;
vector<int>::iterator it; // set some values: myvector: 10 20 30 40 50 60 70 80 90
for (int i=1; i<10; i++) myvector.push_back(i*10); // using default comparison:
int match1[] = {40,50,60,70}; it = search (myvector.begin(), myvector.end(), match1, match1+4); if (it!=myvector.end())
cout << "match1 found at position " << int(it-myvector.begin()) << endl;
else
cout << "match1 not found" << endl; // using predicate comparison:
int match2[] = {20,30,50};
it = search (myvector.begin(), myvector.end(), match2, match2+3, mypredicate); if (it!=myvector.end())
cout << "match2 found at position " << int(it-myvector.begin()) << endl;
else
cout << "match2 not found" << endl;
/**--------------------------------------find_end-----------------------------------------**/ int myints[] = {1,2,3,4,5,1,2,3,4,5,1};
deque<int> mydeque (myints,myints+11);
deque<int>::iterator itd; int match3[] = {1,2,3}; // using default comparison:
itd = find_end (mydeque.begin(), mydeque.end(), match3, match3+3); if (itd!=mydeque.end())
cout << "match1 last found at position " << int(itd-mydeque.begin()) << endl; int match4[] = {4,5,1}; // using predicate comparison:
itd = find_end (mydeque.begin(), mydeque.end(), match4, match4+3, myfunction); if (itd!=mydeque.end())
cout << "match2 last found at position " << int(itd-mydeque.begin()) << endl; /**--------------------------拓展找:偶数奇数奇数------------------------------**/ cout<<"\n1 2 3 4 5 1 2 3 4 5 1"<<endl;
vector<int> vec(myints,myints+11);
bool checkEven[3]={true,false,false};
//search
it=search(vec.begin(),vec.end(),checkEven,checkEven+3,check);
//find_end
itd=find_end(mydeque.begin(),mydeque.end(),checkEven,checkEven+3,check); if (it!=vec.end())
cout << " even odd odd found at position " << int(it-vec.begin()) << endl;
else
cout << "not found" << endl; if (itd!=mydeque.end())
cout << " even odd odd found at position " << int(itd-mydeque.begin()) << endl;
else
cout << "not found" << endl;
return 0;
} /******
Output:
match1 found at position 3
match2 not found
match1 last found at position 5
match2 last found at position 3 1 2 3 4 5 1 2 3 4 5 1
even odd odd found at position 3
even odd odd found at position 8
******/

最新文章

  1. RSA非对称性前端加密后端解密
  2. 关于 Netty Channel 的 Autoread
  3. google-breakpad
  4. 编程之美--2. Trie树 (Trie图)
  5. MySQL主从同步报Client requested master to start replication from position
  6. 安装oracle 11g详细过程仅供参考
  7. Swiper的简单实用方法
  8. javascript 正则匹配 提取所有 preg_match_all matchAll方法
  9. 1774: [Usaco2009 Dec]Toll 过路费
  10. Python的变量及简单数据类型
  11. 大数据量情况下高效比较两个list
  12. symfony command
  13. Linux安装python3.6
  14. P1654 OSU!-洛谷luogu
  15. 洛谷P4782 2-SAT问题
  16. CF1100D Dasha and Chess
  17. Django Forms 表单
  18. TJU Problem 2548 Celebrity jeopardy
  19. exgcd模板
  20. Cesium原理篇:3D Tiles(1)渲染调度【转】

热门文章

  1. Could not create connection to database server. Attempted reconnect 3 times. Giving up.错误
  2. CMSIS-RTOS 中断处理Interrupt Handling
  3. 1193 Eason
  4. django-xadmin使用之配置页眉页脚
  5. resin后台输出中文乱码的解决的方法!
  6. 深度学习 —— 使用 gensim 实现 word2vec
  7. 分布式架构中shiro
  8. 如何搭建Eclipse +Apache Tomcat配置Java开发环境
  9. 分享一个vueui axios-mock-adapter 中的用法
  10. How Javascript works (Javascript工作原理) (十二) 网络层探秘及如何提高其性能和安全性