日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++ STL 算法精选之查找篇

發(fā)布時(shí)間:2025/6/17 c/c++ 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ STL 算法精选之查找篇 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.


1.查找類算法
adjacent_find(first,last);

查找區(qū)間[first,last)內(nèi)第一次出現(xiàn)連續(xù)的兩個(gè)相等的元素,并返回指向第一個(gè)元素的迭代器,連續(xù)元素之間的比較,默認(rèn)是==

adjacent_find(first,last,pred);

用途如上,但是元素之間的比較是通過函數(shù)pred來完成,pred接受兩個(gè)容器內(nèi)元素類型的元素,返回bool值

函數(shù)原型:
template <class ForwardIterator>
?? ForwardIterator adjacent_find ( ForwardIterator first, ForwardIterator last )
{
? ForwardIterator next=first; ++next;
? if (first != last)
??? while (next != last)
????? if (*first++ == *next++)? // or: if (pred(*first++,*next++)), for the pred version
??????? return first;
? return last;
}

?例子:
// adjacent_find example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool myfunction (int i, int j) {
? return (i==j);
}

int main () {
? int myints[] = {10,20,30,30,20,10,10,20};
? vector<int> myvector (myints,myints+8);
? vector<int>::iterator it;

? // using default comparison:
? it = adjacent_find (myvector.begin(), myvector.end());

? if (it!=myvector.end())
??? cout << "the first consecutive repeated elements are: " << *it << endl;

? //using predicate comparison:
? it = adjacent_find (++it, myvector.end(), myfunction);

? if (it!=myvector.end())
??? cout << "the second consecutive repeated elements are: " << *it << endl;
?
? return 0;
}
?

Output:
the first consecutive repeated elements are: 30
the second consecutive repeated elements are: 10


?find(first,last,value);
?查找區(qū)間[first,last)之間內(nèi)值為value的元素,返回迭代器類型,若沒找到,則返回迭代器末尾end 函數(shù)原型:
?template<class InputIterator, class T>
? InputIterator find ( InputIterator first, InputIterator last, const T& value )
? {
??? for ( ;first!=last; first++) if ( *first==value ) break;
??? return first;
? }
例子:// find example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main () {
? int myints[] = { 10, 20, 30 ,40 };
? int * p;

? // pointer to array element:
? p = find(myints,myints+4,30);
? ++p;
? cout << "The element following 30 is " << *p << endl;

? vector<int> myvector (myints,myints+4);
? vector<int>::iterator it;

? // iterator to vector element:
? it = find (myvector.begin(), myvector.end(), 30);
? ++it;
? cout << "The element following 30 is " << *it << endl;

? return 0;
}
?

Output:
The element following 30 is 40
The element following 30 is 40

find_if(first,last,pred);
返回區(qū)間[first,last)內(nèi)第一個(gè)使pred函數(shù)返回為真的元素的迭代器,否則返回last注意:pred接受一個(gè)參數(shù)函數(shù)原型:
template<class InputIterator, class Predicate>
? InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred )
? {
??? for ( ; first!=last ; first++ ) if ( pred(*first) ) break;
??? return first;
? }
例子:// find_if example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool IsOdd (int i) {
? return ((i%2)==1);
}

int main () {
? vector<int> myvector;
? vector<int>::iterator it;

? myvector.push_back(10);
? myvector.push_back(25);
? myvector.push_back(40);
? myvector.push_back(55);

? it = find_if (myvector.begin(), myvector.end(), IsOdd);
? cout << "The first odd value is " << *it << endl;

? return 0;
}
?

Output:
The first odd value is 25

?

find_first_of(first1,last1,first2,last2);
find_first_of(first1,last1,first2,last2,pred);
返回一個(gè)迭代器,使得[first2,last2)之中任意一個(gè)元素第一次出現(xiàn)在區(qū)間[first1,last1)中。
默認(rèn)比較為==,當(dāng)然也可以自己定義pred函數(shù)(接受2個(gè)參數(shù)),返回bool型 函數(shù)原型:
template<class ForwardIterator1, class ForwardIterator2>
? ForwardIterator1 find_first_of ( ForwardIterator1 first1, ForwardIterator1 last1,
?????????????????????????????????? ForwardIterator2 first2, ForwardIterator2 last2)
{
? for ( ; first1 != last1; ++first1 )
??? for (ForwardIterator2 it=first2; it!=last2; ++it)
????? if (*it==*first1)????????? // or: if (comp(*it,*first)) for the pred version
??????? return first1;
? return last1;
}
例子:// find_first_of example
#include <iostream>
#include <algorithm>
#include <cctype>
#include <vector>
using namespace std;

bool comp_case_insensitive (char c1, char c2) {
? return (tolower(c1)==tolower(c2));
}

int main () {
? int mychars[] = {'a','b','c','A','B','C'};
? vector<char> myvector (mychars,mychars+6);
? vector<char>::iterator it;

? int match[] = {'A','B','C'};

? // using default comparison:
? it = find_first_of (myvector.begin(), myvector.end(), match, match+3);

? if (it!=myvector.end())
??? cout << "first match is: " << *it << endl;

? // using predicate comparison:
? it = find_first_of (myvector.begin(), myvector.end(),
????????????????????? match, match+3, comp_case_insensitive);

? if (it!=myvector.end())
??? cout << "first match is: " << *it << endl;
?
? return 0;
}
?

Output:
First match is: A
First match is: a

?

find_end(first1,last1,first2,last2);
find_end(first1,last1,first2,last2,pred);
返回一個(gè)元素迭代器,使得在區(qū)間[first1,last1)中最后一次出現(xiàn)[fiest2,last2),
默認(rèn)比較為==,當(dāng)然也可以寫自己的比較函數(shù)pred,接受兩個(gè)參數(shù),返回bool值函數(shù)原型:
template<class ForwardIterator1, class ForwardIterator2>
? ForwardIterator1 find_end ( ForwardIterator1 first1, ForwardIterator1 last1,
????????????????????????????? ForwardIterator2 first2, ForwardIterator2 last2)
{
? ForwardIterator1 it1, limit, ret;
? ForwardIterator2 it2;

? limit=first1; advance(limit,1+distance(first1,last1)-distance(first2,last2));
? ret=last1;

? while (first1!=limit)
? {
??? it1 = first1; it2 = first2;
??? while (*it1==*it2)????????? // or: while (pred(*it1,*it2)) for the pred version
????? { ++it1; ++it2; if (it2==last2) {ret=first1;break;} }
??? ++first1;
? }
? return ret;
}
例子:// find_end example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool myfunction (int i, int j) {
? return (i==j);
}

int main () {
? int myints[] = {1,2,3,4,5,1,2,3,4,5};
? vector<int> myvector (myints,myints+10);
? vector<int>::iterator it;

? int match1[] = {1,2,3};

? // using default comparison:
? it = find_end (myvector.begin(), myvector.end(), match1, match1+3);

? if (it!=myvector.end())
??? cout << "match1 last found at position " << int(it-myvector.begin()) << endl;

? int match2[] = {4,5,1};

? // using predicate comparison:
? it = find_end (myvector.begin(), myvector.end(), match2, match2+3, myfunction);

? if (it!=myvector.end())
??? cout << "match2 last found at position " << int(it-myvector.begin()) << endl;
?
? return 0;
}
?

Output:
Match found at position 5
Match found at position 3
??

?

總結(jié)

以上是生活随笔為你收集整理的C++ STL 算法精选之查找篇的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 天天看天天爽 | 春色导航| 亚洲中文在线一区 | 免费毛片在线播放免费 | 深夜福利一区二区三区 | 欧美少妇xx | 欧美日韩一区二区三 | 国产传媒av | 欧美在线看片 | 午夜福利视频 | 牛牛精品一区二区 | caopor在线视频 | 日本一区二区视频 | av导航网站 | 久久久久性 | 日韩一区二区欧美 | jizz成熟丰满老女人 | 操亚洲女人 | 国产一区二区三区视频免费在线观看 | 午夜神马影院 | 成年男女免费视频 | 国产视频久久久 | 久久国产精品久久久久久 | 日韩在线免费视频 | 欧美肥老妇视频九色 | 爱情岛论坛自拍亚洲品质极速最新章 | 在线观看国产一区二区三区 | 99re热视频| 久久无码人妻精品一区二区三区 | 国产无套内射又大又猛又粗又爽 | 欧美日韩一区不卡 | 日本一区二区久久 | 操碰在线视频 | 大尺度av在线 | 鲁一鲁一鲁一鲁一av | 亚洲熟妇一区 | 性喷潮久久久久久久久 | 国产又粗又长视频 | 日韩欧美视频一区 | 不卡av电影在线 | 国产一级片视频 | 草草草在线 | 日本人妖在线 | 性xxxx欧美 | 成年视频在线 | 富婆如狼似虎找黑人老外 | 亚洲国产精品无码专区 | 人妻毛片 | 欧美精品片 | 丁香激情综合 | 日本免费一级片 | 深夜福利一区 | 岛国精品视频 | 精品亚洲天堂 | 影音先锋成人在线 | 风间由美在线视频 | tube日本69第一次 | 日批黄色 | 少妇av网 | 五月天中文字幕mv在线 | 免费色网址 | 国产噜噜噜噜噜久久久久久久久 | 午夜视频在线观看国产 | 麻豆最新 | 污视频在线观看免费 | 午夜天堂av | 国产草草草| 91在线超碰 | 欧美视频一区二区 | 一级国产视频 | 午夜在线看 | 色综合久久88 | 24小时日本在线www免费的 | 性一交一乱一色一视频麻豆 | 天堂资源站 | 国产做爰xxxⅹ久久久精华液 | 实拍女处破www免费看 | 精品人妻人人做人人爽夜夜爽 | 欧美黄色大片免费观看 | 欧洲精品码一区二区三区免费看 | 日韩av在线免费看 | 国产寡妇亲子伦一区二区三区四区 | 亚洲图片 欧美 | 日本高清视频一区二区三区 | 性淫bbwbbwbbw| 91精彩刺激对白 | 日韩一级片网址 | 亚洲春色一区二区三区 | 国产精品久久久久久久久免费相片 | 六月婷婷在线观看 | 欧美一区二区三区在线观看 | 香港一级淫片免费放 | 亚洲国产精品久久久久久 | 亚洲免费在线 | 黄色网址av | 久久精品偷拍视频 | 国产ts网站 | 日韩三级麻豆 | 日本一区二区三区免费在线观看 |