简体   繁体   English

C ++等效于C格式“%3d”?

[英]What is the C++ equivalent to C formatting “%3d”?

My code is: 我的代码是:

#include<iostream>
using namespace std;
int main()
{
    int count=0;
    int total=0;

    while(count<=10)
    {   
        total=total+count;   
        cout<<"count"<<"="<<count/*<<','*/<<'\t'<<'\t'<<"total"<<"="<<total<<endl;
        count++;
    }
}

All of the facilities of the C standard library are still available in C++. C标准库的所有功能仍然可以在C ++中使用。 You can write your program like this: 您可以这样编写程序:

#include <cstdio>
using std::printf;

int main()
{
  int count = 0;
  int total = 0;

  while (count<=10)
  {   
    total = total + count;
    printf("count=%3d, total=%3d\n", count, total); 
    count++;
  }
}

It is my personal opinion that the stdio.h output interfaces are nearly always easier to use and produce more readable code than the iostream output interfaces, particularly for formatted output of numbers (as in this case), so I would not hesitate to do so. 我个人认为,与iostream输出接口相比, stdio.h输出接口几乎总是更易于使用,并且产生更具可读性的代码,尤其是对于格式化的数字输出(在这种情况下),因此我会毫不犹豫地这样做。 。 The major advantage of iostream is that it can be extended to format objects via operator<< overloads, but a program like this does not need that. iostream的主要优点是可以通过operator<<重载将其扩展为格式化对象 ,但是像这样的程序不需要这样做。

Note that neither the stdio.h nor the iostream input interfaces are fit for purpose, due to egregious, standard-codified bugs like defining numeric input overflow to trigger undefined behavior (I am not making this up!) 请注意,由于存在严重的,标准编码的错误,例如定义数字输入溢出以触发未定义的行为,因此stdio.hiostream 输入接口都不适合此目的(我没有做这个!)

Using iostream formatting, you need to include the iomanip header and use the setw and setfill , like this: 使用iostream格式,您需要包括iomanip标头,并使用setwsetfill ,如下所示:

#include <iostream>
#include <iomanip>

int main() {
    using namespace std;

    int count=0;
    int total=0;

    while(count<=10)
    {   
        total=total+count;   
        cout<<"count"<<"="<<count<<'\t'<<'\t'<<"total"<<"="<<setfill(' ')<<setw(3)<<total<<endl;
        count++;
    }
}

You can use setw and left from iomanip to achive the effect you want. 您可以使用setwiomanip中的 left来达到所需的效果。

#include <iostream>
#include <iomanip>

int main()
{
    int count=0;
    int total=0;

    while(count<=10)
    {
        total=total+count;
        std::cout << "count = " << std::setw(15) << std::right << count << "total = " << total << std::endl;
        count++;
    }
}

setw set the with of the next "cout impression" (15 in this case) and left just set the aligment to the left. setw设置下一个“ cout印象”(在这种情况下为15)的with,而将left对齐设置为左。

Note: As suggest by @Zack you can write << '\\n' at the end instead << endl . 注意:根据@Zack的建议,您可以在末尾写<< '\\n'而不是<< endl Since << endl is exactly the same as writing << '\\n' << flush and the flush isn't necessary in this case. 由于<< endl与编写<< '\\n' << flush完全相同,因此在这种情况下不需要使用flush。

In C++ IO formatting is being done either in same way as in C (since all C functionalities are in place also in C++) or with std::setw std::setprecission and other C++ manipulators presented in header . 在C ++中,IO格式化的方式与在C中相同(因为所有C功能也都在C ++中存在),或者使用std::setw std::setprecission和标头中提供的其他C ++ 操纵器进行。

So either of this will be fine: 所以这两个都可以:

#include <cstdio>

int main()
{
    int count=0;
    int total=0;

    while( count <= 10)
    {
        total += count;
        printf( "count = %3d, total = %3d\n", count++, total);
    }
}

or 要么

#include <iostream>
#include <iomanip>

int main()
{
    int count=0;
    int total=0;

    while( count <= 10)
    {
        total += count;
        std::cout << "count = " << std::setw(15) << 
                        count++ << "total = " << total << std::endl;
    }
    return 0;
}

You can also do custom formatting by imbue to apply custom facet to locale ( ie extend std::numpunct). 您也可以自定义格式被imbue到应用定制方面,以区域设置 (即延长的std :: numpunct)。

http://ideone.com/nHfTL6 http://ideone.com/nHfTL6

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

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