单词比较 - 向量/getopt
Word comparison - Vectors /Getopt
我对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
的情况下,我认为这不是您的问题,我将回答以下问题:给定一个带有单词列表的文件和一个单词,您如何确定该单词是否存在于文件中。
方法可以做到这一点,但从概念上讲,它涉及以下步骤:
- 读取输入文件并从中创建字典。
- 匹配字典中的输入单词。
代码片段:
#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;
}
相关文章:
- 写入向量<向量<bool>>
- 函数向量_指针有不同的原型,我可以构建一个吗
- std::向量与传递值的动态数组
- 将值指定给向量(2D)的向量中的某个位置
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 如何使用向量的template_back函数
- 尝试通过多个向量访问变量时,向量下标超出范围
- 如何通过派生类函数更改基类中的向量
- C++从另一个类访问公共静态向量的正确方法是什么
- 如何将ampl中的集合表示为c++中的向量
- 变量没有改变?通过向量的函数调用
- 迭代时从向量和内存中删除对象
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 计算排序向量的向量中唯一值的计数
- 矩阵向量乘法(cublasDgemv)返回零
- 一对向量构造函数:初始值设定项列表与显式构造
- 将结构向量排序为子组
- 在C++中调整向量中的索引
- 单词比较 - 向量/getopt