青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Benjamin

靜以修身,儉以養德,非澹薄無以明志,非寧靜無以致遠。
隨筆 - 398, 文章 - 0, 評論 - 196, 引用 - 0
數據加載中……

stl的算法(一):對序列進行只讀操作(查找、搜索等)

Stl的算法的不更改序列操作主要有以下12項:
for_each、find、find_if、find_end、find_first_of、adjacent_find
count、count_if、mismatch、equal、search、search_n

1、 for_each:遍歷某個區域內每個元素
原型:template <class InputIterator, class Function>
   Function for_each (InputIterator first, InputIterator last, Function f);
形參:first、last確定那個一個區域;f是函數指針,必須重載()
例子:// for_each example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
 
void myfunction (int i) {
 cout << " " << i;
}
 
struct myclass {
 void operator() (int i) {cout << " " << i;}
} myobject;
 
int main () {
 vector<int> myvector;
 myvector.push_back(10);
 myvector.push_back(20);
 myvector.push_back(30);
 
 cout << "myvector contains:";
 for_each (myvector.begin(), myvector.end(), myfunction);
 
 // or:
 cout << "\nmyvector contains:";
 for_each (myvector.begin(), myvector.end(), myobject);
 
 cout << endl;
 
 return 0;
}
 

2、 find:返回在迭代器指定的范圍內第一個匹配的值,如果沒有找到返回last
原型:template <class InputIterator, class T>
         InputIterator find ( InputIterator first, InputIterator last, const T& value );
形參:見for_each

例子:
// 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;
}
 

3、 find_if
原型:template <class InputIterator, class Predicate>
   InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred );
形參:同上
功能:變量first和end間的區域,如果調用pred都返回false,則函數返回end;如果返回true,
則直接break,返回當前iterator。這里查找的是一個element
例子:
// 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;
}
 
4、 find_end:
原型: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 );

功能:搜索first2,last2在first1,last1中最后一次匹配的位置
形參: first1、end1和first2、end2確定一個序列,pred的含義和上面的函數雷同
例子:// 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;
}

5、 find_first_of:
原型:template <class ForwardIterator1, class ForwardIterator2>
   ForwardIterator1 find_first_of ( ForwardIterator1 first1, ForwardIterator1 last1,
                                    ForwardIterator2 first2, ForwardIterator2 last2 );

template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
   ForwardIterator1 find_first_of ( ForwardIterator1 first1, ForwardIterator1 last1,
                                    ForwardIterator2 first2, ForwardIterator2 last2,
                                    BinaryPredicate pred );

功能:搜索first2,last2在first1,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;
}
6、 adjacent_find:
原型:template <class ForwardIterator>
   ForwardIterator adjacent_find ( ForwardIterator first, ForwardIterator last );
template <class ForwardIterator, class BinaryPredicate>
   ForwardIterator adjacent_find ( ForwardIterator first, ForwardIterator last,
                                   BinaryPredicate pred );

功能:查找連續重復的元素
形參:見find
例子:// 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
 

7、 count:
原型:template <class InputIterator, class T>
 typename iterator_traits<InputIterator>::difference_type
count ( ForwardIterator first, ForwardIterator last, const T& value );
功能:統計value在first、end間出現的次數
例子:

// count algorithm example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
 
int main () {
 int mycount;

 // counting elements in array:
 int myints[] = {10,20,30,30,20,10,10,20};   // 8 elements
 mycount = (int) count (myints, myints+8, 10);
 cout << "10 appears " << mycount << " times.\n";

 

 // counting elements in container:
 vector<int> myvector (myints, myints+8);

 mycount = (int) count (myvector.begin(), myvector.end(), 20);
 cout << "20 appears " << mycount << " times.\n";

 return 0;
}

8、 count_if
原型:template <class InputIterator, class Predicate>
 typename iterator_traits<InputIterator>::difference_type
count_if ( ForwardIterator first, ForwardIterator last, Predicate pred );
功能:返回滿足pred條件的元素個數
例子:// count_if example

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

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

int main () {
 int mycount;

 vector<int> myvector;

 for (int i=1; i<10; i++) myvector.push_back(i); // myvector: 1 2 3 4 5 6 7 8 9

 

 mycount = (int) count_if (myvector.begin(), myvector.end(), IsOdd);
 cout << "myvector contains " << mycount << " odd values.\n";

 return 0;
}

9、 mismatch
原型: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 );
功能:返回first2和end2在first1和end1內不匹配的位置
例子:// mismatch algorithm example

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

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

int main () {
 vector<int> myvector;

 for (int i=1; i<6; i++) myvector.push_back (i*10); // myvector: 10 20 30 40 50

 int myints[] = {10,20,80,320,1024};                //   myints: 10 20 80 320 1024

 pair<vector<int>::iterator,int*> mypair;

 
 // using default comparison:
 mypair = mismatch (myvector.begin(), myvector.end(), myints);

 cout << "First mismatching elements: " << *mypair.first;
 cout << " and " << *mypair.second << endl;;

 mypair.first++; mypair.second++;


 // using predicate comparison:
 mypair = mismatch (mypair.first, myvector.end(), mypair.second, mypredicate);

 cout << "Second mismatching elements: " << *mypair.first;
 cout << " and " << *mypair.second << endl;;

 return 0;
}
Output:First mismatching elements: 30 and 80
Second mismatching elements: 40 and 320

10、equal
原型: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 );

功能:比較從first2開始的一個序列是否和first1、end1的序列相等
例子:
// equal algorithm example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

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

int main () {

 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;

 return 0;
}

11、search
原型: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 );
功能:和find_end類似,但不是end,而是first
例子:#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

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

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;

 return 0;
}

12、search_n
原型:template <class ForwardIterator, class Size, class T>
   ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,
                              Size count, const T& value );
template <class ForwardIterator, class Size, class T, class BinaryPredicate>
   ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,
                              Size count, const T& value, BinaryPredicate pred );
功能:在某個范圍內搜索值;如果沒找到返回end
例子:

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

bool mypredicate (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 = search_n (myvector.begin(), myvector.end(), 2, 30);

 if (it!=myvector.end())
    cout << "two 30s found at position " << int(it-myvector.begin()) << endl;
 else
    cout << "match not found" << endl;

 // using predicate comparison:
 it = search_n (myvector.begin(), myvector.end(), 2, 10, mypredicate);

 if (it!=myvector.end())
    cout << "two 10s found at position " << int(it-myvector.begin()) << endl;
 else
    cout << "match not found" << endl;

 return 0;
}

 

 

 

 

posted on 2011-12-27 13:41 Benjamin 閱讀(623) 評論(0)  編輯 收藏 引用 所屬分類: 泛型編程

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美国产亚洲精品久久久8v| 久久精品成人一区二区三区蜜臀| 国产精品在线看| 欧美freesex8一10精品| 午夜精品免费在线| 亚洲精品社区| 美女国内精品自产拍在线播放| 亚洲在线免费视频| 亚洲美女av在线播放| 国内一区二区三区| 国产欧美日韩另类一区 | 国产一区美女| 国产精品草草| 欧美另类人妖| 久热精品视频在线| 久久久人成影片一区二区三区观看 | 狠狠做深爱婷婷久久综合一区 | 久久免费少妇高潮久久精品99| 亚洲一区二区精品在线| 亚洲精品美女在线观看| 亚洲国产cao| 亚洲大片精品永久免费| 狠狠色狠狠色综合日日91app| 国产精品稀缺呦系列在线| 欧美日韩视频专区在线播放 | 欧美日韩你懂的| 欧美国产在线视频| 欧美福利一区| 欧美高清视频一区二区三区在线观看| 看欧美日韩国产| 看片网站欧美日韩| 玖玖在线精品| 免费不卡在线视频| 免费久久精品视频| 欧美第十八页| 欧美国产在线电影| 欧美日韩美女| 国产精品日产欧美久久久久| 国产精品萝li| 国产欧美91| 国产午夜精品美女毛片视频| 国产亚洲欧美aaaa| 激情一区二区三区| 亚洲国产另类久久久精品极度 | 校园春色国产精品| 久久国产精品亚洲va麻豆| 久久精品99国产精品| 久久久人成影片一区二区三区观看 | 91久久在线| 亚洲精品中文字幕在线观看| 一本色道久久综合狠狠躁篇怎么玩 | 亚洲日本va在线观看| 日韩一级在线观看| 亚洲性线免费观看视频成熟| 欧美一区国产一区| 久久综合中文| 亚洲乱码国产乱码精品精| 亚洲一卡二卡三卡四卡五卡| 欧美一区二区女人| 免费亚洲一区| 国产精品成人一区二区三区夜夜夜 | 国内成人自拍视频| 亚洲精品乱码久久久久久日本蜜臀 | 久久精品国产99| 欧美a级一区| 国产精品区二区三区日本| 欧美午夜在线视频| 狠狠88综合久久久久综合网| 亚洲激情偷拍| 午夜在线视频一区二区区别| 久久网站热最新地址| 亚洲精品免费一区二区三区| 午夜精品久久久久久久久久久久 | 久久久99国产精品免费| 欧美大秀在线观看| 一区二区高清| 久久久久久一区二区| 欧美色综合天天久久综合精品| 国产丝袜一区二区| 亚洲美女尤物影院| 久久精品国产久精国产一老狼| 亚洲高清网站| 亚洲欧美中文另类| 欧美91福利在线观看| 国产毛片久久| 99视频+国产日韩欧美| 久久人人看视频| 一区二区三区日韩精品| 久久天天躁狠狠躁夜夜av| 欧美午夜精品一区| 亚洲人成毛片在线播放女女| 久久电影一区| 99视频超级精品| 免费亚洲一区二区| 国产在线成人| 亚洲一二三区在线| 亚洲大黄网站| 欧美中文字幕| 国产精品99一区| 亚洲精品欧美极品| 久久久伊人欧美| 亚洲一区二区三区高清 | 亚洲精品久久久久久久久久久久久| 亚洲欧美激情诱惑| 欧美日韩三级| 亚洲美女91| 欧美高清视频www夜色资源网| 亚洲自拍偷拍网址| 国产精品hd| 亚洲视频自拍偷拍| 亚洲国产免费| 蜜桃久久av| 伊人激情综合| 久久久夜精品| 欧美亚洲日本网站| 国产精品男人爽免费视频1| 在线视频你懂得一区| 亚洲区在线播放| 欧美二区在线| 日韩视频在线一区| 亚洲二区三区四区| 欧美高清视频一区二区| 亚洲人体一区| 亚洲激情不卡| 欧美激情按摩在线| 日韩视频在线播放| 亚洲欧洲美洲综合色网| 欧美激情一区二区三区在线视频 | 久久精品国产久精国产一老狼| 国产日韩欧美在线观看| 欧美中文在线观看| 久久国产加勒比精品无码| 国模私拍一区二区三区| 久久综合九色| 老司机精品导航| 亚洲日本欧美| 日韩手机在线导航| 欧美性猛交视频| 欧美在线|欧美| 久久精品女人的天堂av| 国内一区二区在线视频观看| 米奇777超碰欧美日韩亚洲| 久久免费国产精品1| 亚洲国产综合视频在线观看| 亚洲国产91色在线| 欧美日韩国产三区| 午夜在线视频观看日韩17c| 欧美一级专区| 在线观看精品| 亚洲精品一区久久久久久| 欧美视频一区在线| 久久久久久高潮国产精品视| 久久亚洲综合网| 99精品久久| 亚洲欧美日韩一区二区三区在线| 国产综合色产在线精品| 亚洲第一精品影视| 国产精品黄视频| 免费日韩av片| 欧美视频不卡| 久久久久久色| 欧美国产日韩精品| 午夜精品久久久久久久99热浪潮| 欧美一级理论片| 亚洲九九九在线观看| 亚洲午夜精品17c| 依依成人综合视频| 一区二区高清在线| 精久久久久久久久久久| 亚洲精选一区二区| 国产亚洲欧美激情| 亚洲精品久久| 国模大胆一区二区三区| 日韩视频精品| 狠狠色丁香婷婷综合久久片| 亚洲人www| 国产一区二区高清不卡| 亚洲欧洲日韩女同| 国产在线观看一区| 亚洲美女精品一区| 精品9999| 亚洲永久精品国产| 99国产精品99久久久久久| 久久经典综合| 亚洲女同同性videoxma| 美女国产一区| 久久九九久精品国产免费直播| 欧美精品激情| 欧美77777| 国产午夜精品麻豆| 在线一区免费观看| 亚洲精品在线免费观看视频| 欧美一区高清| 午夜精品视频在线观看| 欧美激情亚洲激情| 欧美高清在线视频观看不卡| 国产在线不卡视频| 亚洲一区二区三区午夜| 在线亚洲高清视频| 欧美电影免费观看高清|