比较矢量的元素

时间:2011-12-12 10:23:51

标签: c++ vector comparison

我是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;
}

2 个答案:

答案 0 :(得分:2)

以下是一些提示:

  1. 而不是vector,请考虑使用map - 这样您就可以计数与给定的单词相关联
  2. 插入单词时,查看地图是否包含单词,如果包含,则增加计数,否则插入计数为1的新条目。
  3. 最后,遍历地图并打印单词和计数
  4. 关于你的具体问题。 std::stringoperator==已实施,因此您只需比较相等性,例如

    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...
      }
    }
    

答案 1 :(得分:1)

对于你想要实现的目标,有一种更好的方法:std::map。您使用地图(就像链接中的示例一样),并且每次要添加新元素时,首先搜索它是否存在。如果没有,则用1初始化它。

yourMap[yourString]=1;

如果字符串已经存在,那么你递增该计数器:

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