【发布时间】:2016-02-04 23:30:19
【问题描述】:
我有一个二维字符串数组和一个我想比较的字符串,但 strcmp 不起作用,除非我将字符串转换为 (const char*),这会导致我出现段错误。
目前我的代码简化为:
string dog;
cin >> dog;
string cat[10][10];
//The cat array is then filled with values through cin
//This is the troublesome part
if (strcmp(cat[4][3].c_str(), dog[0]) == 0) {
//do stuff
}
使用 &dog[0] 获取 dog 的地址是可行的,但随后我得到了放入 dog 的整个单词,而不仅仅是我想要的字母。
有什么解决方法吗?
【问题讨论】:
-
这里是some documentation。享受吧。
-
如果只想比较第一个字符,则不需要
strcmp。如果您想查看该单个字符是否在另一个字符串中的某个位置,请使用string::find。