- 浏览: 399521 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
When reading the article by Lippman Stanley, I come across the example on the chpater 12 where it has discussed the topic of generic algorithm;
algorithm is a chapter that introduce the common used algorithms, such as erase, remove, count, count_if, iterator, copy, back_inserter (a kind of iterator adaptor), unique, sort, stable_sort, for_each, and some of the common stream objects/iterators, such as ifstream and istream_iterator, functio object and etc...
to use all of those common algorithm , we'd better put them into some context, the context that I am about to put them into is a live use case scenario.
here is the deal, we want to count several children's illustrated books, and we wantt to get a feel for the level of vocabulary appropriated to such books. the ideas is as follow.
read the text of some number of children's illustrated books, storing the text in invididual string vectors, and then
- make a copy of each vector
- merge the five vectors in to a large vector
- sort the large vector in alphabetic order
- remove all duplicated words
- count the number of words longer than six characters (length is a presumable measure of complexity, at least in terms of vocabulary)
- Remove any semantically neural words (such as and, if or, but and so on)
- Print the vector
Here is the code with the steps above in mind, let's start to looks into the code to get some feel of the normal/common algorithm that we might use in our daily life.
/** * file * process_vocabs.cpp * this is a source file for a text processing application. * * the general steps is as follow. * */ #include "stdafx.h" #include <vector> #include <iostream> #include <iterator> #include <algorithm> #include <string> #include <list> #include <allocators> #include <fstream> using std::cout; using std::cerr; using std::endl; using std::cin; using std::sort; using std::stable_sort; // stable_sort will preserve the order of elements using std::remove; using std::copy; using std::unique; using std::count_if; using std::string; using std::vector; using std::list; using std::back_inserter; using std::fstream; using std::ifstream; using std::istream_iterator; // the iterator to visit the istream object //? // what is the allocator used for? using std::allocator; /** * GreaterThan: function object class to filter word that is less than 6 length long */ class GreaterThan { public : GreaterThan(int sz = 6) : _size(sz) { } bool operator ()( const string &s1) { return s1.size() > _size; } private: int _size; }; /** * PrintElem: * function object class to print element according to print format */ class PrintElem { public: PrintElem(int lineLen = 8) : _line_length(lineLen), _cnt(0) {} void operator() ( const string &elem) { ++_cnt; if (_cnt % _line_length == 0) { cout << "\n"; } cout << elem << " " ; } private: int _line_length; int _cnt; }; class LessThan { public: bool operator() (const string &s1, const string &s2) { return s1.size() < s2.size(); } }; // the original text has the following declaration // typedef vector<string, allocator> textword; // typedef vector<string> textwords; // the original program has the following signature // void process_vocab(vector<textwords, allocator>*pvec) // void process_vocab(vector<textwords> *pvec) { if (!pvec) { return; } vector <string> texts; vector<textwords>::iterator iter; for (iter = pvec->begin(); iter != pvec->end(); ++iter) { copy(iter->begin(), (*iter).end(), back_inserter(texts)); } // sort the element of the texts sort(texts.begin(), texts.end()); // let's see what we have for_each(texts.begin(), texts.end(), PrintElem()); cout << "\n\n"; // delete all duplicate elements vector<string>::iterator it; it = unique(texts.begin(), texts.end()); texts.erase(it, texts.end()); // ok, let's see what we have now for_each( texts.begin(), texts.end(), PrintElem() ); cout << "\n\n"; // ok, sort elements based on default length of 6 // stable_sort will preserve the order of equal elements stable_sort(texts.begin(), texts.end(), LessThan() ); for_each(texts.begin(), texts.end(), PrintElem() ); cout << "\n\n"; // count number of string greater than length 6 int cnt = 0; // obsolte form of count -- standard change this cnt = count_if(texts.begin(), texts.end(), GreaterThan()); cout << "Number of words greater than length six are " << cnt << endl; static string rw[] = {"and", "if", "or", "but", "the" }; vector<string, allocator<string> > remove_words(rw, rw + 5); vector<string, allocator<string> >::iterator it2 = remove_words.begin(); for (; it2 != remove_words.end(); ++it2) { int cnt = 0; // obsolte form of count -- standard change this cnt = count(texts.begin(), texts.end(), *it2); cout << cnt << " instances removed: " << (*it2) << endl; texts.erase( remove(texts.begin(), texts.end(), *it2), // remove does not change the size of the container, but it // moves the elements to rmeoved to the end of the vector texts.end() ); cout << "\n\n"; for_each( texts.begin(), texts.end(), PrintElem()); } } // difference type is the type capable of holding the result // of subtracting two iterator of a container // -- in this case, of a string vector ... // ordinarily, this is handled by default typedef vector<string, allocator<string> >::difference_type diff_type; // prestandard header syntax for <fstream> int prcess_vocab_main() { vector<textwords> sample; vector<string, allocator<string> > t1, t2; string t1fn, t2fn; // request input files from user ... // should so some error checking in real-world program cout << "text file #1 : " ; cin >> t1fn; cout << "text file #2 : " ; cin >> t2fn; // open the files ifstream infile1 (t1fn.c_str() ); ifstream infile2 (t2fn.c_str() ); // special form of iterator // ordinarily, diff_type is provided by default... // the obsolte form of the istream_iterator is as // istream _iterator<string, diff_type> .... istream_iterator<string> input_set1(infile1), eos; // eos is the conierge that keep the door of something istream_iterator<string> input_set2(infile2); // special form of iterator copy(input_set1, eos, back_inserter(t1) ); copy(input_set2, eos, back_inserter(t2) ); // sample.push_back(t1); sample.push_back(t2); process_vocab(&sample); return 0; }
and to drive this, you can create several data.txt files, and input the path of data.txt files which will starts to load the files in.
so in wrap up/sum up/conclusion/summarize, what you can see from the use of common algorithms are the following categories
- iterator pattern to go through some collections
- iterator adaptor pattern where you can tell where/how to insert to a collection (back_inserter, inserter)
- function object which can be used in lieu of the function pointer, with advantages of (1). store information; (2). optimize such as inline function.
- common sort algorithms - sort, stable_sort
- aggregation algorithms - count, count_if
- iterative algorithm - for_each
- filter algorithm - remove, erase, unique....
发表评论
-
不安装Visual Studio,只用Windows SDK搭建VC环境
2013-12-31 21:52 15342首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 1012RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 771It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 960As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1060typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 771There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 823we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 977As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 754There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 960Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 975As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 994As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1328in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 939class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 986template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1212There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 854the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 588The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1170In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 873A class member operator new() c ...
相关推荐
本篇文章将深入探讨如何使用C#语言实现一个通用的遗传算法,主要依据给定的文件信息,包括Population.cs、ListGenome.cs、AssemblyInfo.cs、Genome.cs、Class1.cs、GeneticAlgorithm.csproj、App.ico、Genetic...
C++_algorithm
c++中的<algorithm>实现 c++中的<algorithm>实现 c++中的<algorithm>实现 c++中的<algorithm>实现 c++中的<algorithm>实现 c++中的<algorithm>实现 c++中的<algorithm>实现 c++中的<algorithm>实现 c++中的...
ssGA is a simple and easy to use implementation of a Steady State Generic Algorithm. This algorithm generates a new individual in every step. The new individual is inserted in the population with his ...
同时,合理使用C++的STL(标准模板库)如`<algorithm>`中的`sort()`函数,可以简化排序操作,提高代码效率。 总的来说,掌握排序算法是成为一名优秀程序员的基础。通过学习和实践C++中的冒泡排序、快速排序和直接...
Gossip of Algorithm Examples
本节教学课件“13_Design Generic Algorithm”主要探讨如何设计泛型算法(Generic Algorithm)。泛型算法是不依赖于特定数据类型或容器的算法,它们可以在多种类型或容器上工作,提供了极大的灵活性和代码复用性。...
在"C++_development_examples"这个资源中,我们看到的是一个基础篇的C++开发实例集合,旨在帮助初学者和有经验的开发者进一步理解和掌握C++编程的核心概念。 1. **C++简介**: C++是C语言的扩展,由Bjarne ...
在本实例中,"round_C++_algorithm_" 的标题暗示我们将会探讨一种用C++实现的高级线段树算法。 线段树的基本思想是分治,即将问题分解为更小的部分,然后分别解决这些部分,并将结果合并以得到原问题的答案。在C++...
"Potyczki_c_C++_algorithm_"这一标题暗示了这是一个关于C++算法学习的资源集合,可能是教程、代码示例或者练习题集,旨在帮助开发者提升在C++中应用算法的能力。 C++中的算法是程序设计的基础,它涉及到数据结构的...
algorithm design foundations analysis and internet examples 中文版
遗传算法(Genetic Algorithm)是模拟达尔文生物进化论的自然选择和遗传学机理的生物进化过程的计算模型,是一种通过模拟自然进化过程搜索最优解的方法,本文介绍了一种基于Matlab的遗传算法实例。
The fourth edition of Data Structures and Algorithm Analysis in C++ describes data structures, methods of organizing large amounts of data, and algorithm analysis, the estimation of the running time ...
Dev-Cpp是一款经典的C++集成开发环境,专为初学者和专业开发者提供了一个轻量级且功能齐全的平台,用于学习和实践C++编程语言。这个软件包的标题"dev-cpp-c++"明确指出了它的核心功能,即支持C++编程。下面将详细...
good fill polygon algorithm c++
- **Data Structures and Algorithm Analysis in C++**: 这本书主要聚焦于数据结构和算法分析的领域,并且是用C++编程语言实现这些数据结构和算法。 #### 描述解析 - **Data Structures and Algorithm Analysis in ...
数学计算经典书籍,主要矩阵论理论,曲线拟合,差值等算法