简体   繁体   English

为什么这个 C++ 程序给出了错误的输出?

[英]why this C++ program gives incorrect output?

Consider following program:考虑以下程序:

#include <iostream>
int main()
{
    std::cout<<std::ios::showbase<<123<<", "<<std::hex<<123<<", "<<std::oct<<123<<'\n';
}

Expected Output: 123, 0x7b, 0173预期输出:123、0x7b、0173

Acquired Output: 512123, 7b, 173 (see live demo here: http://ideone.com/Khzj5j )获得的输出:512123、7b、173(在此处查看现场演示: http ://ideone.com/Khzj5j)

But If I modify the above program slightly as following:但是如果我稍微修改上面的程序如下:

#include <iostream>
using namespace std;
int main()
{
    cout<<showbase<<123<<", "<<hex<<123<<", "<<oct<<123<<'\n';
}

Now I got desired output.现在我得到了想要的输出。 (see live demo here http://ideone.com/gcuHbm ). (在此处查看现场演示http://ideone.com/gcuHbm )。

Why first program gave incorrect output but 2nd program doesn't?为什么第一个程序给出了错误的输出,而第二个程序却没有? What's going wrong in first program?第一个程序出了什么问题?

std::ios::showbase is a format flag. std::ios::showbase是格式标志。 std::showbase is a function that takes a std::ios_base and calls ios_base::setf(std::ios::showbase) on it to set the showbase flag. std::showbase是一个函数,它采用std::ios_base并在其上调用ios_base::setf(std::ios::showbase)以设置showbase标志。

You use the prior in your first example and the latter in your second example.您在第一个示例中使用先验,在第二个示例中使用后者。

#include <iostream>
int main()
{
    std::cout<<std::ios::showbase<<123<<", "<<std::hex<<123<<", "<<std::oct<<123<<'\n';
}

This uses std::ios::showbase ( http://www.cplusplus.com/reference/ios/ios_base/fmtflags/ )这使用std::ios::showbase ( http://www.cplusplus.com/reference/ios/ios_base/fmtflags/ )

While your other program而你的其他程序

#include <iostream>
using namespace std;
int main()
{
    cout<<showbase<<123<<", "<<hex<<123<<", "<<oct<<123<<'\n';
}

Uses std::showbase ( http://en.cppreference.com/w/cpp/io/manip/showbase ) which is why you're getting different results.使用std::showbase ( http://en.cppreference.com/w/cpp/io/manip/showbase ) 这就是你得到不同结果的原因。

Changing the first program to use std::showbase gives you your expected output:更改第一个程序以使用std::showbase为您提供预期的输出:

#include <iostream>
int main()
{
    std::cout<<std::showbase<<123<<", "<<std::hex<<123<<", "<<std::oct<<123<<'\n';
}

http://ideone.com/OodBvo http://ideone.com/OodBvo

std::ios::showbase is a format flag that has some implementation defined value. std::ios::showbase是一个格式标志,具有一些实现定义的值。 When you call std::cout << std::ios::showbase you are displaying that value and not setting the stream format flag.当您调用std::cout << std::ios::showbase您正在显示该值而不是设置流格式标志。

In you second example you are using std::showbase which sets the format flag of the stream.在第二个示例中,您使用std::showbase设置流的格式标志。

In std::ios_base , showbase is a fmtflag that has an implementation-defined value.std::ios_baseshowbase是具有实现定义值的fmtflag In this case, it appears to be 512 .在这种情况下,它似乎是512 On the other hand, there is a stream manipulator (aka, a function) also called showbase , which calls setf(std::ios_base::showbase) .另一方面,还有一个流操纵器(又名函数)也称为showbase ,它调用setf(std::ios_base::showbase) This is defined as a free function in namespace std whereas the fmtflag is a member of std::ios_base .这被定义为namespace std的自由函数,而fmtflagstd::ios_base的成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM