I am very new to C++ programming and you will see why.
我是C ++编程的新手,你会明白为什么。
I want to make a character array consisting of a few words that I want to search with a linear search function. Does this array have to be a two-dimensional array? For example:
我想制作一个由我想用线性搜索功能搜索的几个单词组成的字符数组。这个数组必须是二维数组吗?例如:
char Colors[3][6] = {"red", "green", "blue"};
I tried it like this:
我试过这样的:
char Colors[] = {"red", "green", "blue"};
This gave me a "too many initializers" error.
这给了我一个“太多的初始化程序”错误。
I assume the 1st method is correct because it states the amount of elements in the array and the maximum length of an element, correct?
我假设第一种方法是正确的,因为它说明了数组中元素的数量和元素的最大长度,对吗?
Now how would I implement a linear search function to find a word inside that array? Can I do something like the following:
现在我如何实现线性搜索功能来查找该数组中的单词?我可以做以下事情:
(Assuming the linearSearch function has already been declared)
(假设已经声明了linearSearch函数)
char searchKey;
char element;
char Colors[3][6] = {"red", "green", "blue"};
printf("Enter the color to look for: \n");
scanf("%s", searchKey);
element = linearSearch(Colors, searchKey, ??); //?? is where I don't know what to enter
if (element != -1)
{
printf("Found the word.\n");
}
else
{
printf("Didn't find the word.\n");
}
Is this possible? If so, what would the declaration look for the linearSearch function? I hope I provided enough information for this to be somewhat usable.
这可能吗?如果是这样,声明会为linearSearch函数寻找什么?我希望我提供了足够的信息,以便在某种程度上可用。
edit: Thanks all for the help, got the program working as intended.
编辑:感谢大家的帮助,让程序按预期工作。
6 个解决方案
#1
You could make your linearSearch function to return the index of the search term in the array. Here is a sample program:
您可以使用linearSearch函数返回数组中搜索词的索引。这是一个示例程序:
#include <stdio.h>
#include <string.h>
int linearSearch (const char **Array, const char *searchKey, int arraySize) {
for (int i = 0; i < arraySize; ++i) {
if (strcmp(Array[i], searchKey) == 0)
return i;
}
// We didn't find the searchKey in the Array
return -1;
}
int main () {
char *colors[] = { "red", "green", "blue" };
int index = linearSearch (colors, "green", 3);
if (index < 0) { // search string was not found
printf("Search string not found!\n");
}
else {
printf("String %s found at index %d\n", colors[index], index);
}
return 0;
}
We use the strcmp() function to compare the strings. It returns zero if strings match and nonzero if they don't. To use it, you need to include the string.h
header.
我们使用strcmp()函数来比较字符串。如果字符串匹配则返回零,如果不匹配则返回非零值。要使用它,您需要包含string.h标头。
But, as others have suggested, you should use STL if you can.
但是,正如其他人所建议的那样,如果可以的话,你应该使用STL。
#2
I would recommend to learn about the C++ standard library, which would help you very much. For example,
我建议您了解C ++标准库,它对您非常有帮助。例如,
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
vector<string> words;
words.push_back("red");
words.push_back("blue");
words.push_back("green");
if (find(words.begin(), words.end(), "green") != words.end())
cout << "found green!"
else
cout << "didn't find it";
Why implement linearSearch
yourself? c++ already has std::find
which does it for you! Moreover, if you use a set
instead of a vector
, you can now use std::binary_search
which is O(log n) instead of O(n), since a set is sorted.
为什么要自己实现linearSearch? c ++已经有std :: find哪个为你做了!此外,如果使用集合而不是向量,现在可以使用std :: binary_search,它是O(log n)而不是O(n),因为集合已经排序。
#3
To declare an array of strings, use this syntax
要声明字符串数组,请使用此语法
char *Colors[] = {"red", "green", "blue"};
This is an array of pointers to characters ("Hi" evaluates to a const char* pointing at the 'H'). The compiler will figure out how many elements are needed to store your array (hence the []) in this case it will always be of size 3.
这是一个指向字符的指针数组(“Hi”计算为指向'H'的const char *)。编译器将确定存储数组所需的元素数量(因此[])在这种情况下,它总是大小为3。
Overall, I agree with rlbond's answer - you should use the STL.
总的来说,我同意rlbond的回答 - 你应该使用STL。
#4
No, you don't need a two-dimensional array.
不,你不需要二维数组。
Here is a way to declare string arrays:
这是一种声明字符串数组的方法:
char* Colors[3] = {"red", "green", "blue"};
or
char* Colors[] = {"red", "green", "blue"}; // the compiler will figure out the size
int colors = sizeof(Colors)/sizeof(Colors[0]);
Right after experimenting with C++ you should learn to use STL.
在尝试使用C ++之后,您应该学会使用STL。
#5
This article contains a string search function. It should also give you insight into how to properly structure your character arrays.
本文包含字符串搜索功能。它还应该让您深入了解如何正确构造字符数组。
#6
If you dont want to use strings, and stay with char arrays you can use strcmp to compare 2 words. The thing to remember with strcmp is that it returns the index of where the word was found, so you if you want only to find words in the begining you do it like this:
如果你不想使用字符串,并保留char数组,你可以使用strcmp来比较2个单词。使用strcmp要记住的是它返回找到单词的索引,所以如果你只想在开头找到单词,你可以这样做:
for(int i=0;i<SizeOfColorArray;i++)
{
if(strcmp (MySearchTerm,colors[i]) == 0)
{
// it was a match
return i;
}
}
Depending on what you are doing and how big your array is going to get, you should consider looking into having hashes of the strings to increase preformance.
根据你正在做什么以及你的数组有多大,你应该考虑使用字符串的哈希来提高性能。
#1
You could make your linearSearch function to return the index of the search term in the array. Here is a sample program:
您可以使用linearSearch函数返回数组中搜索词的索引。这是一个示例程序:
#include <stdio.h>
#include <string.h>
int linearSearch (const char **Array, const char *searchKey, int arraySize) {
for (int i = 0; i < arraySize; ++i) {
if (strcmp(Array[i], searchKey) == 0)
return i;
}
// We didn't find the searchKey in the Array
return -1;
}
int main () {
char *colors[] = { "red", "green", "blue" };
int index = linearSearch (colors, "green", 3);
if (index < 0) { // search string was not found
printf("Search string not found!\n");
}
else {
printf("String %s found at index %d\n", colors[index], index);
}
return 0;
}
We use the strcmp() function to compare the strings. It returns zero if strings match and nonzero if they don't. To use it, you need to include the string.h
header.
我们使用strcmp()函数来比较字符串。如果字符串匹配则返回零,如果不匹配则返回非零值。要使用它,您需要包含string.h标头。
But, as others have suggested, you should use STL if you can.
但是,正如其他人所建议的那样,如果可以的话,你应该使用STL。
#2
I would recommend to learn about the C++ standard library, which would help you very much. For example,
我建议您了解C ++标准库,它对您非常有帮助。例如,
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
vector<string> words;
words.push_back("red");
words.push_back("blue");
words.push_back("green");
if (find(words.begin(), words.end(), "green") != words.end())
cout << "found green!"
else
cout << "didn't find it";
Why implement linearSearch
yourself? c++ already has std::find
which does it for you! Moreover, if you use a set
instead of a vector
, you can now use std::binary_search
which is O(log n) instead of O(n), since a set is sorted.
为什么要自己实现linearSearch? c ++已经有std :: find哪个为你做了!此外,如果使用集合而不是向量,现在可以使用std :: binary_search,它是O(log n)而不是O(n),因为集合已经排序。
#3
To declare an array of strings, use this syntax
要声明字符串数组,请使用此语法
char *Colors[] = {"red", "green", "blue"};
This is an array of pointers to characters ("Hi" evaluates to a const char* pointing at the 'H'). The compiler will figure out how many elements are needed to store your array (hence the []) in this case it will always be of size 3.
这是一个指向字符的指针数组(“Hi”计算为指向'H'的const char *)。编译器将确定存储数组所需的元素数量(因此[])在这种情况下,它总是大小为3。
Overall, I agree with rlbond's answer - you should use the STL.
总的来说,我同意rlbond的回答 - 你应该使用STL。
#4
No, you don't need a two-dimensional array.
不,你不需要二维数组。
Here is a way to declare string arrays:
这是一种声明字符串数组的方法:
char* Colors[3] = {"red", "green", "blue"};
or
char* Colors[] = {"red", "green", "blue"}; // the compiler will figure out the size
int colors = sizeof(Colors)/sizeof(Colors[0]);
Right after experimenting with C++ you should learn to use STL.
在尝试使用C ++之后,您应该学会使用STL。
#5
This article contains a string search function. It should also give you insight into how to properly structure your character arrays.
本文包含字符串搜索功能。它还应该让您深入了解如何正确构造字符数组。
#6
If you dont want to use strings, and stay with char arrays you can use strcmp to compare 2 words. The thing to remember with strcmp is that it returns the index of where the word was found, so you if you want only to find words in the begining you do it like this:
如果你不想使用字符串,并保留char数组,你可以使用strcmp来比较2个单词。使用strcmp要记住的是它返回找到单词的索引,所以如果你只想在开头找到单词,你可以这样做:
for(int i=0;i<SizeOfColorArray;i++)
{
if(strcmp (MySearchTerm,colors[i]) == 0)
{
// it was a match
return i;
}
}
Depending on what you are doing and how big your array is going to get, you should consider looking into having hashes of the strings to increase preformance.
根据你正在做什么以及你的数组有多大,你应该考虑使用字符串的哈希来提高性能。