C ++ indexed according to text



#include "stdafx.h"
#include <iostream>
#include <fstream> 
#include <set> 
#include <map> 
#include <string> 
#include <cctype>//isalpha, isupper,tolower 
#include <cstdlib>//exit 


using namespace std; 


static int s_lineNum = 1;//static linenumber  


int main(int argc, char *argv[]) 

if (argc < 3) 

cout << "usage: " << argv[0] <<"infile" << "outfile" << endl; 
cout << "generate a word list from an Englishfile," 
<< "each word is followed by the number of the lines  where it occured and then output the resultto a file." << endl; 

else 

//common words 
string commWord[13] = {"a", "an", "and","are", "in", "is", "of",  
"or", "that","the", "this", "to", "have"}; 


set<string> ignore(commWord, commWord + 13);//ignore the commonwords 
set<int> lineNum; 
string word;//contain the word extracted from infile 
map< string, set<int> > wordlist;//associate word withlineNum 
pair< map<string, set<int> >::iterator, bool> pr; 
map< string, set<int> >::iterator itWord; 
set<int>::iterator itLine; 


ifstream infile(argv[1]);//open infile 
if (infile.bad()) 

cout << "open  "<< argv[1] << " error" << endl; 
exit(EXIT_FAILURE); 



ofstream outfile(argv[2]); 
if (outfile.bad()) 

cout << "open " << argv[2] << "error" << endl; 
exit(EXIT_FAILURE); 



char temp; 
while(infile.good()) 

temp = infile.get();//get a char 
while (isalpha(temp)) 

if (isupper(temp)) 

temp = tolower(temp); 

word.append(1, temp); 
temp = infile.get();//get a newchar  

//not common word and not empty 
if (ignore.count(word) == 0 && word.size() != 0) 

lineNum.insert(s_lineNum);//insert the line number into lineNum if it isa new word 
//return a pair, the firstmember is the iterator, the second is a bool type which indicate whether theinsertion is successful or not 
pr = wordlist.insert(pair<string, set<int> >(word, lineNum)); 
//the map already contained anelement whose key had an equivalent value in the ordering 
if (pr.second == false) 

wordlist[word].insert(s_lineNum);//insert the line number into the setcontainer paired with the word 
}                

word.clear(); 
lineNum.clear(); 
if (temp == '\n') 

s_lineNum++; 




//itWord points to pair< string, set<int> >, the firstmember is word, the second is line number  
for (itWord = wordlist.begin(); itWord != wordlist.end(); ++itWord) 

//set output format 
outfile.setf(ios_base::left, ios_base::adjustfield); 
outfile.fill('-'); 
outfile.width(34); 
outfile << itWord->first;
outfile << " "; 


for (itLine = itWord->second.begin(); itLine !=itWord->second.end(); ++itLine) 

outfile << *itLine<< ' '; 

outfile << endl; 



infile.close(); 
infile.open(argv[2]); 
cout << infile.rdbuf();//streambuf, print the output file 
infile.close(); 
outfile.close(); 

map< int,string > ss;
ss.insert(pair<int,string>(6,"aa"));
ss.insert(pair<int,string>(4,"zz"));
ss.insert(pair<int,string>(5,"gg"));
map< int,string >::iterator pt;
for(pt = ss.begin();pt!=ss.end();++pt)
{
cout<< pt->second<<endl;


}




//system("pause"); 
return 0; 
}
Published 12 original articles · won praise 6 · views 20000 +

Guess you like

Origin blog.csdn.net/huaweizte123/article/details/51761261