如何在 C++ 中为单个案例接受多个值?我知道您可以使用case 1..2其他一些语言为一个案例(例如)设置一系列值,但它似乎不适用于 Xcode 上的 C++。
int main() {
int input;
cin >> input;
switch (input) {
case 1:
cout << "option 1 \n";
break;
case 2..3: //This is where the error occurs
cout << "option 2 and 3 \n";
break;
default:
break;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该程序显示错误,指出“浮动常量上的后缀无效'.3'”范围所在。
我应该如何在 C++ 中查找字符数组的长度?我已经尝试了两种方法,但它们都导致数组中的字符数错误。到目前为止,我已经使用了strlen和sizeof运营商,但无济于事。
void countOccurences(char *str, string word)
{
char *p;
string t = "true";
string f = "false";
vector<string> a;
p = strtok(str, " ");
while (p != NULL)
{
a.push_back(p);
p = strtok(NULL, " ");
}
int c = 0;
for (int i = 0; i < a.size(); i++)
{
if (word == a[i])
{
c++;
}
}
int length = sizeof(str); //This is where I'm having the problem
string result;
cout << length …Run Code Online (Sandbox Code Playgroud)