查找单词

#include #include #include #include #include #include using namespace std; int main(int argc, char *argv[]) { string inputFileName("input.txt"); ifstream inputFile(inputFileName.c_str()); if(!inputFile) { cerr << "Can not open the file: " << inputFile << endl; return -1; } map > wordTable; string eachLine; int lineIndex = 0; while(!inputFile.eof()) { getline(inputFile, eachLine); istringstream is(eachLine); string eachWord; while(is >> eachWord) wordTable[eachWord].insert(lineIndex); lineIndex++; } string searchWord; ofstream output("output.txt", ios::app); while(true) { cout << "please input the word want to search(type /"q/" to quit): "; cin >> searchWord; if(searchWord == "q" || !cin ) break; map >::iterator it = wordTable.find(searchWord); if(it != wordTable.end()) { set::iterator setIt = (it->second).begin(); while(setIt != (it->second).end()) output << *setIt++ << endl; } else output << "NOT FIND" << endl; } system("PAUSE"); return EXIT_SUCCESS; }  

这个是c++ primerP379的一个例子,实现了一下...

你可能感兴趣的:(STL练习,iterator,string,output,include,search,system)