//: C07:MultiSetWordCount.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. // Count occurrences of words using a multiset. #include #include #include #include #include #include "../require.h" using namespace std; int main(int argc, char* argv[]) { const char* fname = "MultiSetWordCount.cpp"; if(argc > 1) fname = argv[1]; ifstream in(fname); assure(in, fname); multiset wordmset; string word; while(in >> word) wordmset.insert(word); typedef multiset::iterator MSit; MSit it = wordmset.begin(); while(it != wordmset.end()) { pair p = wordmset.equal_range(*it); int count = distance(p.first, p.second); cout << *it << ": " << count << endl; it = p.second; // Move to the next word } } ///:~