这是实现您既定目标的解决方案。看到它住在这里。
它std::map
用于维护(类别,单词)对出现的次数。
std::istringstream
用于将数据首先分解为行,然后分解为单词。
输出:
(colors, black) => 1
(colors, blue) => 4
(colors, brown) => 1
(colors, green) => 1
(colors, orange) => 1
(colors, purple) => 1
(colors, red) => 1
(colors, white) => 1
(colors, yellow) => 1
(ocean, aquatic) => 1
(ocean, blue) => 1
(ocean, water) => 1
(ocean, wet) => 1
(sky, air) => 1
(sky, big) => 1
(sky, blue) => 1
(sky, clouds) => 1
(sky, empty) => 1
(sky, high) => 1
(sky, vast) => 1
程序:
#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;
}
}