使用泛型算法的例子, 使用了函数对象
2009-09-16 01:16
639 查看
Test.txt中包含以下单词:
wo shi and if chen or xuefeng, how are you?
wo or shi but chen but Xue but if and
test.cpp
// Patterns.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
#include "vector"
#include "algorithm"
#include "iterator"
#include "string"
#include "fstream"
using namespace std;
// 小于的函数对象
class LessThan
{
public:
bool operator()(const string& s1, const string& s2)
{
return s1.size() < s2.size();
}
};
// 负责输出的函数对象
class PrintItem
{
public:
PrintItem(int lineLen = 8): m_cnt(0){}
void operator()(const string& elem)
{
m_cnt++;
cout << "Elem[" << m_cnt << "]:";
cout << elem << endl;
}
private:
int m_cnt;
};
void Process(vector<string> &vec)
{
cout << "/nInvoke some test algorithm.../n";
// 调用sort排序
cout << "sort()/n";
sort(vec.begin(), vec.end());
cout << "after sort:/n";
for_each(vec.begin(), vec.end(), PrintItem());
// 删除重复的
cout << "/nunique and erase to remove the same elem/n";
vector<string>::iterator it;
// 返回的是需要删除的位置
it = unique(vec.begin(), vec.end());
vec.erase(it, vec.end());// 删除重复的
cout << "/nAfter making sure the unique elem in vector:/n";
for_each(vec.begin(), vec.end(), PrintItem());
// 按照长度再排一次,如果长度相同,保留现有顺序, 使用函数对象
cout << "/n stable_sort according to the length of word/n";
stable_sort(vec.begin(), vec.end(), LessThan());
for_each(vec.begin(), vec.end(), PrintItem());
// 定义需要排除的单词
static string rw[] = {"and", "if", "or", "but"};
vector<string> remove_words(rw, rw+4);
// 删除那些需要排除的词
vector<string>::iterator it2 = remove_words.begin();
for (; it2!=remove_words.end(); ++it2)
{
// 当前词在vector中的个数
int cnt = 0;
cnt = count(vec.begin(), vec.end(), *it2);
cout << cnt << " instance removed:" << (*it2) << endl;
// remove 以后,分成两部分,一部分是要保留的,另一半是要删除的,返回的就是要删除的开始位置
vec.erase(remove(vec.begin(), vec.end(), *it2), vec.end());
cout << "Current show:/n";
for_each(vec.begin(), vec.end(), PrintItem());
}
}
void main()
{
vector<string> sample;
string file1;
cout << "Input file name:/n";
cin >> file1;
// 打开文件,作为输入流
ifstream infile1(file1.c_str());
// iterator的特殊形式
istream_iterator<string> input_set1(infile1), eos;
// 把文件中的单词添加到sample vector的末尾
copy(input_set1, eos, back_inserter(sample));
// 使用函数对象输出,也可以使用函数指针,但是函数指针就不能内联了,函数对象可以
for_each(sample.begin(), sample.end(), PrintItem());
cout << "/nWord number in txt: " << sample.size() << endl;
Process(sample);
}
wo shi and if chen or xuefeng, how are you?
wo or shi but chen but Xue but if and
test.cpp
// Patterns.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
#include "vector"
#include "algorithm"
#include "iterator"
#include "string"
#include "fstream"
using namespace std;
// 小于的函数对象
class LessThan
{
public:
bool operator()(const string& s1, const string& s2)
{
return s1.size() < s2.size();
}
};
// 负责输出的函数对象
class PrintItem
{
public:
PrintItem(int lineLen = 8): m_cnt(0){}
void operator()(const string& elem)
{
m_cnt++;
cout << "Elem[" << m_cnt << "]:";
cout << elem << endl;
}
private:
int m_cnt;
};
void Process(vector<string> &vec)
{
cout << "/nInvoke some test algorithm.../n";
// 调用sort排序
cout << "sort()/n";
sort(vec.begin(), vec.end());
cout << "after sort:/n";
for_each(vec.begin(), vec.end(), PrintItem());
// 删除重复的
cout << "/nunique and erase to remove the same elem/n";
vector<string>::iterator it;
// 返回的是需要删除的位置
it = unique(vec.begin(), vec.end());
vec.erase(it, vec.end());// 删除重复的
cout << "/nAfter making sure the unique elem in vector:/n";
for_each(vec.begin(), vec.end(), PrintItem());
// 按照长度再排一次,如果长度相同,保留现有顺序, 使用函数对象
cout << "/n stable_sort according to the length of word/n";
stable_sort(vec.begin(), vec.end(), LessThan());
for_each(vec.begin(), vec.end(), PrintItem());
// 定义需要排除的单词
static string rw[] = {"and", "if", "or", "but"};
vector<string> remove_words(rw, rw+4);
// 删除那些需要排除的词
vector<string>::iterator it2 = remove_words.begin();
for (; it2!=remove_words.end(); ++it2)
{
// 当前词在vector中的个数
int cnt = 0;
cnt = count(vec.begin(), vec.end(), *it2);
cout << cnt << " instance removed:" << (*it2) << endl;
// remove 以后,分成两部分,一部分是要保留的,另一半是要删除的,返回的就是要删除的开始位置
vec.erase(remove(vec.begin(), vec.end(), *it2), vec.end());
cout << "Current show:/n";
for_each(vec.begin(), vec.end(), PrintItem());
}
}
void main()
{
vector<string> sample;
string file1;
cout << "Input file name:/n";
cin >> file1;
// 打开文件,作为输入流
ifstream infile1(file1.c_str());
// iterator的特殊形式
istream_iterator<string> input_set1(infile1), eos;
// 把文件中的单词添加到sample vector的末尾
copy(input_set1, eos, back_inserter(sample));
// 使用函数对象输出,也可以使用函数指针,但是函数指针就不能内联了,函数对象可以
for_each(sample.begin(), sample.end(), PrintItem());
cout << "/nWord number in txt: " << sample.size() << endl;
Process(sample);
}
相关文章推荐
- 使用STL中的泛型算法及函数对象
- 使用函数对象代替函数作算法的 参数
- @classmethod, @staticmethod和@property这三个装饰器的使用对象是在类中定义的函数。下面的例子展示了它们的用法和行为:
- c#打包文件解压缩 C#中使用委托、接口、匿名方法、泛型委托实现加减乘除算法 一个简单例子理解C#的协变和逆变 对于过长字符串的大小比对
- Effective STL: Item 44:优先使用与泛型算法同名的成员函数
- STL_算法_中使用的函数对象
- 关于在标准库算法中使用函数对象的一点认识
- 考虑使用函数对象代替函数作算法的参数(Effective STL条款46)
- 函数对象的构造与使用例子
- Effective STL: Item 44:优先使用与泛型算法同名的成员函数
- 函数对象 与 count_if()结合使用 例子
- 11.4 泛型算法与函数对象
- 函数对象的意义:泛型算法应用以及避免重载带来的全局影响
- JSON对象和JSON字符串以及JSON.parse 函数的使用
- javascript中对象写在函数外面,函数内部不能使用
- 使用NSStringFromSelector(_cmd), self) 函数 来跟踪对象的生命周期
- python里使用Future对象进行回调别的函数
- effective stl 第46条:考虑使用函数对象而不是函数作为STL算法的参数
- 使用wrap和unwrap加密解密Oracle的PL/SQL对象(包,存储过程,函数等)代码
- 63、实例 使用jQuery全局函数遍历数组和集合对象