[英]Vector of Structs of Vector and String Not printing correctly
我是一名刚开始使用C ++的学生,并且正在尝试编写一个程序,该程序将一个单词带入文件并对其进行索引,每个单词仅列出一次,并在每次出现该单词时显示行号。 我曾尝试使用地图,但发现无法获得单词的行号。 取而代之的是,我使用的是结构的向量,每个向量都有一个整数向量和一个字符串。 我试图读取每个单词,将其放入字符串流,然后将其输出到结构中的字符串中。 然后,我将行号并将其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;
}
对于程序的这一部分:
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
,而不仅仅是当前单词的行号。 您可能需要在每次运行时清除它。
您的问题似乎是您总是将每个单词附加到vector<words>
。
您可能想要的是map<string, vector<int>>
。 使用.insert在集合中插入单词(如果已经存在),则只需将当前行号附加到值上。
这里有一些问题:
添加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";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.