在 C++ 中返回 true 或 false
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22082536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Returning true or false in c++
提问by Bob Shannon
When I run a method of type bool in c++ with a return statement like so:
当我在 c++ 中使用类似这样的 return 语句运行 bool 类型的方法时:
bool method() {
return true;
}
there is no output at the console. To get output, I have to do:
控制台没有输出。要获得输出,我必须这样做:
bool method() {
cout << "true";
return true;
}
Is this the right approach?
这是正确的方法吗?
回答by Vlad from Moscow
This program is compiled and executed successfully and outputs 1 that is the value of true.
该程序编译执行成功,输出值为true的1。
#include <iostream>
bool method() {
return true;
}
int main()
{
std::cout << method() << std::endl;
}
If you want that instead of 1 there will be literal true you can write
如果你想要那个而不是 1 你可以写字面真
#include <iostream>
#include <iomanip>
bool method() {
return true;
}
int main()
{
std::cout << std::boolalpha << method() << std::endl;
}
The problem maybe is also that your program after executing closes the window and you have no time to see the result. You should insert some input statement in the end of the program that it would wait until you enter something.
问题可能还在于您的程序在执行后关闭了窗口,而您没有时间看到结果。您应该在程序末尾插入一些输入语句,它会等到您输入某些内容。
回答by Manu343726
C++ is not an interpreted language, like python, its a compiled language. So you don't write the function call on a interpreter and it prints the result.
You are compiling your program and executing it later. So if you need to output something to the console in your program, you have to write an instruction to do that ( like std::cout <<
does ).
C++ 不像 Python 那样是一种解释型语言,它是一种编译型语言。所以你不要在解释器上写函数调用,它会打印结果。您正在编译程序并稍后执行。因此,如果您需要向程序中的控制台输出某些内容,则必须编写一条指令来执行此操作(就像 do 一样std::cout <<
)。
回答by Bartek Banachewicz
It is usually a good idea to separate the logicof your program from the input/outputpart of it. That way, you can change the logic without needing to change the display and vice versa.
将程序的逻辑与其输入/输出部分分开通常是个好主意。这样,您无需更改显示即可更改逻辑,反之亦然。
An example of that might be (I've made it a bit less trivial):
一个例子可能是(我已经让它变得不那么微不足道了):
int operation(int a, int b) {
return a + b;
}
void process() {
int a, b;
std::cin >> a >> b;
std::cout << operation(a, b);
}
That should be followed even in languages that directly print the output of the executed function (but it's oftentimes not for sake of "simplicity" in example programs). It makes a massive difference when designing larger systems.
即使在直接打印执行函数的输出的语言中也应该遵循这一点(但在示例程序中通常不是为了“简单”)。在设计更大的系统时,它会产生巨大的差异。
You can find out more about it by googling "Model-View-Controller" or simply "separating logic from IO".
您可以通过谷歌搜索“模型-视图-控制器”或简单地“将逻辑与 IO 分离”来了解更多信息。
To get to your particular example, you made a function that's distinctively "logic", and that's a good thing. You could add the printing statement inside, but typically it's better, again, to separate the concerns.
为了获得您的特定示例,您创建了一个独特的“逻辑”函数,这是一件好事。您可以在其中添加打印语句,但通常最好再次将关注点分开。