为什么使用is_it_valid_color("yellow")
工作和输出FOUND IT
但使用is_it_valid_color(x.c_str());
不起作用?
我感觉它与以空字符结尾的字符串有关。输出在控制台中看起来相同:
color: 'yellow'
FOUND IT
color: 'yellow'
.
const char *color_names[] = {"yellow", "green", "red"};
const int color_names_SIZE = 3;
void is_it_valid_color(const char* color) {
cout << "color: '" << color << "'" << endl;
for(int i = 0; i < color_names_SIZE; ++i) {
if(color == *(color_names + i)) {
cout << "FOUND IT" << endl;
break;
}
}
}
is_it_valid_color("yellow");
string x = "yellow";
is_it_valid_color(x.c_str());