-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathunordered map.cpp
More file actions
39 lines (32 loc) · 1.01 KB
/
unordered map.cpp
File metadata and controls
39 lines (32 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <unordered_map>
#include <sstream>
int main() {
std::string text = "This is a sample text. This is a simple example.";
// Create an unordered_map to store word frequencies
std::unordered_map<std::string, int> wordFrequency;
// Tokenize the text into words
std::istringstream iss(text);
std::string word;
while (iss >> word) {
// Remove punctuation and convert to lowercase (you may need a more comprehensive approach)
for (char& c : word) {
if (std::ispunct(c)) {
c = ' ';
}
else {
c = std::tolower(c);
}
}
// Increment the frequency in the map
if (!word.empty()) {
wordFrequency[word]++;
}
}
// Display the word frequencies
for (const auto& pair : wordFrequency) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
return 0;
}