我遇到了一个奇怪的问题。出于某种原因多个||语句,即使用逗号和括号划分也不行。我期望的最后一件事是&声明,它要求满足这两个条件,但在我的情况下,它适用于一个条件,就好像它是一个OR语句。C++为什么if语句不适用于多个条件? 〜除了用&语句
有人请向我解释为什么会发生这种情况。我很困扰。
WORKS:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
string quest;
quest = "Where is my dog?";
string::iterator m;
vector<string>question;
string t;
for(m = quest.begin(); m != quest.end(); m++)
{
if(*m != ' ' & *m != ',' & *m != '?' & *m != '.') //works with & and &&
{
t.push_back(*m);
}
else
{
cout << t << endl;
question.push_back(t);
t.clear();
}
}
}
不起作用:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
string quest;
quest = "Where is my dog?";
string::iterator m;
vector<string>question;
string t;
for(m = quest.begin(); m != quest.end(); m++)
{
if(*m != ' ' || *m != ',' || *m != '?' || *m != '.') // DOES NOT WORK
{
t.push_back(*m);
}
else
{
cout << t << endl;
question.push_back(t);
t.clear();
}
}
}
你有什么期望第二个输出的,什么是它的输出? – Dani
答复者,请说明两者1)发生了什么事,2)如何正确地做。我不会投一半答案。 –
我希望人们不要投我一票,因为我不明白,即使这是非常愚蠢的事情。 – VSB