#include <iostream>
#include <map>
#include <string>
using namespace std;
void m(int size, string s[]) {
map<string, int> wordCounts;
for (int i = 0; i < size; ++i) {
wordCounts[s[i]]++;
}
cout << "There are " << wordCounts.size() << " distinct words:";
for (auto it = wordCounts.begin(); it != wordCounts.end(); ++it) {
if (it != wordCounts.begin()) {
cout << ",";
}
cout << it->first;
}
cout << "." << endl;
for (const auto& pair : wordCounts) {
cout << pair.first << " occurs " << pair.second << (pair.second > 1 ? " times" : " time") << " ." << endl;
}
}
int main() {
string s[] = {"80", "90", "10", "70", "90", "90", "10"};
int size = sizeof(s) / sizeof(s[0]);
m(size, s);
return 0;
}
這兩個程式都使用了相應語言的標準程式庫中的容器來存儲和計數每個獨特字串的出現次數。Java版本使用了HashMap來實現這一功能,而C++版本則使用了std::map。兩者都能有效地完成任務,並按要求輸出結果。