Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++等于C格式“%3D”的内容是什么?_C++ - Fatal编程技术网

C++等于C格式“%3D”的内容是什么?

C++等于C格式“%3D”的内容是什么?,c++,C++,我的代码是: #include<iostream> using namespace std; int main() { int count=0; int total=0; while(count<=10) { total=total+count; cout<<"count"<<"="<<count/*<<','*/<<'\t'<<

我的代码是:

#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++;
    }
}

< C++标准库的所有设施在C++中仍然可用。您可以这样编写程序:

#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++;
  }
}
#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++;
    }
}

我个人认为,stdio.h输出接口几乎总是比iostream输出接口更易于使用,产生的代码也更可读,特别是对于本例中的数字格式化输出,因此我会毫不犹豫地这样做。iostream的主要优点是,它可以通过操作符扩展到使用iostream格式化来格式化对象,您需要包括iomanip头并使用and,如下所示:

#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++;
  }
}
#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++;
    }
}
您可以使用setw和left from来获得想要的效果

#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设置下一个cout impression 15的带,然后向左,只需将对齐设置到左侧即可


<> >注释:正如@ ZAK所建议的,C++ C++ IO格式中的格式与C一样,因为C++中的所有C函数也都在C++中,或者STD::SEtW商店STD::StEngReScript和其他C++在标头中显示。p> 所以这两个都可以:

#include <cstdio>

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

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

您还可以通过imbue将自定义方面应用于,即扩展std::numpunct,来进行自定义格式设置


你想干什么?你期望的输出是什么?使用I/O操纵器,例如在C中,我们使用%3D来做C++中使用的3个空间,而不是你想要1个出来为“1”和12,作为“12”填充。什么不清楚他的问题?不是我的下投票,而是……在一个层次上正确的时候,这不是很有帮助,因为OP明确地询问使用C++格式化。@ JONATANEL LIFFER这是一个公平的批评,我很高兴这个答案会丢失声誉游戏,而实际上是使用IOFFROW,但我认为,如果不想使用C++,就不必使用IOFFROTS是有价值的。@ ZACK,与其说你想使用IoSoW;如果可以避免的话,没有一个正常人会使用printf。建议你为什么使用setw15而不是setw3?为什么离开?为什么使用名称空间std;全球地?这行代码应该是cout@nwp,我发布的代码是一个关于如何实现某些格式的示例。如果您愿意,可以建议使用setw3,因为它在这里没有任何相关性。关于左边,你试过没有它的代码了吗?我不这么认为。最好不要把一个完整的名称空间std带到scope中。我不想贬低你的答案。我没有回答,因为我的答案基本上和你的一样。我还以为你打错了一些部件,会解决的。我还想知道15是从哪里来的,因为另一个答案也使用setw15。关于左侧:默认值是right,其行为与%3d相同,因此您有意使cout的行为与所讨论的格式不同。我不明白你为什么这么做,所以我问。老兄,你抄了我的答案!!!你的代码甚至没有使用你建议的setPrecision函数。@RaydelMiranda你的意思是和的内容吗?setfill的作用是什么?据我所知,“”是默认的填充符,因此它不起任何作用。setfill定义了如果输入字符串长度小于setw指定的值,将使用哪个字符。使用setfill明确表示您希望该字符作为填充符是一种很好的做法,因为它依赖于编译器,并且它会更改流的状态,而不仅仅是下一个输入setw-这意味着如果您之前有一个像setfill“0”这样的调用,您需要重置它。