可能重复:
逻辑运算符的书面版本.
我注意到,c + +定义关键字and,or,not,xor,and_eq,or_eq,not_eq和xor_eq作为替代&&,||,!,^,&=,|=,!=和|=.他们很少使用!怎么了?它们不便携吗?
这是我用QtCreator创建的一个非常简单的C++应用程序:
int main(int argc, char *argv[])
{
int a = 1;
int b = 2;
if (a < 1 or b > 3)
{
return 1;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
对我来说,这不是有效的C++,因为关键字或不是保留关键字.
但是如果我编译并运行它,它没有任何警告就可以正常工作!退出代码为0,如果我改变b = 4,则退出代码为1!
我没有包括任何东西,以确保没有隐藏的定义.
这对我来说真的很奇怪.这是Qt定义的东西吗?我在文档中没有找到任何相关内容.
使用"和"运算符与&&运算符有什么优缺点?我个人认为"和"只会引起混淆(具有讽刺意味).
如果没有任何差异,为什么会存在?这看似愚蠢和不必要.
该程序应该存储标准输入流中给出的每个单词并计算它们的出现次数.结果应该按顺序打印,然后按顺序打印.据我所知,该程序正在工作,但字符串打印为字符的ASCII值而不是字符本身.怎么了?
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cctype>
#include <algorithm>
std::string get_word();
int main()
{
std::vector<std::string> words;
std::string word;
while (std::cin.good()) {
word = get_word();
if (word.size() > 0)
words.push_back(word);
}
std::sort(words.begin(), words.end());
unsigned n, i = 0;
while (i < words.size()) {
word = words[i];
n = 1;
while (++i < words.size() and words[i] == word)
++n;
std::cout << word << ' ' << n << std::endl;
}
}
std::string get_word()
{
while (std::cin.good() and !std::isalpha(std::cin.peek())) …Run Code Online (Sandbox Code Playgroud) 首先让我说这在Visual Studio中编译并运行良好.但是当我在Linux(g ++)上编译相同的文件时,我得到编译错误,用于声明和实现<<运算符的重载.
代码的相关部分在下面提取.(它是一个带有Google测试用例的.cpp文件,并且通过了类和方法定义来支持测试用例.)我除了代码的相关部分之外的所有部分(我希望).
class orderrequest : public msg_adapter {
public:
// ... snip
friend bool operator ==(const orderrequest &or1, const orderrequest &or2);
friend ostream& operator <<(ostream &out, const orderrequest &or); // compiler error here
};
bool operator ==(const orderrequest &or1, const orderrequest &or2) {
bool result = or1.symbol == or2.symbol
&& or1.orderQty == or2.orderQty;
// ... snip
return result;
}
// compiler error here
ostream& operator <<(ostream &out, const orderrequest &or) {
out << "symbol=" << or.symbol …Run Code Online (Sandbox Code Playgroud) 给定C++函数foo:
bool foo();
Run Code Online (Sandbox Code Playgroud)
以及以下代码行
bool some_bool = false;
some_bool = some_bool and foo();
Run Code Online (Sandbox Code Playgroud)
我观察到foo()虽然它可能有副作用,但没有被调用.这种行为的名称是什么,它是否依赖于编译器?
我目前正在学习一个非常基础的 C++ 课程,我们正在编写一个程序来检查输入是否是字符串或其中是否包含数字。我目前有我的代码,一切似乎都按顺序进行,但是,当我尝试测试它时,出现错误:
行长度应小于或等于 80 个字符。
错误位置揭示了这个问题:
if ((checknum == 1 && checkVector[0] == 1)|| checknum == 0 || checknum == strlength) {
Run Code Online (Sandbox Code Playgroud)
有没有办法将其分成多行以避免此错误?如果没有所有空格,它不会让我运行它。
我已经看到and 关键字if像&&运算符一样在语句内部使用。这些(and,&&)有什么区别?
#include <iostream>
using namespace std;
int main()
{
bool a = true;
bool b = true;
if(a==true and b==true) // if(a==true && b == true)
{
cout << "YES ";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在将现有项目迁移到 VS2019。具有在所有先前版本中成功编译的非常简单的结构:
template <class Marker>
struct not
{
// ...
Run Code Online (Sandbox Code Playgroud)
错误 C2332:“结构”:缺少标记名称
NOTE-1我已经检查过not没有用一些宏替换的预编译生成,*.i文件的输出完全匹配上面的声明
NOTE-2当然,如果我重命名not->inv那么一切都是正确的
我试图创建一个XOR运算符,但由于未知原因,我的编译器不接受bool xor()该函数,它也不允许我调用它或以任何可能的方式使用它。
我想指出的是,我正在学习一本书来学习C ++。具体地说,它是Herbert Schildt(第3版)的“从头开始学习C ++”。本书中引用了这段代码。
如果我将函数命名为bool xar()或bool XOR(),我的代码就可以正常工作,但是由于我正在尝试学习C ++,所以我想对为什么会发生此错误有所了解。
#include <iostream>
using namespace std;
bool xor(bool a, bool b);
int main()
{
bool q, p;
cout << "Enter Q (0 or 1): ";
cin >> q;
cout << "Enter P (0 or 1): ";
cin >> p;
cout << "Q AND P: " << (q && p) << '\n';
cout << "Q OR P: " << (q || p) << '\n';
cout << …Run Code Online (Sandbox Code Playgroud)