0
모음을 집계하는 함수를 작성했습니다. 하천의 끝 부분에 모음이 있으면 두 번 계산됩니다. 왜? 마지막 읽기 때문에 데이터의 부족으로 실패했을 때 in
이 거짓으로 평가하기 때문에예상 한대로 istream의 끝이 검색되지 않습니다.
#include <iostream>
#include <string>
#include <map>
using namespace std;
void countChars(istream& in, string theChars, ostream& out) {
map<char, int> charMap;
map<char, int>::iterator mapIt;
for (string::iterator i = theChars.begin(); i != theChars.end(); ++i) {
charMap[*i] = 0;
}
while (in) {
char c;
in >> c;
c = tolower(c);
if (charMap.count(c))
++charMap[c];
}
for (mapIt = charMap.begin(); mapIt != charMap.end(); ++mapIt) {
out << (*mapIt).first << ":" << (*mapIt).second << endl;
}
}
int main(int argc, char **argv) {
std::string s = "aeiou";
countChars(std::cin, s, std::cout);
}
시도 '동안 (>>에서 C)'대신''(에서) 동안? – Bill