单词比较 - 向量/getopt

Word comparison - Vectors /Getopt

本文关键字:getopt 向量 比较 单词      更新时间:2023-10-16

我对getopt的理解非常有限。但是,我确实意识到argv[0]是exe文件,argv[1]是选项,argv[2]是要比较的单词,argv[3]是我要搜索的字典或文档(文件.txt)。

我正在尝试设置指向字典的指针,然后遍历它以查看是否与文本文件的argv[2](输入单词)匹配,如果有匹配输出argv[2]单词。以下是我当前有错误的代码:

main.cpp:61: error: no match for 'operator==' in 'list == *(argv + 12u)'
main.cpp:64: error: no match for 'operator*' in '*list'

任何帮助将不胜感激。

#include <cstdlib>
#include <unistd.h>
#include <vector>
#include <iostream>
#include <string>
#include <iterator>
using namespace std; 
int main(int argc, char** argv) {
    enum {
        WHOLE, PREFIX, SUFFIX, ANYWHERE, EMBEDDED
    } mode = WHOLE;
    bool jumble = false;
    bool ignore_case = false;
    bool invert = false;
    string length = "0,0";
    int c;
    string input;
    vector <string> list;
    vector <string>::iterator i;
    while ((c = getopt(argc, argv, ":wpsaejivn:")) != -1) {
        switch (c) {
            case 'w': mode = WHOLE;
                break;
            case 'p': mode = PREFIX;
                break;
            case 's': mode = SUFFIX;
                break;
            case 'a': mode = ANYWHERE;
                break;
            case 'e': mode = EMBEDDED;
                break;
            case 'j': jumble = true;
                break;
            case 'i': ignore_case = true;
                break;
            case 'v': invert = true;
                break;
            case 'n': length = optarg;
                break;
            default: WHOLE;
                break;
        }
    }
    argc -= optind;
    argv += optind;
    switch (mode) {
        case WHOLE:
            while(argc != -1){
                list == argv[3];
                for(i == list.begin(); i != list.end(); i++)
                if(argv[1] == argv[3]){
                    cout << *list << endl;
                }else cout << "Did not work again" << endl;
            }                                  
    }
    return 0;
}

如果我理解正确,这里不需要向量。你需要读取文件 argv[3],逐字解析它,当你找到一个等于 argv[2] 的单词时停止。

我想你想要这样的东西:

#include <string>
#include <fstream>
#include <ostream>
using namespace std;
int main(int argc, char** argv)
{    
  // The part where you parse the input and validate it
  // ...
  // Read the dictionary specified in argv[3] and compare it with argv[2] line by line
  ifstream input_file(argv[3]);
  string match_string(argv[2]);
  string current_string;
  bool found = false;
  while(getline(input_file, current_string) && !found)
      found = (current_string.compare(match_string) == 0);
  // Check the result
  if (found)
     cout << "Found " << match_string << " in " << argv[3] << endl;
  else
     cout << "Did not work again" << endl;
  return 0;
}

在这个基本解决方案中,我假设字典文件中的每个单词都在单独的行中。当然,您需要根据需要对其进行修改,并根据需要添加更多输入验证。

希望对您有所帮助!

在不进入getopt的情况下,我认为这不是您的问题,我将回答以下问题:给定一个带有单词列表的文件和一个单词,您如何确定该单词是否存在于文件中。

可以有很多

方法可以做到这一点,但从概念上讲,它涉及以下步骤:

  1. 读取输入文件并从中创建字典。
  2. 匹配字典中的输入单词。

代码片段:

#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <set>
int main (int argc, char *argv[])
{
  // Input file stream
  std::ifstream file_in(argv[3]);
  // Iterators to iterate over input file
  std::istream_iterator<std::string> in_begin(file_in), in_end;
  // Create a dictionary of words.
  // Using std::set for this.
  std::set<std::string> dictionary(in_begin, in_end);
  // Word to find in dictionary
  std::string word(argv[2]);
  // See if the word is present in our dictionary
  if(dictionary.find(word) != dictionary.end())
    std::cout << word << " found in " << argv[3] << std::endl;
  else
    std::cout << word << " not found in " << argv[3] << std::endl;
}