這是一個解決方案,可以實現您陳述的目標。 See it live here。
它利用std::map
保持一個(類別,字)對發生的次數的計數。
std::istringstream
用於將數據首先分解成行,然後分解成單詞。
OUTPUT:
(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;
}
}
請出示你到目前爲止做了什麼的例子。 – chux
像@chux說的,給我們一些代碼來處理。 –
我不同意關閉此問題的決定。這個問題的意圖似乎很清楚,從第一段的最後兩句開始。以下OP的例子和最後一句話完全足夠。最後,我相信我在[我的回答](http://stackoverflow.com/a/16868853/1497596)中提供的示例輸出與OP期望的匹配。他人提供的答案也向我表明了對問題核心的理解。 – DavidRR