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::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