检查数组中是否存在字符串
Check if a string exists in array
对于学校作业,我需要检查用户输入的字符串是否存储在预定义的单词阵列中。
我想实现一个函数来执行检查,可能看起来像这样:
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::string
和std::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::set
和std::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
- 将文件复制到自定义位置,存在字符串转换问题
- 我遇到了这个代码片段,不明白. 它递归检查 C++ 字符串中是否存在大写字符
- 有没有办法将字符串添加到 Vector 中,但前提是它尚不存在?->C++
- C++ 查找字符串中存在的元素向量
- 与C++中的空字符串相比,突变仍然存在?
- 将字符串转换为整数类型T,检查是否存在溢出
- 如何检查二进制文件中是否存在字符串
- 是否存在像C++中那样带有分隔符的C#原始字符串
- 在向量中存储字符串存在 SEG 错误
- 给定字符串和单词 S 的列表,检查列表中是否存在 S
- 除了字符串或宏调用之外,是否存在任何情况,其中"(<"是有效的字符流?
- 查找字符串中存在的不同字符
- C++实现代码中的字符串不应存在于输出二进制文件中.如何解决
- 控制一个字符串是否存在于另一个字符串中(以 C++ 为单位)
- 阵列不能两次将相同的名称保存,当要添加新字符串时,程序应检查以查看该名称是否已经存在
- C 验证字符串2中是否存在字符串1
- 什么是最快检查字符串数组中是否存在字符串的方法
- 如何知道基于项目的ListView QT C 中是否存在字符串
- 检查数组中是否存在字符串
- 如果存在字符串,则设置bool值