?? test.txt
字號:
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>
#include <map>
using namespace std;
//#include <string.h>
//#include <stdio.h> #include <stdlib.h>
int main()
{
ifstream ifile("test.txt");
map<string, int>strmap; // 定義map
map<string, int>::iterator it;// 定義map迭代器
string str, w;
int line=0, words=0, i;
/* 構造單詞字符集*/
string pum("_");
for(i='A'; i<'Z'+1; i++) pum+=i;
for(i='a'; i<'z'+1; i++) pum+=i;
for(i='0'; i<'9'+1; i++) pum+=i;
string::size_type index, end;
while(!ifile.eof())
{
getline(ifile, str);
line++;
index=str.find_first_of(pum); //尋找單詞起點
while(index != string::npos)
{
end = str.find_first_not_of(pum, index); //尋找單詞終點
words++; //++
w = str.substr(index, end-index); //截取單詞
it = strmap.find(w);
if(it == strmap.end())
strmap.insert(make_pair(w, 1));
else it->second++;
if(end == string::npos)break; //如果行結束, 則break跳出
else
index=str.find_first_of(pum, end); //否則尋找下一個起點
}
}
cout<<"Result:"<<endl
<<"Total line(s) is: "<<line<<endl
<<"Total word(s) is: "<<words<<endl
<<"Count word \"if\":"<<strmap["if"]<<endl
<<"Count word \"while\":"<<strmap["while"]<<endl
<<"Count word \"case\":"<<strmap["case"]<<endl;
cout<<endl<<"There are such word(s) in this file:"<<endl;
for(it = strmap.begin(); it!=strmap.end(); it++)
cout<<"Word:\""<<it->first<<"\", Count:"<<it->second<<endl;
system("pause");
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -