C++中泛型编程风格——iterator学习,挺有难度,概念很抽象,这里主要把一些知识点和习题给过一遍!
一、前言
C++中泛型编程风格——iterator学习(Essential C++ 第三章)。
二、例题
-
P218 练习 3.1
写一个读取文本文件的程序,将文件中的每个单字存人 map。map 的 key便是刚才所说的单字,map 的 value 则是该单字在文本文件中的出现次数。再定义一份由“排除字眼”组成的 set,其中包含诸如 a、an、or、the、and 和 but 之类的单字。将某单字放入 map 之前,先确定该单字并不在“排除字集”中。一旦文本文件读取完毕,请显示一份单字清单,并显示各单字的出现次数。你甚至可以再加以扩展,在显示单字之前,允许用户查询某个单字是否出现于文本文件中。 -
简单分析一下,四个函数:(1)筛选功能:再定义一份由“排除字眼”组成的 set,其中包含诸如 a、an、or、the、and 和 but 之类的单字;(2)计数功能:map 的 key便是刚才所说的单字,map 的 value 则是该单字在文本文件中的出现次数;(3)查询功能:在显示单字之前,允许用户查询某个单字是否出现于文本文件中;(4)显示功能:显示一份单字清单,并显示各单字的出现次数。
#include<map>
#include<set>
#include<string>
#include<iostream>
#include<fstream>
using namespace std;
void init_exclusion_set(set<string>&);
void process_file(map<string, int>&, const set<string>&, ifstream&);
void user_query(const map<string, int>&);
void display_word(const map<string, int>&, ofstream&);
int main()
{
ifstream ifile("C:\\Users\\13660\\Desktop\\column.txt");
ofstream ofile("C:\\Users\\13660\\Desktop\\column.map");
if (!ifile || !ofile)
{
cerr << "Unable to open file\n";
return -1;
}
set<string> exclude_set;
init_exclusion_set(exclude_set);
map<string, int> word_count;
process_file(word_count,exclude_set,ifile);
user_query(word_count);
display_word(word_count, ofile);
}
void init_exclusion_set(set<string> &exs)
{
static string excluded_words[6] = {
"a","ab","or","the","and","but"
};
exs.insert(excluded_words, excluded_words+6);
}
void process_file(map<string, int> &word_count, const set<string> &exclude_set, ifstream &ifile)
{
string word;
while (ifile >> word)
{
if (exclude_set.count(word))
{
continue;
}
word_count[word]++;
}
}
void user_query(const map<string, int> &word_map)
{
string searche_word;
cout << "Please input a word to search: Q or q to quit";
cin >> searche_word;
while (searche_word.size() && searche_word != "q" && searche_word != "Q")
{
map<string, int>::const_iterator it;
if ((it = word_map.find(searche_word)) != word_map.end())
{
cout << it->first << it->second << "\n";
}
else
{
cout << "Can't find.";
}
cout << "\n Another search? (q or Q to quit)";
cin >> searche_word;
}
}
void display_word(const map<string, int> &word_map, ofstream &os)
{
map<string, int>::const_iterator iter = word_map.begin(), end_it = word_map.end();
while (iter != end_it)
{
os << iter->first << "(" << iter->second << ")" << endl;
++iter;
}
os << endl;
}
- 还是需要自己敲一敲代码才行。使用不熟悉。
-P220 练习 3.2
读取文本文件内容–和练习 3.1一样–并将内容储存于 vector。以字符串长度为依据,对 vector排序。定义一个 function object 并传给 sort();这- function object接受两个字符串,当第一字符串的长度小于第二字符串的长度时,就返回true。最后,打印排序后的 vector 内容。
class LessThan {
public:
bool operator() (const string &s1, const string &s2)
{
return s1.size() < s2.size();
}
};
// sort(vec.begin(),vec.end(),LessThan());
template <typename elemType>
void display_vector(const vector<elemType>& vec, ostream& os = cout, int len = 8)
{
vector<elemType>::const_iterator iter = vec.begin(), end_iter = vec.end();
int elem_cnt = 1;
while (iter != end_iter)
{
os << *iter++ << (!(elem_cnt++ % len) ? '\n' : ' ');
}
os << endl;
}
代码是在 visual studio 中编写的,该软件还是比较好用的,我安装的是2022专业版;
共勉!