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

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>
            国内成+人亚洲| 国产乱理伦片在线观看夜一区| 国产真实久久| 午夜精品久久久久久久99热浪潮 | 国内精品免费午夜毛片| 欧美一站二站| 欧美在线视频免费播放| 狠狠色狠狠色综合日日91app| 久久久在线视频| 久久久最新网址| 亚洲乱码精品一二三四区日韩在线 | 久久精品久久综合| 影音先锋日韩资源| 亚洲国产精品高清久久久| 久热爱精品视频线路一| 99国产精品国产精品久久 | 欧美一区二区黄| 亚洲欧美成人网| 激情五月综合色婷婷一区二区| 欧美成人资源| 欧美日韩免费在线视频| 性伦欧美刺激片在线观看| 性做久久久久久久免费看| 激情六月婷婷久久| 亚洲欧洲一区二区天堂久久| 欧美丝袜一区二区三区| 久久人人九九| 欧美视频一区二区| 久久久午夜电影| 欧美激情一区二区三区在线| 亚洲欧美在线另类| 裸体歌舞表演一区二区| 亚洲资源在线观看| 久久美女性网| 亚洲欧美综合国产精品一区| 久久婷婷国产麻豆91天堂| 亚洲无亚洲人成网站77777| 午夜亚洲福利| 在线视频欧美日韩精品| 久久久久网站| 亚洲欧美日韩系列| 欧美大片在线影院| 久久久91精品国产一区二区精品| 欧美精品福利在线| 久久综合999| 国产精品嫩草影院av蜜臀| 欧美激情女人20p| 国产一区二区你懂的| 99精品国产热久久91蜜凸| 亚洲国产va精品久久久不卡综合| 亚洲一区www| 99国产精品久久久久久久成人热 | 亚洲另类黄色| 亚洲国产精品一区二区第四页av| 亚洲制服少妇| 亚洲午夜电影网| 欧美福利视频一区| 欧美成人在线免费观看| 国产一区二区高清视频| 亚洲天堂av在线免费观看| 一区二区久久久久| 欧美激情在线| 亚洲激情视频网站| 亚洲六月丁香色婷婷综合久久| 久久婷婷亚洲| 欧美国产精品| 亚洲国产欧洲综合997久久| 久久激情视频| 久久人人九九| 亚洲第一在线视频| 久久久久这里只有精品| 久久亚洲国产成人| 激情文学综合丁香| 久久综合色播五月| 欧美激情一二三区| 日韩亚洲国产精品| 欧美日韩福利视频| 一本色道88久久加勒比精品| 日韩亚洲视频| 欧美午夜宅男影院在线观看| 亚洲免费大片| 亚洲免费伊人电影在线观看av| 欧美日韩精品一二三区| 宅男噜噜噜66一区二区| 午夜精品视频一区| 国内外成人免费激情在线视频| 欧美中文在线视频| 美女黄网久久| 一本到12不卡视频在线dvd| 欧美日韩午夜激情| 性做久久久久久| 欧美成人精品福利| 一区电影在线观看| 国产精品久久久久一区| 欧美综合二区| 亚洲激情另类| 欧美一区二区日韩一区二区| 国内精品视频久久| 欧美日韩大片| 欧美在线啊v| 亚洲国产日韩欧美在线99| 亚洲一区二区三区高清不卡| 国产麻豆日韩| 欧美成人性生活| 亚洲欧美日韩系列| 欧美激情综合色| 亚洲男同1069视频| 亚洲国产精品电影在线观看| 欧美性猛交99久久久久99按摩| 午夜精品成人在线| 亚洲国产欧美一区二区三区久久 | 国产人成精品一区二区三| 久久久噜噜噜久久狠狠50岁| 日韩视频免费在线| 快射av在线播放一区| 亚洲在线观看免费视频| 亚洲国产精品va| 国产伦精品一区二区三| 欧美电影资源| 久久九九精品| 亚洲综合国产激情另类一区| 免费观看亚洲视频大全| 欧美一区二区三区四区在线| 91久久精品国产91性色tv| 国产午夜精品一区二区三区视频 | 美腿丝袜亚洲色图| 欧美亚洲视频一区二区| 99re成人精品视频| 亚洲激情小视频| 欧美成年人视频网站| 欧美在线视频a| 亚洲一区二区毛片| 亚洲精品之草原avav久久| 娇妻被交换粗又大又硬视频欧美| 欧美午夜精品久久久久久人妖| 母乳一区在线观看| 久久久久se| 久久国产精品第一页| 亚洲欧美日韩成人| 中文在线一区| 亚洲图片你懂的| 一本色道久久综合狠狠躁篇怎么玩 | 国产一级揄自揄精品视频| 欧美无乱码久久久免费午夜一区 | 欧美在线视频二区| 亚洲综合导航| 亚洲欧美成人综合| 亚洲自拍偷拍网址| 性视频1819p久久| 久久aⅴ乱码一区二区三区| 午夜一级在线看亚洲| 亚洲综合视频1区| 亚洲影院免费| 欧美伊人久久久久久久久影院| 亚洲免费影视| 欧美永久精品| 久久久最新网址| 欧美承认网站| 欧美日韩一区二区三区四区五区| 欧美精品一区在线观看| 欧美日本一区二区高清播放视频| 欧美国产精品专区| 欧美日韩123| 国产精品一区一区| 国产一区视频观看| 亚洲高清一区二| 亚洲精品四区| 欧美一区二区三区的| 久久久夜夜夜| 亚洲福利电影| 一道本一区二区| 欧美一级一区| 欧美99在线视频观看| 欧美日韩在线播放三区| 国产香蕉97碰碰久久人人| 亚洲缚视频在线观看| 亚洲神马久久| 久久蜜桃香蕉精品一区二区三区| 欧美成人免费小视频| 99在线观看免费视频精品观看| 亚洲一区激情| 久久欧美中文字幕| 欧美手机在线| 亚洲国产精品精华液2区45| 一区二区国产在线观看| 久久aⅴ国产紧身牛仔裤| 欧美激情一二三区| 亚洲欧美日韩精品久久| 美女任你摸久久| 国产伦精品一区| 日韩亚洲精品视频| 久久久久亚洲综合| 亚洲毛片在线观看.| 久久福利精品| 国产精品久久中文| 亚洲人成人一区二区三区| 欧美一区二区在线免费播放| 亚洲黄色大片| 久久精品视频在线| 国产精品捆绑调教|