简体   繁体   English

数字和字段宽度的格式化输出,C ++标准对此有何说明?

[英]formatted output of a number and field width, where does the C++ standard say about it?

This code snippet: 此代码段:

//
// This is example code from Chapter 11.2.5 "Fields" of
// "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup
//

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    cout << 123456                        // no field used
         <<'|'<< setw(4) << 123456 << '|' // 123456 doesn't fit in a 4 char field
         << setw(8) << 123456 << '|'      // set field width to 8
         << 123456 << "|\n";              // field sizes don't stick
}

produces this output: 产生以下输出:

123456|123456|  123456|123456|

The second print of 123456 is not truncated to fit in a field with width of 4 and Stroustrup explains that it is the right thing to do because a bad looking table with right numbers is better than a good looking table with wrong numbers. 第二张123456文字不会被截断以适合宽度为4的字段,Stroustrup解释说这是正确的做法,因为数字正确的不良表比数字错误的良好表更好。

where does the C++ standard say about this behaviour? C ++标准对此行为怎么说?

I found ios_base::width where the standard says: 我发现ios_base::width标准说:

The minimum field width (number of characters) to generate on certain output conversions 在某些输出转换中生成的最小字段宽度(字符数)

Is "minimum" the keyword here to explain the said behaviour? 这里的关键字“ minimum”是用来解释所说行为的吗?

The statement you cite is a generic description. 您引用的语句是一般性描述。 Regardless of what is being output, the field will have at least that many characters; 不管输出什么,该字段至少将具有那么多字符; that is the meaning of minimum . 那就是最小的意思。 The exact meaning of the field depends on the type of data being output. 该字段的确切含义取决于输出的数据类型。 In the case of integer output, the exact format is specified in §22.4.2.2; 对于整数输出,确切的格式在§22.4.2.2中指定; this includes not only how the width field is interpreted, and a guarantee that the field will not be larger unless necessary to display the value according to the format specified, but also what character to use for the fill, and where to put it. 这不仅包括如何解释width字段,并保证该字段不会变大,除非有必要根据指定的格式显示该值,而且还包括用于填充的字符以及将其放在何处。 (Stroustrup's example leaves all of the other parameters with their default values, but if you have a negative number, and specified a fill character of '0', you wouldn't want it to result in |000-1234| , but rather |-0001234| .) (Stroustrup的示例将所有其他参数|000-1234|为其默认值,但是如果您使用负数并将其填充字符指定为'0',则不希望其导致|000-1234| ,而是|-0001234| 。)

For user defined types, it's entirely possible that the field contain less than the minimum. 对于用户定义的类型,该字段包含的内容可能少于最小值。 I would consider this a bug, but I imagine a lot of user defined << are written without consideration of any of the formatting parameters. 我会认为这是一个错误,但我想写了许多用户定义的<<而不考虑任何格式化参数。 The actual effect of std::setw is only to set a field in the std::basic_ios<char> class; std::setw的实际效果只是在std::basic_ios<char>类中设置一个字段; it's up to the implementation of << to handle it correctly. <<的实现来正确处理它。

暂无
暂无

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

相关问题 C++ 标准对堆栈溢出有何规定? - What does the C++ standard say about stack overflow? 在C ++标准中它说的是什么:: delete可以改变左值? - Where in the C++ Standard does it say ::delete can change lvalues? C ++标准在哪里说,C在哪里说相同:编译单元(.cpp文件)中的变量按声明顺序初始化 - Where does the C++ standard say, and does C say the same: variables in a compilation unit (.cpp file) are initialised in order of declaration C ++语言标准对static_cast如何处理减小整数大小有何看法? - What does the C++ language standard say about how static_cast handles reducing the size of an integer? 关于移动活动对象存储,C ++标准怎么说? - What does C++ standard say about moving live object storage? 关于使用noexcept覆盖throw()函数,C ++标准有何说法? - What does the C++ standard say about overriding a throw() function with noexcept? C ++标准对于在目标类型范围之外的类型的转换结果的结果有何评价? - What does the C++ standard say about results of casting value of a type that lies outside the range of the target type? 在C ++标准中它表示必须初始化const内置类型变量的定义? - Where in the C++ Standard does it say that the definition of a const built-in type variable must be initialized? 在C ++标准中,它表示sizeof(wchar_t)<= sizeof(long)和sizeof(bool)<= sizeof(long)? - Where in the C++ Standard does it say that sizeof(wchar_t) <= sizeof(long) and sizeof(bool) <= sizeof(long)? C++ 标准究竟在哪里说取消引用未初始化的指针是未定义的行为? - Where exactly does C++ standard say dereferencing an uninitialized pointer is undefined behavior?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM