C ++我如何比较用户输入字符串的严格排序的名单? [英] C++ How can I compare a user input to a list of string with strict ordering?

查看:173
本文介绍了C ++我如何比较用户输入字符串的严格排序的名单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建在那里我有字符串列表的搜索数组函数和我拿的用户输入。然后,我比较用户输入,在输入的准确顺序匹配任何字符串数组和输出任何字符串。我不知道从哪里开始。像这样:

I am trying to create a search array function where I have a list of string and I take a user input. I then compare the user input to the strings in the array and output any that matches any string in exact order of the input. I am not sure where to start. like so:

数组:这个,是,一个,测试

array: "this", "is", "a", "test"

用户输入:T

输出应该再是本和测试

这是我迄今为止:

string arr[6] = {"hello", "this", "is", "a", "test", "string"};
vector<string> words;
for (int i=0; i<5; i++) {
    if (a.find(arr[i]) != string::npos) {
        words.push_back(arr[i]);
    }
}

for (int i=0; i<words.size(); i++) {
    cout << words[i] << endl;
}

目前只搜索完全匹配的时刻。我怎么能使其上面描述的场景?

at the moment it only searches for exact matches. How could i make it to the scenario described above?

推荐答案

更改此

if (a.find(arr[i]) != string::npos) 

if (arr[i].find(a) != string::npos) 

您将一个搜索您的用户字符串中的字符串之一清单。

You will search your User string in the List of strings one by one.

这篇关于C ++我如何比较用户输入字符串的严格排序的名单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆