使用 map 关联容器 统计文章中原音字母的个数
2011-04-22 23:34
363 查看
开始这样写
读出结果比较麻烦,后来 写了个
int getCount(std::map<char, int> &lCount , char ch);
读出的方法 简单点,但是还是不太方便
能否在包装一下呢,于是使用字符串当参数,可能多个参数一起传入
和前面的函数也不冲突,可以重载使用
#include <iostream> // 数据流输入/输出 #include <string> // 字符串类 #include <algorithm> // STL 通用算法 #include <vector> // STL 动态数组容器 #include <fstream> //文件输入/输出 #include <cstdio> // 定义输入/输出函数 #include <cctype> // 字符处理 #include <sstream> #include <map> using namespace std; int main() { string IN_TEXT = "AEIOU, or A.E.I.O.U., was a symbolic device utilised by the Habsburg emperors. " "Emperor Frederick III (1415-93), who had a fondness for mythical formulae, " "habitually signed buildings and objects with the acronym.[1] Frederick III did not "; istringstream instr(IN_TEXT); // instr 代替 cin 人工输入测试字符 char ch; map<char, int> letterCount; while (instr >> ch) { if (isalpha(ch)) { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') ++letterCount[ch]; if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') ++letterCount[ch]; } } int aCnt, eCnt, iCnt, oCnt, uCnt; // 定义计数器,准备读出原音计数 aCnt = eCnt = iCnt = oCnt = uCnt = 0; if (letterCount.count('a')) // 读出小写 a 计数 aCnt = letterCount['a']; if (letterCount.count('A')) // 加上大写 A 计数 aCnt += letterCount['A']; if (letterCount.count('e')) eCnt = letterCount['e']; if (letterCount.count('E')) eCnt += letterCount['E']; if (letterCount.count('i')) iCnt = letterCount['i']; if (letterCount.count('I')) iCnt += letterCount['I']; if (letterCount.count('o')) oCnt = letterCount['o']; if (letterCount.count('O')) oCnt += letterCount['O']; if (letterCount.count('u')) uCnt = letterCount['u']; if (letterCount.count('U')) uCnt += letterCount['U']; cout << "Number of vowel a and A:/t" << aCnt << endl; cout << "Number of vowel e and E:/t" << eCnt << endl; cout << "Number of vowel i and I:/t" << iCnt << endl; cout << "Number of vowel o and O:/t" << oCnt << endl; cout << "Number of vowel u and U:/t" << uCnt << endl; return 0; }
读出结果比较麻烦,后来 写了个
int getCount(std::map<char, int> &lCount , char ch);
读出的方法 简单点,但是还是不太方便
#include <iostream> // 数据流输入/输出 #include <string> // 字符串类 #include <algorithm> // STL 通用算法 #include <vector> // STL 动态数组容器 #include <fstream> //文件输入/输出 #include <cstdio> // 定义输入/输出函数 #include <cctype> // 字符处理 #include <sstream> #include <map> int getCount(std::map<char, int> &lCount , char ch); int main() { using namespace std; string IN_TEXT = "AEIOU, or A.E.I.O.U., was a symbolic device utilised by the Habsburg emperors. " "Emperor Frederick III (1415-93), who had a fondness for mythical formulae, " "habitually signed buildings and objects with the acronym.[1] Frederick III did not "; istringstream instr(IN_TEXT); // instr 代替 cin 人工输入测试字符 char ch; map<char, int> letterCount; while (instr >> ch) { if (isalpha(ch)) { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') ++letterCount[ch]; if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') ++letterCount[ch]; } } int aCnt, eCnt, iCnt, oCnt, uCnt; // 定义计数器,准备读出原音计数 aCnt = getCount(letterCount,'a'); // 读出小写 a 计数 aCnt += getCount(letterCount,'A'); // 加上大写 A 计数 eCnt = getCount(letterCount,'e'); eCnt += getCount(letterCount,'E'); iCnt = getCount(letterCount,'i'); iCnt += getCount(letterCount,'I'); oCnt = getCount(letterCount,'o'); oCnt += getCount(letterCount,'O'); uCnt = getCount(letterCount,'u'); uCnt += getCount(letterCount,'U'); cout << "Number of vowel a and A:/t" << aCnt << endl; cout << "Number of vowel e and E:/t" << eCnt << endl; cout << "Number of vowel i and I:/t" << iCnt << endl; cout << "Number of vowel o and O:/t" << oCnt << endl; cout << "Number of vowel u and U:/t" << uCnt << endl; return 0; } int getCount(std::map<char, int> &lCount , char ch) { int Count=0; if (lCount.count(ch)) Count = lCount[ch]; return Count; }
能否在包装一下呢,于是使用字符串当参数,可能多个参数一起传入
和前面的函数也不冲突,可以重载使用
#include <iostream> // 数据流输入/输出 #include <string> // 字符串类 #include <algorithm> // STL 通用算法 #include <vector> // STL 动态数组容器 #include <fstream> //文件输入/输出 #include <cstdio> // 定义输入/输出函数 #include <cctype> // 字符处理 #include <sstream> #include <map> int getCount(std::map<char, int> &lCount , const char *str); int main() { using namespace std; string IN_TEXT = "AEIOU, or A.E.I.O.U., was a symbolic " "device utilised by the Habsburg emperors. "; istringstream instr(IN_TEXT); // instr 代替 cin 人工输入测试字符 char ch; map<char, int> letterCount; while (instr.get(ch)) { // 改成 cin.get() 就是人工输入 if (isalpha(ch)) { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') ++letterCount[ch]; if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') ++letterCount[ch]; } if (isspace(ch)) { ++letterCount[ch]; } } int aCnt, eCnt, iCnt, oCnt, uCnt , spCnt , total ; // 定义计数器,准备读出原音计数 total = getCount(letterCount, "aeiouAEIOU"); aCnt = getCount(letterCount, "aA"); // 读出小写 a 计数 eCnt = getCount(letterCount, "eE"); iCnt = getCount(letterCount, "iI"); oCnt = getCount(letterCount, "oO"); uCnt = getCount(letterCount, "uU"); spCnt = letterCount[' ']; cout << "Number of vowel a and A:/t" << aCnt << endl; cout << "Number of vowel e and E:/t" << eCnt << endl; cout << "Number of vowel i and I:/t" << iCnt << endl; cout << "Number of vowel o and O:/t" << oCnt << endl; cout << "Number of vowel u and U:/t" << uCnt << endl; cout << "Number of vowel total :/t" << total << endl; cout << "Number of vowel space :/t" << spCnt << endl; return 0; } int getCount(std::map<char, int> &lCount , const char *str) { int Count = 0; while(*str) { if (lCount.count(*str)) Count += lCount[*str]; str++; } return Count; }
相关文章推荐
- map等关联容器 vector等序列容器 如何防止迭代器失效 即erase()的使用
- C++中防止STL中迭代器失效__map/set等关联容器vector/list/deque等序列容器_如何防止迭代器失效_即erase()的使用
- 关联容器map应用-统计单词出现频率
- C++中防止STL中迭代器失效__map/set等关联容器vector/list/deque等序列容器_如何防止迭代器失效_即erase()的使用
- map等关联容器 vector等序列容器 如何防止迭代器失效 即erase()的使用
- C++中防止STL中迭代器失效——map/set等关联容器——vector/list/deque等序列容器—如何防止迭代器失效—即erase()的使用
- map等关联容器 vector等序列容器 如何防止迭代器失效 即erase()的使用
- 使用 MAP CLASS 库创建关联容器
- 使用map容器,统计输入的单词出现的次数
- 统计频率(map映照容器的使用)
- STL之关联容器(pair、map、set的使用)
- map等关联容器 vector等序列容器 如何防止迭代器失效 即erase()的使用
- 《C++ Primer》学习笔记:map容器insert操作的使用——编写程序统计并输出所读入的单词出现的次数
- 如何统计出一篇文章出现的文字个数? (高级) (使用std::map)
- STL的基本使用之关联容器:map和multiMap的基本使用
- 【STL容器学习】-关联容器与map的使用方法
- STL之关联容器(pair、map、set的使用)
- C++ 11 特性:关联容器map、set的使用
- 使用map集合统计字符串中字母出现的次数
- 使用map关联容器实现单词转换的程序