0
我想創建一個函數/仿函數,其對一個字母的出現次數在字符串中的向量。如何cout「count_if」-STL函數的字符串?
例如: 輸出:
字符串:一二三四五
信:電子
頻率:1 0 2 0 1
我覺得我的算法將工作(我必須解決這個問題通過使用函數,count_if和for_each),但我不能把count_if或for_each /我函數LetterFrequency的解決方案放在cout-Output上。
我已經用difference_type字符串的嘗試,...
希望你能幫助我 - 謝謝你了很多!
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
#include "LetterFunctions.h"
using namespace std;
class CompareChar : public unary_function<char, bool>
{
public:
CompareChar(char const s): mSample(s){}
bool operator() (char const a)
{
return (tolower(a) == tolower(mSample));
}
private:
char mSample;
};
class LetterFrequency : public unary_function<string, size_t>
{
public:
LetterFrequency(char const s): mSample(s) {}
size_t operator() (string const& str)
{
return count_if(str.begin(),str.end(),CompareChar(mSample));
}
private:
char mSample;
};
class PrintFrequency : public unary_function<string, void>
{
public:
PrintFrequency(char const s): mSample(s) {}
void operator() (string const& str)
{
string::difference_type i = LetterFrequency(mSample);
cout << i << ", ";
}
private:
char mSample;
};
};