检查数组中是否存在字符串

Check if a string exists in array

本文关键字:存在 字符串 是否 数组 检查      更新时间:2023-10-16

对于学校作业,我需要检查用户输入的字符串是否存储在预定义的单词阵列中。

我想实现一个函数来执行检查,可能看起来像这样:

bool exists(dict words, char check) { /* how to implement this? */ }

,但我不知道这会起作用还是如何实施它。谁能帮忙?

这是我的代码:

#include <iostream>
#include <string>
using namespace std;
struct dict {
    string word;
};
int main() {
    dict words[5];
    words[0].word = 'abc';
    words[1].word = 'bcd';
    words[2].word = 'cde';
    words[3].word = 'def';
    words[4].word = 'efg';
    char user_input[100];
    cin.getline(user_input, 100);
    if (...) { // how do I check if the user input is in my word array?
        cout << "foundn";
    }
    else {
        cout << "not foundn";
    }
}

首先,dict是一个结构,char类型可以保持单个字符,因此您宁愿拥有:

bool exists(const dict* words, const string& check);

从这一点开始,我会说:

  • const dict*应该更改为const vector<dict>&
  • std::getline能够直接读取输入到字符串中,因此不需要普通的char数组。

,由于我想这是一项学校任务,因此您有一些局限性(并且不能使用std::vector也不能使用STD ::查找,这将可以完成这项工作)。所以:

bool exists(const dict* words, size_t count, const std::string& check)
{
    for(size_t n = 0; words && (n < count); ++n)
    {
        if(words[n].word == check)
            return true;
    }
    return false;
}

示例:

dict langs[3];
langs[0].word = "C++";
langs[1].word = "Java";
langs[2].word = "Python";
std::string s_1 = "Java";
std::string s_2 = "C++ 11";
printf("exists(%s) : %sn", s_1.c_str(), exists(langs, 3, s_1) ? "yes" : "no");
printf("exists(%s) : %sn", s_2.c_str(), exists(langs, 3, s_2) ? "yes" : "no");

输出:

exists(Java) : yes
exists(C++ 11) : no

链接到示例代码。

正如其他答案已经指出的那样,您应该在功能签名中添加大小参数,以便能够迭代数组(尤其是知道何时停止迭代。)。然后,一个简单的循环进行比较将解决问题。

请注意,通常不需要在C 中使用原始数组,而是标准库中的一个容器之一,例如std::vector。另外,您应该将std::stringstd::getline()用于用户输入,并且应该修复字符串文字(使用双引号" ..."而不是单引号'...')。此外,您应该避免using namespace std;无意识。查看本文结尾处的链接,以便在这些点上进行进一步阅读。

示例代码:

#include <iostream>
#include <string>
#include <vector>
bool exists(std::string const & user_input, 
            std::vector<std::string> const & words)
{
    for (int i = 0; i < words.size(); i++)
        if (user_input == words[i])        
            return true;
    return false;
}
int main() {
    std::vector<std::string> words(5);
    words[0] = "abc";
    words[1] = "bcd";
    words[2] = "cde";
    words[3] = "def";
    words[4] = "efg";
    std::string user_input;
    std::getline(std::cin, user_input);
    if (exists(user_input, words))
        std::cout << "foundn";
    else
        std::cout << "not foundn";
}

示例输出:

$ g++ test.cc && echo "abc" | ./a.out
found

以下可能超出了您学校任务的范围,但这可能对以后的访问者对此问题有所帮助。

请注意,数组(std::vector是)不是执行此类任务的最有效的数据结构,因为您必须迭代整个数组以检查每个项目(线性复杂性)。

C 标准库还提供了容器类型std::setstd::unordered_set(后者以来C 11)。在这里,搜索空间以特殊的方式组织(二进制搜索树:对数复杂性,哈希表:平均恒定复杂性),以改善关键类型的查找时间(在这种情况下为std::string)。

这是一个示例:

#include <iostream>
#include <string>
#include <set>
typedef std::set<std::string> set_type;
bool input_exists(std::string input, set_type const & words) {
    return words.find(input) != words.end();
}
int main() {
    set_type words = {"abc", "bcd", "cde", "def", "efg"};
    std::string input;
    if (std::getline(std::cin, input)) {
        std::cout << "input: '" << input << "' ";
        if (input_exists(input, words))
            std::cout << "foundn";
        else
            std::cout << "not foundn";
    }
}

示例输出:

$ g++ test.cc -std=c++11
$ echo "abc" | ./a.out
input: 'abc' found
$ echo "abcdefg" | ./a.out
input: 'abcdefg' not found

供参考:

  • http://en.cppreference.com/w/cpp/container/vector
  • http://en.cppreference.com/w/cpp/string/basic_string
  • http://en.cppreference.com/w/cpp/string/basic_string/getline
  • http://en.cppreference.com/w/cpp/language/string_literal
  • 为什么使用命名空间std'被认为是不良习惯吗?
  • http://en.wikipedia.org/wiki/binary_search_tree
  • http://en.wikipedia.org/wiki/hash_table
  • http://en.cppreference.com/w/cpp/container/set
  • http://en.cppreference.com/w/cpp/container/set/find
  • http://en.cppreference.com/w/cpp/container/unordered_set_set
  • http://en.cppreference.com/w/cpp/container/unordered_set/find
  • http://en.wikipedia.org/wiki/computational_complexity_theory