比较向量的元素

comparing elements of a vector

本文关键字:元素 向量 比较      更新时间:2023-10-16

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

以下是一些提示:

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

关于你的具体问题。std::string已经实现了operator==,所以你可以简单地比较是否相等,例如

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

其他提示:

使用流操作一次读取一个单词,而不是用于EOF的peek(),例如:

// assume fin is a file input stream
std::string word;
while(fin >> word)
{
  if (!word.empty())
  {
    // do stuff with word...
  }
}

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

yourMap[yourString]=1;

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

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