在使用一个同义语汇编 thesaurus 时
想知道所有与某个特定单词相似的所有单词
多重容器 multiset multimap 都不合适
解决方案是将容器联合在一起使用
//: C07:Thesaurus.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// A map of vectors.
#include <map>
#include <vector>
#include <string>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <ctime>
#include <cstdlib>
using namespace std;
typedef map<string, vector<string> > Thesaurus;
typedef pair<string, vector<string> > TEntry;
typedef Thesaurus::iterator TIter;
// Name lookup work-around:
namespace std {
ostream& operator<<(ostream& os,const TEntry& t) {
os << t.first << ": ";
copy(t.second.begin(), t.second.end(),
ostream_iterator<string>(os, " "));
return os;
}
}
// A generator for thesaurus test entries:
class ThesaurusGen {
static const string letters;
static int count;
public:
int maxSize() { return letters.size(); }
TEntry operator()() {
TEntry result;
if(count >= maxSize()) count = 0;
result.first = letters[count++];
int entries = (rand() % 5) + 2;
for(int i = 0; i < entries; i++) {
int choice = rand() % maxSize();
char cbuf[2] = { 0 };
cbuf[0] = letters[choice];
result.second.push_back(cbuf);
}
return result;
}
};
int ThesaurusGen::count = 0;
const string ThesaurusGen::letters("ABCDEFGHIJKL"
"MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
// Ask for a "word" to look up:
string menu(Thesaurus& thesaurus) {
while(true) {
cout << "Select a \"word\", 0 to quit: ";
for(TIter it = thesaurus.begin();
it != thesaurus.end(); it++)
cout << (*it).first << ' ';
cout << endl;
string reply;
cin >> reply;
if(reply.at(0) == '0') exit(0); // Quit
if(thesaurus.find(reply) == thesaurus.end())
continue; // Not in list, try again
return reply;
}
}
int main() {
srand(time(0)); // Seed the random number generator
Thesaurus thesaurus;
// Fill with 10 entries:
generate_n(inserter(thesaurus, thesaurus.begin()),
10, ThesaurusGen());
// Print everything:
copy(thesaurus.begin(), thesaurus.end(),
ostream_iterator<TEntry>(cout, "\n"));
// Create a list of the keys:
string keys[10];
int i = 0;
for(TIter it = thesaurus.begin();
it != thesaurus.end(); it++)
keys[i++] = (*it).first;
for(int count = 0; count < 10; count++) {
// Enter from the console:
// string reply = menu(thesaurus);
// Generate randomly
string reply = keys[rand() % 10];
vector<string>& v = thesaurus[reply];
copy(v.begin(), v.end(),
ostream_iterator<string>(cout, " "));
cout << endl;
}
getchar();
} ///:~
输出
A: u i y
B: w o
C: J A Y M
D: U J E D
E: y r l b F f
F: d y h m F E
G: V b p F N B
H: H G V l
I: m f r
J: B p h u p
u i y
J A Y M
d y h m F E
U J E D
B p h u p
V b p F N B
u i y
U J E D
w o
J A Y M
Thesaurus将一个string 映射到一个vector<string>
TEntry是Thesaurus中的一个条目
通过为TEntry创建一个输出流操作符ostream operator<<
可以很容易地打印来自Thesaurus中的条目