STL_算法_查找算法(find_first_of、find_last_of)

C++ Primer 学习中。。。

 

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


find_first_of         //找第一个符合条件的位置


find_last_of         //找最后一个符合条件的位置


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
/*****************************************
//所有容器适用
find_first_of(b,e,sb,se);
find_first_of(b,e,sb,se,bp);
使用逆向迭代器 实现find_last_of算法
*****************************************/

/******************************************************
string查找函数和STL查找算法的比较
--------------------------------------------
string函数                    STL算法
find()                      find()
rfind()                     find()+逆向迭代器
find()                      search()
find()                      find_end()
find_first_of()             find_first_of()
find_last_of()              find_first_of()+逆向迭代器
******************************************************/

/*************************************************************************************
std::find_first_of()           所有容器适用                                algorithm
--------------------------------------------------------------------------------------
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 );

//eg:
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;
}
*************************************************************************************/

//不分大小写
bool comp_case_insensitive (char c1, char c2)
{
    return (tolower(c1)==tolower(c2));
}

void SplitFilename (const string& str);

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'};

    cout<<"母串:a b c A B C\n";
    cout<<"子串:A B C\n";

    // 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;
/**----------------------------find_first_of()+逆向迭代器-------------------------------**/
    vector<char>::reverse_iterator rit; // 逆向迭代器
    rit = find_first_of (myvector.rbegin(), myvector.rend(), match, match+3);

    if (rit!=myvector.rend())
        cout << "last match is: " << *rit << endl;

    cout<<endl;
/**----------------------------string-------------------------------**/
/*********************************************************************
std::string::find_last_of                                       string
-----------------------------------------------------------------------
size_t find_last_of ( const string& str, size_t pos = npos ) const;
size_t find_last_of ( const char* s, size_t pos, size_t n ) const;
size_t find_last_of ( const char* s, size_t pos = npos ) const;
size_t find_last_of ( char c, size_t pos = npos ) const;
***********************************************************************/
    string str1 ("/usr/bin/man");
    string str2 ("c:\\windows\\winhelp.exe");

    SplitFilename (str1);
    cout<<endl;
    SplitFilename (str2);

    return 0;
}
void SplitFilename (const string& str)
{
  size_t found;
  cout << "Splitting: " << str << endl;
  found=str.find_last_of("\\/");
  cout << " folder: " << str.substr(0,found) << endl;
  cout << " file: " << str.substr(found+1) << endl;
}
/*****
Output
    母串:a b c A B C
    子串:A B C
    first match is: A
    first match is: a
    last match is: C

    Splitting: /usr/bin/man
     folder: /usr/bin
     file: man

    Splitting: c:\windows\winhelp.exe
     folder: c:\windows
     file: winhelp.exe
*/

    原文作者:查找算法
    原文地址: https://blog.csdn.net/u010579068/article/details/50503474
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞