实现一个简单的代码字计数器(二)
- 分割字符串
- 實現(xiàn)一個簡單的代碼字計數(shù)器(一)
- 實現(xiàn)一個簡單的代碼字計數(shù)器(二)
- 實現(xiàn)一個簡單的代碼字計數(shù)器(三)
- 實現(xiàn)一個簡單的代碼字計數(shù)器(四)
這一篇里讓我們先實現(xiàn)基本功能,特性和改善放在后幾篇實現(xiàn)里面。
要使一個函數(shù)執(zhí)行一段代碼的單詞計數(shù),讓我們從設計它的接口開始。我們考慮輸出的形式應該是什么樣的:輸入為code文本,輸出應該為單詞:出現(xiàn)次數(shù),這樣的話用map實現(xiàn):
std::map<std::string, size_t> getWordCount(std::string const& code);但是為了以后的設計,因為我們肯定需要知道出現(xiàn)次數(shù)最大的單詞,如果用map的話需要設置迭代器遍歷,通過it->second來判斷大小,不夠方便,所以這里采用vector來做可能更加妥當,因為之后還可以直接利用sort來進行排序操作:
std::vector<std::pair<std::string, size_t>> getWordCount(std::string const& code);接下來要處理分隔符的問題,因為可能存在空格、逗號或者&&,||和->這樣的符號,所以需要通過分割字符串將這些分隔符去掉:
bool isDelimiter(char c) {auto const isAllowedInName = isalnum(c) || c == '_';return !isAllowedInName; }auto symbols = std::vector<std::string>{}; boost::split(symbols, code, isDelimiter); symbols.erase(std::remove(begin(symbols), end(symbols), ""), end(symbols));然后是計數(shù)函數(shù):
std::map<std::string, size_t> countWords(std::vector<std::string> const& words) {auto wordCount = std::map<std::string, size_t>{};for (auto const& word : words){++wordCount[word];}return wordCount; }進行排序操作:
auto sortedWordCount = WordCount(begin(wordCount), end(wordCount)); //類型轉(zhuǎn)換std::sort(begin(sortedWordCount), end(sortedWordCount), [](auto const& p1, auto const& p2) { return p1.second > p2.second; });基本操作就完成了,那么我們怎么進行測試呢?先進行本地測試吧,在代碼中嵌入一小段代碼進行測試,那么問題來了,代碼格式中可能有空格、引用符號、代碼分散在幾行,我們怎樣才能做到解決這個問題呢?好在C++11中的原始字符串字面量正好解決了這類問題。我們可以寫一個R在這些代碼之前:R"(this is a code test)"。
所有代碼如下:
#include<iostream> #include<iomanip> #include<string> #include<map> #include<vector> #include<iterator> #include<boost/algorithm/string.hpp>using WordCount = std::vector<std::pair<std::string, size_t>>; WordCount getWordCount(std::string const& code);bool isDelimiter(char c) {auto const isAllowedInName = isalnum(c) || c == '_';return !isAllowedInName; }std::map<std::string, size_t> countWords(std::vector<std::string> const& words) {auto wordCount = std::map<std::string, size_t>{};for (auto const& word : words){++wordCount[word];}return wordCount; }WordCount getWordCount(std::string const& code) {auto symbols = std::vector<std::string>{};boost::split(symbols, code, isDelimiter);symbols.erase(std::remove(begin(symbols), end(symbols), ""), end(symbols));auto const wordCount = countWords(symbols);auto sortedWordCount = WordCount(begin(wordCount), end(wordCount)); //類型轉(zhuǎn)換std::sort(begin(sortedWordCount), end(sortedWordCount), [](auto const& p1, auto const& p2) { return p1.second > p2.second; });return sortedWordCount; }//void print(WordCount const& entries) //{ // for (auto const& entry : entries) // { // std::cout << std::setw(30) << std::left << entry.first << '|' << std::setw(10) << std::right << entry.second << '\n'; // } //}void print(WordCount const& entries) {if (entries.empty()) return;auto const longestWord = *std::max_element(begin(entries), end(entries), [](auto const& p1, auto const& p2) { return p1.first.size() < p2.first.size(); });auto const longestWordSize = longestWord.first.size();for (auto const& entry : entries){std::cout << std::setw(longestWordSize + 1) << std::left << entry.first << '|' << std::setw(10) << std::right << entry.second << '\n';} }static constexpr auto code = R"( bool isDelimiter(char c) { auto const isAllowedInName = isalnum(c) || c == '_'; return !isAllowedInName; } std::map<std::string, size_t> countWords(std::vector<std::string> const& words) { auto wordCount = std::map<std::string, size_t>{}; for (auto const& word : words) { ++wordCount[word]; } return wordCount; } WordCount getWordCount(std::string const& code) { auto symbols = std::vector<std::string>{}; boost::split(symbols, code, isDelimiter); symbols.erase(std::remove(begin(symbols), end(symbols), ""), end(symbols)); auto const wordCount = countWords(symbols); auto sortedWordCount = WordCount(begin(wordCount), end(wordCount)); std::sort(begin(sortedWordCount), end(sortedWordCount), [](auto const& p1, auto const& p2){ return p1.second > p2.second; }); return sortedWordCount; } })";int main() {print(getWordCount(code));system("pause"); }結(jié)果如下:
轉(zhuǎn)載于:https://www.cnblogs.com/yunlambert/p/10131248.html
總結(jié)
以上是生活随笔為你收集整理的实现一个简单的代码字计数器(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 问一下柜体板材选什么品牌的会比较好?
- 下一篇: tcpdump软件使用