/* * Copyright 2011 Steven Gribble * * This file is the solution to an exercise problem posed during * one of the UW CSE 333 lectures (333exercises). * * 333exercises is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * 333exercises is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 333exercises. If not, see . */ // STL lecture exercise 2 #ifndef HISTOGRAM_H_ #define HISTOGRAM_H_ #include // for uint32_t #include // for std::ostream #include // for std::string #include // for std::vector #include // for std::map #include // for std::pair // A Histogram is a class that builds up a histogram of word // occurrence frequency. Internally, it uses an STL map to store // a map from word to integer count. class Histogram { public: Histogram() { } // Add a word occurrence to the histogram. Converts the word to // lowercase before incorporating. void AddWord(const string &word); // Return a vector of pairs, sorted in descending order // of count. std::vector > GetSortedData() const; // Override "<<" for std::ostream. friend std::ostream &operator<<(std::ostream &out, const Histogram &pt); private: // Disable the copy constructor and assignment operator. Histogram(const Histogram &pt); Histogram &operator=(const Histogram &pt); // Our private member variable storing the histogram data. // We use an STL map from word-->count. std::map histogram_data_; }; #endif // HISTOGRAM_H_