STL algorithm演算法lexicographical_compare(30),stlalgorithm

來源:互聯網
上載者:User

STL algorithm演算法lexicographical_compare(30),stlalgorithm

lexicographical_compare原型:

std::lexicographical_compare
default (1)
template <class InputIterator1, class InputIterator2>  bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1,                                InputIterator2 first2, InputIterator2 last2);
custom (2)
template <class InputIterator1, class InputIterator2, class Compare>  bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1,                                InputIterator2 first2, InputIterator2 last2,                                Compare comp);
該函數是按照字典序測試[frist1,last1)是否小於[first2,last2).

字典序是指按照字母在字典中出現的順序。

該函數使用opeartor<或者是comp進行比較。

如果兩個序列長度不同,並且短序列和長序列頭部完全一樣,例如example和examplee.那麼,長度大的字典序比短序的大。

其行為類似於:

123456789101112
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);}
一個簡單的例子:

#include <iostream>#include <vector>#include <algorithm>using namespace std;int main(int argv,char **argc){vector<char> v1{'h','e','l','l','o'};vector<char> v2{'h','e','l','l','o','o'};vector<char> v3{'h','e','l','m','o'};cout<<"v1=";for(char i:v1)cout<<i<<" ";cout<<endl;cout<<"v2=";for(char i:v2)cout<<i<<" ";cout<<endl;cout<<"v3=";for(char i:v3)cout<<i<<" ";cout<<endl;if(lexicographical_compare(v1.begin(),v1.end(),v2.begin(),v2.end()))cout<<"v1 is less than v2 "<<endl;elsecout<<"v2 is less than v1 "<<endl;if(lexicographical_compare(v1.begin(),v1.end(),v3.begin(),v3.end()))cout<<"v1 is less than v3 "<<endl;elsecout<<"v3 is less than v1 "<<endl;}
運行:



該函數是否只能比較字母呢?答案是肯定的,不是!

因為對於任意的可以使用opeartor<進行比較的對象都可以使用該函數!

一個簡單的例子:

#include <iostream>#include <vector>#include <algorithm>using namespace std;int main(int argv,char **argc){vector<int> v1{1,2,3,4};vector<int> v2{1,2,3,4,5};vector<int> v3{1,2,3,3};cout<<"v1=";for(int i:v1)cout<<i<<" ";cout<<endl;cout<<"v2=";for(int i:v2)cout<<i<<" ";cout<<endl;cout<<"v3=";for(int i:v3)cout<<i<<" ";cout<<endl;if(lexicographical_compare(v1.begin(),v1.end(),v2.begin(),v2.end()))cout<<"v1 is less than v2 "<<endl;elsecout<<"v2 is less than v1 "<<endl;if(lexicographical_compare(v1.begin(),v1.end(),v3.begin(),v3.end()))cout<<"v1 is less than v3 "<<endl;elsecout<<"v3 is less than v1 "<<endl;}
運行:



——————————————————————————————————————————————————————————————————

//寫的錯誤或者不好的地方請多多指導,可以在下面留言或者點擊左上方郵件地址給我發郵件,指出我的錯誤以及不足,以便我修改,更好的分享給大家,謝謝。

轉載請註明出處:http://blog.csdn.net/qq844352155

author:天下無雙

Email:coderguang@gmail.com

2014-9-17

於GDUT

——————————————————————————————————————————————————————————————————





C++STL中algorithm什函數可以統計字串中某個字元出現的個數,怎使用

char s[]="hello stl";
int n=count(s,s+10,'l');//計算字串s中字元'l'的個數。其中前兩個參數表示要尋找的區間,第三個參數表示要尋找的值。函數將返回區間中指定的值出現的次數,如果區間中不包含指定的值,將返回0.
 
為何STL中的algorithm採用全域函數實現,而不可以採用容器類的成員函數實現?

因為algorithm中的演算法是跨容器的、泛型的。
不僅不限於某一個容器,甚至任何實現介面的資料類型都可以使用。
int char這種基礎類型可以用,MyType自訂類型也可以用。容器也可以用
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.