部分解答来自网上已有解答
EX.7-0
1)
#include <iostream>
#include <string>
#include <map>
#include <conio.h>
using namespace std;
int main()
{
string s;
map<string, int> counters;
while (cin >> s) {
++counters[s];
}
for (map<string, int>::const_iteratoriter = counters.begin(); iter != counters.end(); ++iter) {
cout <<iter->first << "\t"<< iter->second << endl;
}
getch();
return 0;
}
2)
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <conio.h>
using namespace std;
//vector<string> split(const string& s)
//{
// vector<string>ret;
// typedefstring::size_type string_size;
// string_size i =0;
//
// while (i !=s.size()) {
// while (i !=s.size() && isspace(s[i]))
// ++i;
//
// string_size j= i;
//
// while (j !=s.size() && !isspace(s[j]))
// ++j;
//
// if (j != i) {
// ret.push_back(s.substr(i,j-i));
// i = j;
// }
// }
// return ret;
//}
bool space(char c)
{
return isspace(c);
}
bool not_space(char c)
{
return !isspace(c);
}
vector<string> split(conststring& str)
{
typedef string::const_iterator iter;
vector<string>ret;
iter i = str.begin();
while (i != str.end()) {
i = find_if(i,str.end(), not_space);
iter j =find_if(i, str.end(), space);
if (i != str.end())
ret.push_back(string(i,j));
i = j;
}
return ret;
}
map<string, vector<int>> xref(istream& in, vector<string> find_words(const string&) = split)
{
string line;
int line_number = 0;
map<string,vector<int> > ret;
while (getline(in, line)) {
++line_number;
vector<string>words = find_words(line);
for (vector<string>::const_iterator it =words.begin(); it != words.end(); ++it) {
ret[*it].push_back(line_number);
}
}
return ret;
}
int main()
{
map<string,vector<int> > ret = xref(cin);
for (map<string, vector<int> >::const_iterator it = ret.begin(); it != ret.end();++it) {
cout <<it->first << " occurs on line(s):";
vector<int>::const_iterator line_it =it->second.begin();
cout <<*line_it;
++line_it;
while (line_it != it->second.end()) {
cout << ", " << *line_it;
++line_it;
}
cout <<endl;
}
getch();
return 0;
}
EX.7-1
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <conio.h>
using namespace std;
int main() {
string s;
map<string, int> counters;
map<int, vector<string> > words_by_freq;
while (cin >> s)
++counters[s];
for (map<string, int>::const_iteratorit = counters.begin();
it !=counters.end(); ++it)
words_by_freq[it->second].push_back(it->first);
for (map<int,vector<string> >::const_iterator i = words_by_freq.begin();
i !=words_by_freq.end(); ++i) {
cout << "\nFrequency: " << i->first<< endl;
for (vector<string>::const_iterator j =i->second.begin();
j != i->second.end(); ++j)
cout << *j << endl;
}
getch();
return 0;
}
EX.7-2
string get_letter_grade(doublegrade) {
if (grade >= 90.0)
return "A";
if (grade >= 80.0 && grade < 90.0)
return "B";
if (grade >= 70.0 && grade < 80.0)
return "C";
if (grade >= 60.0 && grade < 70.0)
return "D";
return "F";
}
EX.7-3
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <conio.h>
using namespace std;
bool space(char c)
{
return isspace(c);
}
bool not_space(char c)
{
return !isspace(c);
}
vector<string> split(conststring& str)
{
typedef string::const_iterator iter;
vector<string>ret;
iter i = str.begin();
while (i != str.end()) {
i = find_if(i,str.end(), not_space);
iter j =find_if(i, str.end(), space);
if (i != str.end())
ret.push_back(string(i,j));
i = j;
}
return ret;
}
map<string, vector<int>> xref(istream& in, vector<string> find_words(const string&) = split)
{
string line;
int line_number = 0;
map<string,vector<int> > ret;
while (getline(in, line)) {
++line_number;
vector<string>words = find_words(line);
for (vector<string>::const_iterator it =words.begin();it != words.end(); ++it)
if (find(ret[*it].begin(), ret[*it].end(),line_number) == ret[*it].end())
ret[*it].push_back(line_number);
}
return ret;
}
int main()
{
map<string,vector<int> > ret = xref(cin);
for (map<string, vector<int> >::const_iterator it = ret.begin(); it != ret.end();++it) {
cout <<it->first << " occurs on line(s):";
vector<int>::const_iterator line_it =it->second.begin();
cout <<*line_it;
++line_it;
while (line_it != it->second.end()) {
cout << ", " << *line_it;
++line_it;
}
cout <<endl;
}
getch();
return 0;
}
EX.7-4
#include <algorithm>
#include <map>
#include <iostream>
#include <string>
#include <sstream>//使用stringstream需要引入这个头文件
#include <vector>
#include <conio.h>
using namespace std;
#define LINE_LENGTH 80
bool space(char c)
{
return isspace(c);
}
bool not_space(char c)
{
return !isspace(c);
}
vector<string> split(conststring& str)
{
typedef string::const_iterator iter;
vector<string>ret;
iter i = str.begin();
while (i != str.end()) {
i = find_if(i,str.end(), not_space);
iter j =find_if(i, str.end(), space);
if (i != str.end())
ret.push_back(string(i,j));
i = j;
}
return ret;
}
map<string, vector<int>> xref(istream& in, vector<string> find_words(const string&) = split) {
string line;
int line_number = 0;
map<string,vector<int> > ret;
while (getline(in, line)) {
++line_number;
vector<string>words = find_words(line);
for (vector<string>::const_iterator it =words.begin(); it != words.end(); ++it)
ret[*it].push_back(line_number);
}
return ret;
}
int main() {
map<string,vector<int> > ret = xref(cin);
for (map<string, vector<int> >::const_iterator it = ret.begin(); it != ret.end();++it) {
stringstream ss;
//将"string"occurs on lines(s)装入到ss中
ss <<it->first << " occurs on line(s):";
vector<int>::const_iterator line_it =it->second.begin();
ss <<*line_it;//将第一次出现的行号装入到ss中
++line_it;
while (line_it != it->second.end()) {
ss << ", " << *line_it;
++line_it;
}
string output =ss.str();
//将ss中的内容通过一个一个的字符输出
for (string::size_type i = 0; i != output.size();++i) {
cout <<output[i];
if ((i + 1) % LINE_LENGTH == 0)
cout <<endl;
}
cout <<endl;
}
getch();
return 0;
}
EX.7-7
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <conio.h>
using namespace std;
bool space(char c)
{
return isspace(c);
}
bool not_space(char c)
{
return !isspace(c);
}
vector<string> split(conststring& str)
{
typedef string::const_iterator iter;
vector<string>ret;
iter i = str.begin();
while (i != str.end()) {
i = find_if(i,str.end(), not_space);
iter j =find_if(i, str.end(), space);
if (i != str.end())
ret.push_back(string(i,j));
i = j;
}
return ret;
}
map<string, vector<int>> xref(istream& in, vector<string> find_words(const string&) = split)
{
string line;
int line_number = 0;
map<string,vector<int> > ret;
while (getline(in, line)) {
++line_number;
vector<string>words = find_words(line);
for (vector<string>::const_iterator it =words.begin();it != words.end(); ++it)
if (find(ret[*it].begin(), ret[*it].end(),line_number) == ret[*it].end())
ret[*it].push_back(line_number);
}
return ret;
}
int main()
{
map<string,vector<int> > ret = xref(cin);
for (map<string, vector<int> >::const_iterator it = ret.begin(); it != ret.end();++it) {
//cout << it->first << " occurs online(s): ";
cout <<it->first << " occurs on ";
cout <<((it->second.size() == 1) ? "line: ": "lines: ");
vector<int>::const_iterator line_it =it->second.begin();
cout <<*line_it;
++line_it;
while (line_it != it->second.end()) {
cout << ", " << *line_it;
++line_it;
}
cout <<endl;
}
getch();
return 0;
}
EX.7-8
split.h
#ifndef GUARD_split_h
#define GUARD_split_h
#include <vector>
#include <string>
std::vector<std::string> split(conststd::string&);
#endif
split.cpp
#include <cctype>
#include <string>
#include <vector>
#include "split.h"
using std::vector;
using std::string;
#ifndef _MSC_VER
using std::isspace;
#endif
vector<string> split(conststring& s) {
vector<string>ret;
typedef string::size_type string_size;
string_size i = 0;
// invariant: we have processed characters `['originalvalue of `i', `i)'
while (i != s.size()) {
// ignore leading blanks
// invariant: characters in range `['original `i', current`i)' are all spaces
while (i != s.size() && isspace(s[i]))
++i;
// find end of next word
string_size j = i;
// invariant: none of the characters in range `['original`j', current `j)' is a space
while (j != s.size() && !isspace(s[j]))
++j;
// if we found some nonwhitespace characters
if (i != j) {
// copy from `s' starting at `i' and taking `j' `\-' `i'chars
ret.push_back(s.substr(i, j - i));
i = j;
}
}
return ret;
}
urls.h
#ifndef GUARD_urls_h
#define GUARD_urls_h
#include <vector>
#include <string>
std::vector<std::string> find_urls(const std::string& s);
#endif
urls.cpp
#include <algorithm>
#include <cctype>
#include <string>
#include <vector>
#include "urls.h"
using std::find;
using std::find_if;
#ifndef _MSC_VER
using std::isalnum;
using std::isalpha;
using std::isdigit;
#endif
using std::search;
using std::string;
using std::vector;
bool not_url_char(char);
string::const_iterator url_end(string::const_iterator,string::const_iterator);
string::const_iterator url_beg(string::const_iterator,string::const_iterator);
vector<string> find_urls(conststring& s) {
vector<string>ret;
typedef string::const_iterator iter;
iter b = s.begin(), e= s.end();
// look through the entire input
while (b != e) {
// look for one or more letters followed by `://'
b = url_beg(b, e);
// if we found it
if (b != e) {
// get the rest of the \s-1URL\s0
iter after =url_end(b, e);
// remember the \s-1URL\s0
ret.push_back(string(b, after));
// advance `b' and check for more \s-1URL\s0s on this line
b = after;
}
}
return ret;
}
string::const_iterator url_end(string::const_iterator b,
string::const_iterator e) {
return find_if(b, e, not_url_char);
}
bool not_url_char(char c) {
// characters, in addition to alphanumerics, that canappear in a \s-1URL\s0
static const stringurl_ch = "~;/?:@=&$-_.+!*'(),";
// see whether `c' can appear in a \s-1URL\s0 and returnthe negative
return !(isalnum(c) ||
find(url_ch.begin(), url_ch.end(), c) !=url_ch.end());
}
string::const_iterator url_beg(string::const_iterator b,
string::const_iterator e) {
static const stringsep = "://";
typedef string::const_iterator iter;
// `i' marks where the separator was found
iter i = b;
while ((i = search(i, e, sep.begin(), sep.end())) !=e) {
// make sure the separator isn't at the beginning or end ofthe line
if (i != b && i + sep.size() != e) {
// `beg' marks the beginning of the protocol-name
iter beg = i;
while (beg != b && isalpha(beg[-1]))
--beg;
// is there at least one appropriate character before andafter the separator?
if (beg != i && !not_url_char(i[sep.size()]))
return beg;
}
// the separator we found wasn't part of a \s-1URL\s0;advance `i' past this separator
i += sep.size();
}
return e;
}
main.cpp
#include <map>
#include <iostream>
#include <string>
#include <vector>
#include <conio.h>
#include "split.h"
#include "urls.h"
using namespace std;
// find all the lines that refer to each word in theinput
map<string, vector<int>> xref(istream& in,
vector<string> find_words(const string&) = split) {
string line;
int line_number = 0;
map<string,vector<int> > ret;
// read the next line
while (getline(in, line)) {
++line_number;
// break the input line into words
vector<string> words = find_urls(line);
// remember that each word occurs on the current line
for (vector<string>::const_iterator it =words.begin();
it != words.end(); ++it)
ret[*it].push_back(line_number);
}
return ret;
}
int main() {
// call `xref' using `split' by default
map<string,vector<int> > ret = xref(cin);
// write the results
for (map<string, vector<int> >::const_iterator it = ret.begin();
it != ret.end();++it) {
// write the word
cout <<it->first << " occurs on line(s):";
// followed by one or more line numbers
vector<int>::const_iterator line_it =it->second.begin();
cout <<*line_it; //write the first line number
++line_it;
// write the rest of the line numbers, if any
while (line_it != it->second.end()) {
cout << ", " << *line_it;
++line_it;
}
// write a new line to separate each word from the next
cout << endl;
}
getch();
return 0;
}