이 샘플 초안은 주어진 문자열의 총 단어 수를 계산할 뿐만 아니라 C++ 프로그래밍 코드에서 stringstream을 사용하여 특정 단어의 총 발생 횟수를 계산합니다. stringstream 클래스는 문자열 객체를 스트림과 함께 사용하여 마치 스트림인 것처럼 문자열을 읽을 수 있도록 합니다. 이 코드는 두 가지 업적을 달성해야 합니다. 먼저 총 단어 수를 계산한 다음 다음과 같이 mapiterator 필수 방법을 사용하여 문자열에서 개별 단어의 빈도를 계산합니다.
예시
#include <bits/stdc++.h>
using namespace std;
int totalWords(string str){
stringstream s(str);
string word;
int count = 0;
while (s >> word)
count++;
return count;
}
void countFrequency(string st){
map<string, int> FW;
stringstream ss(st);
string Word;
while (ss >> Word)
FW[Word]++;
map<string, int>::iterator m;
for (m = FW.begin(); m != FW.end(); m++)
cout << m->first << " = " << m->second << "\n";
}
int main(){
string s = "Ajay Tutorial Plus, Ajay author";
cout << "Total Number of Words=" << totalWords(s)<<endl;
countFrequency(s);
return 0;
} 출력
문자열 "Ajay Tutorial Plus, Ajay 작성자 "이 프로그램에 제공되면 다음과 같이 출력되는 단어의 총 개수와 빈도;
Enter a Total Number of Words=5 Ajay=2 Tutorial=1 Plus,=1 Author=1