-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathregexIterator.cpp
34 lines (23 loc) · 1.04 KB
/
regexIterator.cpp
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
#include <regex>
#include <iostream>
#include <string>
#include <map>
#include <unordered_map>
int main(){
std::cout << '\n';
// Bjarne Stroustrup about C++11 on http://www.stroustrup.com/C++11FAQ.html
std::string text{"That's a (to me) amazingly frequent question. It may be the most frequently asked question. Surprisingly, C++11 feels like a new language: The pieces just fit together better than they used to and I find a higher-level style of programming more natural than before and as efficient as ever."};
// regular expression for a word
std::regex wordReg(R"(\w+)");
// get all words from text
std::sregex_iterator wordItBegin(text.begin(), text.end(), wordReg);
const std::sregex_iterator wordItEnd;
// use a map to count the wourds
std::unordered_map<std::string, std::size_t> allWords;
// count the words
for (; wordItBegin != wordItEnd;++wordItBegin){
++allWords[wordItBegin->str()];
}
for (auto wordIt: allWords) std::cout << "(" << wordIt.first << ":" << wordIt.second << ")" ;
std::cout << '\n';
}