日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

实现一个简单的代码字计数器(二)

發(fā)布時間:2024/10/12 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实现一个简单的代码字计数器(二) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
  • 分割字符串
  • 實現(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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。