[英]C++ 2d array search error
I'm trying to search a 2d array for a string. 我正在尝试在二维数组中搜索字符串。
The 2d array contains names and birthdays. 2d数组包含名称和生日。
I want to search a name and display their birthday. 我想搜索一个名字并显示他们的生日。
When I try to use strcmp to see if the inputted name compares to any in the array I get this error: 当我尝试使用strcmp查看输入的名称是否与数组中的名称进行比较时,出现此错误:
IntelliSense: no suitable conversion function from "std::string" to "const char *" exists
Here is the bit of code I have: 这是我的一些代码:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
char name[100];
string firstnames[2][4]= { {"John", "Emily", "Juan", "Sally"},
{"Nov 6", "Jan 13", "Oct 10", "Mar 29"} };
cout << "Enter a name: \n";
cin >> name;
if (strcmp(name, firstnames[0][0]) == 0)
{
}
}
I don't understand how to fix this error? 我不知道如何解决此错误? I had another similar error, but it went away when I changed the name to a char array instead of a string.
我还有另一个类似的错误,但是当我将名称更改为char数组而不是字符串时,它消失了。 So, I think it has something to do with that, but I don't know what to do to compare the inputted name to the array to find a match.
所以,我认为这与此有关,但是我不知道该怎么做才能将输入的名称与数组进行比较以找到匹配项。
strcmp
function takes 2 parameters of type const char*
. strcmp
函数采用const char*
类型的2个参数。 While const char*
is implicitly convertible to std::string
, it is not the other way around. 虽然
const char*
可隐式转换为std::string
,但并非相反。
You can get a pointer to internal char
array by c_str
member function: 您可以通过
c_str
成员函数获取指向内部char
数组的指针:
strcmp(name, firstnames[0][0].c_str());
or you can make a string
from name
and use operator==
或者您可以根据
name
创建一个string
并使用operator==
if (std::string(name) == firstnames[0][0]) // ...
By the way, this: 顺便说一句,这:
char name[100];
cin >> name;
is pretty vulnerable to buffer overflows. 很容易受到缓冲区溢出的影响。 What if the user enters more than 100 characters?
如果用户输入超过100个字符怎么办? I'd suggest you use
std::string
from the start. 我建议您从一开始就使用
std::string
。
Since you're already using the string class, why not just make name
a string object 由于您已经在使用字符串类,所以为什么不直接
name
一个字符串对象
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
string courses[2][4]= { {"John", "Emily", "Juan", "Sally"},
{"Nov 6", "Jan 13", "Oct 10", "Mar 29"} };
cout << "Enter a course name: \n";
cin >> name;
if (name == courses[0][0])
{
cout << "Done!" << endl;
}
}
The problem is that you have defined your courses as type "string" but are accessing them like a "char*" (or in your case "char[]" which is pretty much the same thing). 问题是您已经将课程定义为“字符串”类型,但是却像“ char *”(或者在您的情况下为“ char []”这几乎是同一回事)一样访问它们。 All you would need to do is change this line:
您需要做的就是更改此行:
if (strcmp(name, courses[0][0]) == 0)
to this: 对此:
if (strcmp(name, courses[0][0].c_str()) == 0)
Note the "c_str()". 注意“ c_str()”。 This simply returns a char* pointer of your string which can be used in strcmp().
这只是返回字符串的char *指针,该指针可在strcmp()中使用。
As an aside : is there any particular reason you have gone from using std::string to char*? 顺便说一句 :从使用std :: string到char *有什么特殊的原因吗? The reason I ask is because char* is more of a C-style way to program.
我问的原因是因为char *更像是一种C风格的编程方式。 The std:string type was designed for C++ and contains built in comparison operators for other strings (and I believe even char*s).
std:string类型是为C ++设计的,并且包含用于其他字符串的内置比较运算符(我相信甚至char * s)。 Generally when programming in C++, it's best to use C++ style types whenever possible as there are a lot of advantages (RAII memory managements, standard algorithms, utility methods, etc...).
通常,在使用C ++进行编程时,最好使用C ++样式类型,因为它有很多优点(RAII内存管理,标准算法,实用程序方法等)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.