Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 检查数组中是否存在字符串_C++ - Fatal编程技术网

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

C++ 检查数组中是否存在字符串,c++,C++,对于学校作业,我需要检查用户输入的字符串是否存储在预定义的单词数组中 我想实现一个函数来执行检查,它可能如下所示: bool exists(dict words, char check) { /* how to implement this? */ } 但我不知道这是否有效,也不知道如何实施。有人能帮忙吗 这是我的密码: #include <iostream> #include <string> using namespace std; struct dict {

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

我想实现一个函数来执行检查,它可能如下所示:

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 << "found\n";
    }
    else {
        cout << "not found\n";
    }
}
#包括
#包括
使用名称空间std;
结构指令{
字符串字;
};
int main(){
听写单词[5];
单词[0]。单词='abc';
单词[1]。单词='bcd';
单词[2]。单词='cde';
单词[3]。单词='def';
单词[4]。单词='efg';
字符用户_输入[100];
cin.getline(用户输入,100);
如果(…){//如何检查用户输入是否在我的word数组中?

cout首先,
dict
是一种结构,而
char
是一种能够容纳单个字符的类型,因此您需要:

bool存在(常量dict*单词、常量字符串和检查);

从这一点上,我要说:

  • const dict*
    应更改为
    const vector&
  • std::getline
    能够,因此不需要普通字符数组
但由于这是一项学校作业,我想,你有一些局限性(既不能使用
std::vector,也不能使用这项功能)

输出:

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

.

正如另一个答案已经指出的,您应该在函数签名中添加一个size参数,以便能够迭代数组(特别是知道何时停止迭代)。然后,一个简单的循环和一个比较就可以了

注意,通常不需要在C++中使用原始数组,而应该是标准库中的一个容器,例如,“代码> STD::vector < /代码>。此外,您应该使用<代码> STD::String 和<代码> STD::GETLION()/<代码>供您的用户输入,并且您应该修复字符串文字(使用双引号)…不要用单引号“…”)。此外,你应该避免不简洁地使用命名空间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 << "found\n";
    else
        std::cout << "not found\n";
}

以下内容可能超出了您学校作业的范围,但这可能会对未来的访问者有帮助

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

< C++ >标准库还提供了容器类型<代码> STD::SET//COD>和 STD::unOrdEdEdTys(后者是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 << "found\n";
        else
            std::cout << "not found\n";
    }
}
供参考:


…和字符串文本…以及如何询问(堆栈溢出)这可能是学校的作业,但如果没有更好的数据结构来存储字符串,例如普通或。那么也很容易找到特定的“单词”使用。现在你可以使用,但它有点复杂。是的,这是我学校作业的一部分。你可能还想阅读,因为这样你就不必担心纯
char
数组和大小。
#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 << "found\n";
    else
        std::cout << "not found\n";
}
$ g++ test.cc && echo "abc" | ./a.out
found
#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 << "found\n";
        else
            std::cout << "not found\n";
    }
}
$ g++ test.cc -std=c++11
$ echo "abc" | ./a.out
input: 'abc' found
$ echo "abcdefg" | ./a.out
input: 'abcdefg' not found