2011-12-12 128 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

非常感謝!所有的意見都非常感謝 –