ファイル内の一意の単語を印刷するC++プログラム
ファイルは、ワードストリームを格納するメモリの場所です。ファイルにはいろいろな言葉があります。このプログラムでは、ファイルからすべての一意の単語を見つけて印刷します。
ユニーク 単語とは、その単語の出現回数がファイル内で1回であることを意味します。
たとえば、
Tutorials point is best for programming tutorials.
ここでは、チュートリアルという単語が複数回出現するため、一意ではありません。残りのすべての単語は一意です。
アルゴリズム
To check for unique words in the given file. Using iterator with two variables : data and occurence. Input : File Step 1 : Read each line from the file and follow step 2 Step 2 : check for the occurence of the word in the data structure using interator.data. Step 2.1 : if data matches increase the occurrence by one corresponding to the data. Step 2.2 : if data does not match add new value and set its occurence to one. Step 3: Iterate over the date structure. And check for occurence value of each value. Step 3.1 : If the occerence is equals to 1 then prints the data corresponding it else do nothing.
例
#include <bits/stdc++.h>
using namespace std;
int main(){
char filename[] = "test.txt";
ofstream fs("test.txt", ios::trunc);
fs << "tutorials point is best for programming tutorials";
fs.close();
fstream fs("test.txt");
map<string, int> mp;
string word;
while (fs >> word){
if (!mp.count(word))
mp.insert(make_pair(word, 1));
else
mp[word]++;
}
fs.close();
for (map<string, int> :: iterator p = mp.begin();
p != mp.end(); p++){
if (p->second == 1)
cout << p->first << endl;
}
return 0;
} 出力
best for is point Programming
-
お誕生日おめでとうを印刷するC++プログラム
これは、お誕生日おめでとうを印刷するためのC++プログラムです。 アルゴリズム Begin Take a str1 which takes the next character of our desired ouput like for H it will be G. Assign the string to a pointer p. Make a while loop till *p != NULL. Go next character of the string prin
-
ユーザーが入力した番号を印刷するC++プログラム
オブジェクト「cin」と「cout」は、C++でそれぞれ入力と出力に使用されます。 cinはistreamクラスのインスタンスであり、キーボードなどの標準入力デバイスに接続されています。 coutはostreamクラスのインスタンスであり、表示画面などの標準出力デバイスに接続されています。 ユーザーが入力した数字を印刷するプログラムは次のとおりです- 例 #include <iostream> using namespace std; int main() { int num; cout<<"Enter t