codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#include <iostream> // std::cout, std::endl #include <map> // std::map #include <sstream> // std::istringstream #include <utility> // std::pair int main() { // The data. std::string content = "colors red blue green yellow orange purple\n" "sky blue high clouds air empty vast big\n" "ocean wet water aquatic blue\n" "colors brown black blue white blue blue\n"; // Load the data into an in-memory table. std::istringstream table(content); std::string row; std::string category; std::string word; const char delim = ' '; std::map<pair<std::string, std::string>, long> category_map; std::pair<std::string, std::string> cw_pair; long count; // Read each row from the in-memory table. while (!table.eof()) { // Get a row of data. getline(table, row); // Allow the row to be read word-by-word. std::istringstream words(row); // Get the first word in the row; it is the category. getline(words, category, delim); // Get the remaining words in the row. while (std::getline(words, word, delim)) { cw_pair = std::make_pair(category, word); // Maintain a count of each time a (category, word) pair occurs. if (category_map.count(cw_pair) > 0) { category_map[cw_pair] += 1; } else { category_map[cw_pair] = 1; } } } // Print out each unique (category, word) pair and // the number of times that it occurs. std::map<pair<std::string, std::string>, long>::iterator it; for (it = category_map.begin(); it != category_map.end(); ++it) { cw_pair = it->first; category = cw_pair.first; word = cw_pair.second; count = it->second; std::cout << "(" << category << ", " << word << ") => " << count << std::endl; } }
Private
[
?
]
Run code
Submit