Bootstrap

2、find 、find_if、find_first_of 的实例用法

一、find 的用法:

#include <iostream>
#include <vector>
#include <list>
#include <string>

#include <algorithm>

using namespace std;

// 打印
template <typename T>
void print_ivec(T first, T last, const string str)
{
    cout << str << ": ";
    for (; first != last; ++first)
    {   
        cout << *first << ' ';
    }   
    cout << endl;
}
// 查找
template <typename iterType, typename elemType>
iterType find(iterType first, iterType last, elemType &value)
{
    for (; first != last; ++first)
    {   
        if (*first == value)
        {   
            return first;
        }   
    }   
    return last;
}

int main(int argc, const char *argv[])
{
    //int i;
    vector<int>::size_type i;
    vector<int> ivec;
    for (i = 0; i < 10; ++i)
    {   
        ivec.push_back(i);
    }   
    print_ivec(ivec.begin(), ivec.end(), "init ivec");
        
    // 查找 vector int
    vector<int>::iterator it; 
    it = find(ivec.begin(), ivec.end(), 5); 
    if (it != ivec.end())
    {   
        cout << "find the element " << '5' << endl;
    }   
        
    list<string> slist;
    slist.push_back("nn");
    slist.push_back("xx");
    slist.push_back("oo");
    slist.push_back("nn");
    print_ivec(slist.begin(), slist.end(), "init slist");
        
    // 查找 list string
    list<string>::iterator st; 
    st = find(slist.begin(), slist.end(), "nn");
    if (st != slist.end())
    {
        cout << "find the element " << "nn" <<endl;
    }

    return 0;
}

二、find_first_of 的用法

#include <iostream>
#include <vector>
#include <list>
#include <string>

#include <algorithm>

using namespace std;

template <typename T>
void print_ivec(T first, T last, const string str)
{
    cout << str << ": ";
    for (; first != last; ++first)
    {   
        cout << *first << ' ';
    }   
    cout << endl;
}

int main(int argc, const char *argv[])
{
    vector<int> ivec;

    for (int i = 0; i < 10; ++i)
    {   
        ivec.push_back(i);
    }   
    print_ivec(ivec.begin(), ivec.end(), "init ivec");

    list<string> slist1;
    list<string> slist2;

    slist1.push_back("nn");
    slist1.push_back("xx");
    slist1.push_back("oo");
    slist1.push_back("nn");
    print_ivec(slist1.begin(), slist1.end(), "init slist1");
    
    slist2.push_back("oo");
    slist2.push_back("nn");
    print_ivec(slist2.begin(), slist2.end(), "init slist2");
    
    // find_first_of  统计slist2中所有元素在slist1中出现的次数
    int cnt = 0;
    list<string>::iterator it = slist1.begin();
    while ((it = find_first_of(it, slist1.end(), slist2.begin(), slist2.end())) != slist1.end() )
    {   
        cnt++;
        it++;
    }   
    cout << cnt << endl;

    return 0;
}

 

三、find_if 的用法

#include <iostream>
#include <vector>
#include <list>
#include <string>

#include <algorithm>

using namespace std;

template <typename T>
void print_ivec(T first, T last, const string str)
{
    cout << str << ": ";
    for (; first != last; ++first)
    {   
        cout << *first << ' ';
    }   
    cout << endl;
}

bool divbythree(int x)
{
    // x % 3 != 0 表示没有整除,返回false
    return x % 3 ? false: true;
}

bool findStr(string str)
{
    // compare 匹配返回0
    return !str.compare("nn");
}

int main(int argc, const char *argv[])
{
    //int i;
    vector<int>::size_type i;
    vector<int> ivec;
    for (i = 1; i <= 10; ++i)
    {   
        ivec.push_back(i);
    }   
    print_ivec(ivec.begin(), ivec.end(), "init ivec");
    
    // 查找 vector int
    vector<int>::iterator it; 
    it = find_if(ivec.begin(), ivec.end(), divbythree);
    if (it != ivec.end())
    {   
        cout << "the first element divby 3 is " << *it << " location: " << it - ivec.begin() << endl;
    }   
    
    list<string> slist;
    slist.push_back("nn");
    slist.push_back("xx");
    slist.push_back("oo");
    slist.push_back("nn");
    print_ivec(slist.begin(), slist.end(), "init slist");
    
    // 查找 list string
    list<string>::iterator st; 
    st = find_if(slist.begin(), slist.end(), findStr);
    if (st != slist.end())
    {   
        cout << "the first element find is " << *st << endl;
    }

    return 0;
}

;