2011-12-12 172 views
0

我是C++的初学者,我可以从一个文件动态地添加单词到一个矢量数组,但我想要记录每个单词并找出该单词在文件中出现的次数,只打印一次该单词并列出每次出现的行号。我不确定从我的词语向量中何去何从。有没有办法比较每个字符串元素?下面是我的源代码:比较元素的矢量

#include <iostream> 
#include <string> 
#include <fstream> 
#include <vector> 
#include <sstream>#include 
using namespace std; 
int main() { 
ifstream inFile, testStream; 
ofstream outFile; 
vector<string> words; 
string temp, choice, inFileName, outFileName, word, trash; 
int idx = 0, lineCount = 0; 
bool outputOpened = false; 
stringstream wordStream;  
for (;;) { 
    cout << "Options: "<< endl << "1. Index" << endl << "2. Quit" << endl 
    << "Please enter an option: "; 
    getline(cin, temp); 
    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 (inFile.peek() != EOF) { 
      getline(inFile,word, ' '); 

      lineCount++; 


      words.push_back(word); // now the vector 'words' contains all words in the file 
     } 
    for (idx = 0; idx < words.size(); idx++) { 
     outFile << words[idx] << endl; 
    } 
} 
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

我有一个问题,使用地图的容器,这是我成功的做到了,我将如何合并每个单词所在的行号?如果我在while循环中添加了一个计数器,它只会在文件中的每个单词之后递增,而不是每个新行。 –

回答

2

下面是一些提示:

  1. 代替vector,考虑使用map - 这将让你关联与给定的字计数
  2. 当插入一个单词时,看看地图是否包含它,如果是,则增加计数,否则插入一个计数为1的新条目。
  3. 最后,遍历地图并打印作业d和计数

针对您的具体问题。 std::string已执行operator==,因此您可以简单比较相等性,例如

std::string f("foo"); 
std::string b("bar"); 

if (f == b) 
    std::cout << "foobar" << std::endl; 

其他一些提示:

使用流操作在时间读一个字,而不是peek()为EOF,是这样的:

// assume fin is a file input stream 
std::string word; 

while(fin >> word) 
{ 
    if (!word.empty()) 
    { 
    // do stuff with word... 
    } 
} 
+0

好的,谢谢!我确定我可以使用while循环,我将开始学习地图。我们没有在班上讨论地图,但我的教授并不介意我们使用其他东西 –

+1

@BryanSmith,如果要求使用矢量(而不是地图),那么你可以模仿它,但是你需要存储一个简单的结构,它有一个字符串和一个计数(你可以使用'std :: pair')。然后当你添加一个单词时,通过查找矢量来找到这个单词(使用'operator =='),并增加计数,否则在最后插入一个新的条目。 – Nim

1

对于你想要什么实现有更好的方法:std::map。您可以使用地图(就像链接中的示例一样),并且每次要添加新元素时,都会首先搜索它,如果它存在。如果没有,那么你用1

yourMap[yourString]=1; 

初始化如果字符串已经存在,那么你就增加该计数器:

yourMap[yourString]=1+yourMap[yourString]; 
+0

你可以简单地预增量,不需要第二次查找。 (即'++ yourMap [yourString]') - 理论上,你甚至不需要看它是否存在(如果不存在的话,你应该初始化为1)。 – Nim

+0

@尼姆是的。它应该初始化为1.更正 – INS

+0

非常感谢!所有的意见都非常感谢 –