STL--函數對象學習

函數對象:一個重載了運算符()的個對象,可以像一個函數一樣使用。

①普通的函數

bool comp(int val)
{
	return val > 3;
}
②函數對象,聲明爲模板類,並可以接受參數,具有更大擴展性

template<typename T>
class Comp
{
public:
	Comp(T val) {m_val = val;}

	bool operator() (T val) {return val > m_val;}

private:
	T m_val;
};
調用上面兩種情況,實際是一樣,但是函數對象可以設置參數,能夠靈活控制比較值大小

	vector<int> vec;
	for (int i = 1; i< 20; ++i)
		vec.push_back(i);

	int count = count_if(vec.begin(), vec.end(), comp);
	cout<<count<<endl;

	count = count_if(vec.begin(), vec.end(), Comp<int>(5));
	cout<<count<<endl;

	vector<char> c_vec;
	for (char c = 'a'; c < 'z'; ++c)
		c_vec.push_back(c);

	cout<<count_if(c_vec.begin(), c_vec.end(), Comp<char> ('c'))<<endl;
③自己寫的統計函數

template<typename T, typename FUNC>

int count_n(T *arr, int  n, FUNC func)
{
	int count = 0;

	for (int i = 0; i < n; ++i)
		if (func(arr[i]))
			count++;

	return count;
}

調用統計函數

int arr[] = {2, 4, 5, 7, 9, 333};
	count = count_n<int, Comp<int> >(arr, sizeof(arr) / sizeof(int), Comp<int>(1));
	cout<<count<<endl;
④在網上找一個remove_if的例子,覺得很不錯

[cpp] view plaincopy
#ifndef STRING_MATCH_HPP_  
#define STRING_MATCH_HPP_  
  
#include <string>  
#include <functional>  
  
using std::string;  
  
typedef unsigned int UINT;  
  
enum findmodes  
{  
    FM_INVALID = 0,  
    FM_IS,  
    FM_STARTSWITH,  
    FM_ENDSWITH,  
    FM_CONTAINS  
};  
  
typedef struct tagFindStr  
{  
    UINT iMode;  
    string szMatchStr;  
} FindStr;  
  
typedef FindStr* LPFINDSTR;  
  
class FindMatchingString : public std::unary_function<string, bool>  
{  
public:  
    FindMatchingString(const LPFINDSTR lpFS) : m_lpFS(lpFS) {}  
  
    bool operator()(string& strCmp) const  
    {  
        bool retVal = false;  
        string strMatch = m_lpFS->szMatchStr;  
        switch(m_lpFS->iMode)  
        {  
        case FM_IS:  
            retVal = (strCmp == strMatch);  
            break;  
        case FM_STARTSWITH:  
            retVal = (strCmp.find(strMatch)==0);  
            break;  
        case FM_ENDSWITH:  
            retVal = (strCmp.find(strMatch)==(strCmp.length()-strMatch.length()));  
            break;  
        case FM_CONTAINS:  
            retVal = (strCmp.find(strMatch) != string::npos);  
            break;  
        }  
        return retVal;  
    }  
private:  
    LPFINDSTR m_lpFS;  
};  
  
  
#endif /* STRING_MATCH_HPP_ */  

接着就是使用remove_if方法了。文件main.cpp

[cpp] view plaincopy
#include <string>  
#include <vector>  
#include <iostream>  
#include <algorithm>  
  
#include "header/string_match.hpp"  
  
using std::vector;  
using std::string;  
using std::cout;  
using std::endl;  
//using std::remove_if;  
  
int main(int argc, char* argv[])  
{  
    vector<string> vs;  
    vs.push_back("Alice");  
    vs.push_back("Bob");  
    vs.push_back("Cart");  
    vs.push_back("Duncan");  
    vs.push_back("Kelvin");  
  
    cout << "Before remove:\n[";  
    for (vector<string>::iterator iter=vs.begin();iter!=vs.end();iter++)  
    {  
        cout << *iter << ", ";  
    }  
    cout << "]" << endl;  
  
    FindStr fs;  
    fs.iMode = FM_CONTAINS;  
    fs.szMatchStr = "Alice";  
    vs.erase(std::remove_if(vs.begin(), vs.end(), FindMatchingString(&fs)), vs.end());  
  
    cout << "After remove:\n[" ;  
    for (vector<string>::iterator iter=vs.begin();iter!=vs.end();iter++)  
    {  
        cout << *iter << ", ";  
    }  
    cout << "]" << endl;  
    return 0;  
}  




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章