在这一章的内容中,集中介绍了泛型指针 Iterator等泛型编程风格,说白了就是让我们的程序有更好的通用性,在这章的内容中,比较不好理解的是函数对象 function object 

 

下面给出课后习题的一些做法,本人c++小白,欢迎指点,编译环境 VS2015

1. 写一个读取文本文件的程序,将文件中的每个单字存入map。map的key是单字,value是单字在文本文件中出现的次数。再定义一份由“排除字眼”组成的set,其中包含诸如a, an , the, or, and ,but之类的单字。将某个单字放入map之前,先确定该单字并不在set中。一旦文本文件读取完毕,请显示一份单子清单,并显示各单字出现的次数。你甚至可以再加以拓展,在显示单字之前,允许用户查询某个单字是否出现于文本文件中

// chapter01.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<map>
#include<set>
#include<fstream>
#include<algorithm>
#include<string>
using namespace std;


void show(string c) {
cout << c << " ";
}

int main() {
ifstream in_file("Text1.txt");
//ofstream out_file("Text1.txt");
map<string, int> words; //存放不在排除字眼中的单字以及出现的次数
set<string> ban; //存放排除字眼
set<string>all_words; //用来存放文本中的所有单字,供用户查询
string word;
string cin_word;
ban.insert("a");
ban.insert("an");
ban.insert("the");
ban.insert("and");
ban.insert("or");
ban.insert("but");
if (!in_file) {
cout << "文件未能打开" << endl;
}
else {
while (in_file >> word) {
all_words.insert(word);
//当在set中存在时,不存入map中
if (ban.count(word)) {
continue;
}
words[word]++;
}
cin >> cin_word;
while (cin_word.compare("0")!=0) {
if (all_words.count(cin_word)) {
cout << "文章中存在" << endl;
}
else
{
cout << "不存在" << endl;
}

cin >> cin_word;
}
map<string, int>::iterator ite = words.begin();
for (;ite != words.end();ite++) {
cout << ite->first << ":" << ite->second << endl;
}
}

return 0;
}



2.读取文本文件内容,和练习3.1一样,并将内容存储于vector中,以字符串长度为依据,对vector排序。定义一个函数对象并传给sort,这个函数对象接受两个字符串,当第一个字符串的长度小于第二字符串的长度时,返回True.最后打印排序后的vector。

// chapter01.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;


class mySort {
public:
bool operator()(string s1, string s2) {
return s1.length()<s2.length() ? true : false;
}
};

void myShow(string st) {
cout << st<<endl;
}

int main() {
ifstream in_file("Text1.txt");
vector<string> vec;
string word;
mySort my;
if (!in_file) {
cout << "文件打开失败" << endl;
}
else {
while (in_file>>word) {
vec.push_back(word);
}
sort(vec.begin(), vec.end(), my);
for_each(vec.begin(), vec.end(), myShow);
}

return 0;
}



3. 定义一个map,以家庭姓氏为key,value为家庭所有小孩的名字。令此map至少容纳六笔数据,允许用户根据姓氏来查询,并得以打印map内的每一笔数据

// chapter01.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<vector>
using namespace std;

void my_show(string name) {
cout << name << endl;
}

int main() {
map<string, vector<string>> family;
typedef pair<string, vector<string>> in_pair;
//由于这里说是存储家庭所有小孩的名字,所以这里我用了一个vector来存储
vector<string> vec;
string name;
map<string, vector<string>>::iterator ite;
vec.push_back("zi hao");
vec.push_back("messi");
vec.push_back("weixiao");
family.insert(in_pair("zhao", vec));
family.insert(in_pair("qian", vec));
family.insert(in_pair("sun", vec));
family.insert(in_pair("li", vec));
family.insert(in_pair("zhou", vec));
family.insert(in_pair("wu", vec));

while (cin>>name) {
if ((ite = family.find(name))!=family.end()) {
cout << "查找成功 \n";
for_each(ite->second.begin(),ite->second.end(),my_show);
}
else {
cout << "查找失败 \n";
}
}
return 0;
}



这里在结束输入的时候是用了CTRL+Z   

4. 利用 istream_iterator 从标准输入设备读取一连串整数,利用 ostream_iterator 将其中的奇数写入某个文件,每个数值之间以空格分隔。再利用ostream_iterator 将偶数写到另一个文件,每个数值单独放到一行中

// chapter01.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<algorithm>
#include<vector>
#include<iterator>
#include<fstream>
using namespace std;

int main() {

istream_iterator<int> is(cin);
istream_iterator<int> eof;
vector<int> vec;
ofstream outA("jishu.txt",ios_base::app);
ofstream outB("oushu.txt",ios_base::app);
//第二个元素用来表示各个元素被输出时的分隔符
ostream_iterator<int> osA(outA, " ");
ostream_iterator<int> osB(outB, "\n");
copy(is, eof, back_inserter(vec));

vector<int>::iterator ite = vec.begin();
for (;ite != vec.end();ite++) {
if (*ite % 2 == 0) {
copy(ite, ite+1, osB);
}
else {
copy(ite, ite+1, osA);
}
}
return 0;
}