我是一名初学c ++学生,我正在尝试编写一个程序,该程序在文件中对单词进行索引并对其进行索引,仅列出每个单词一次,并显示每次单词出现时的行号。我试过使用地图,但我发现无法获得单词的行号。相反,我使用的结构向量具有整数向量和每个单词的字符串。我试图读取每个单词,将其放入stringstream,然后将其输出到struct中的字符串中。然后我将行号和push_back引入结构中的向量中。然后我将所有内容保存到结构的向量中,并尝试打印出与行号向量相关联的每个单词。我无处可去,希望得到一些帮助。非常感谢!这是我的源代码:
#include <iostream>
#include <string>
#include <fstream>
#include <map>
#include <sstream>
#include <vector>
using namespace std;
struct words {
vector<int> lineNumbers;
string word;
};
int main() {
ifstream inFile, testStream;
ofstream outFile;
string temp, choice, inFileName, outFileName, word, trash, word2, tempString;
int idx = 0, count = 0, idxTwo = 0;
bool outputOpened = false;
//map <string,int> wordList;
/map <string,int>::iterator wordIt;
stringstream myStream(ios_base::in| ios_base::out);
vector<words> myIndex;
words data;
for (;;) {
cout << "Options: "<< endl << "1. Index" << endl << "2. Quit" << endl
<< "Please enter an option: ";
getline(cin, temp);
//cin >> temp;
//cin.ignore(8192, '\n');
choice.resize(temp.length());
transform(temp.begin(), temp.end(), choice.begin(), ::toupper);
if (choice.compare("INDEX") == 0 || choice.compare("1") == 0) {
do {
inFileName.clear();
cout << "Index Program" << endl
<< "==============" << endl << endl;
cout << "Input file name: ";
getline(cin, inFileName);
inFile.open(inFileName.c_str());
if(inFile.fail()) {
cout << "Can't open file" << endl;
if(inFile.bad()) {
cout << "Bad" << endl;
}
inFile.clear();
}
}
while (!inFile.is_open());
do {
cout << "Output file name: ";
getline( cin, outFileName);
testStream.clear();
testStream.open(outFileName.c_str());
if(testStream.good()) {
cout << "That file already exists, try again" << endl;
testStream.clear();
testStream.close();
}
else {
testStream.clear();
testStream.close();
outFile.open(outFileName.c_str());
if (outFile.good()) {
outputOpened = true;
}
}
}
while (!outputOpened);
while (getline(inFile, trash)){
count++;
myStream << trash;
//myStream >> tempString;
while(myStream >> data.word) {
data.lineNumbers.push_back(count);
myIndex.push_back(data);
}
}
for (idx = 0; idx < myIndex.size(); idx++) {
outFile << "Word: "<< " "<< myIndex[idx].word << ", ";
for (idxTwo = 0; idxTwo < myIndex[idx].lineNumbers.size(); idxTwo++) {
outFile << "Found on lines " << " " << myIndex[idx].lineNumbers[idxTwo];
}
}
inFile.close();
outFile.close();
}
else if (choice.compare("QUIT") == 0 || choice.compare("2") == 0) {
return 0;
}
else {
cout << temp << " is an unrecognized option, please try again" << endl;
}
}
return 0;
}
答案 0 :(得分:0)
对于这部分程序:
while (getline(inFile, trash)){
count++;
myStream << trash;
//myStream >> tempString;
while(myStream >> data.word) {
// 2.
// data.lineNumbers.clear();
data.lineNumbers.push_back(count);
myIndex.push_back(data);
}
// 1. add:
// myStream.clear();
}
您正在使用全局myStream
。但是,处理完每一行后,myStream
的状态将处于不良状态,因为数据耗尽。国家已被清除,以便再次运作。
data
也是全局的,它会将每个单词的行号保存在data.lineNumber
中,而不仅仅是当前单词的行号。您可能希望在每次运行时清除它。
答案 1 :(得分:0)
您的问题似乎是,您始终将每个单词附加到vector<words>
。
你可能想要的是map<string, vector<int>>
。使用.insert将单词插入到集合中,如果它已经存在,则只需将当前行号附加到该值。
答案 2 :(得分:0)
以下是一些问题:
在myStream.clear();
循环后添加while(myStream >> data.word)
。
将“\ n”添加到以下行的末尾以在新行上打印:
outFile << "Found on lines " << " " << myIndex[idx].lineNumbers[idxTwo]<<"\n";
此外,以下嵌套循环不会给您正确的结果。它将随行号增长并继续打印“在线上找到”:
for (idx = 0; idx < myIndex.size(); idx++) {
outFile << "Word: "<< " "<< myIndex[idx].word << ", ";
for (idxTwo = 0; idxTwo < myIndex[idx].lineNumbers.size(); idxTwo++) {
outFile << "Found on lines " << " " << myIndex[idx].lineNumbers[idxTwo]<<"\n";
}