C + + flow operator

Source: Internet
Author: User

C language used to know the accuracy of C-format control is very simple and easy, just learn C + + may not know how to use these features in C + +. Here are two ways to use these features.

Variable use:

    int64;    double123.45;    double0.0183;

I. Using flow manipulation operators
First you have to include the header file
1. Width control

    cout‘#‘ << endl;    //宽度控制不会影响下一个变量    cout << setw(10‘#‘ << endl;//默认右对齐    cout‘#‘ << endl;

2. Alignment control

    //对齐控制会影响下面的变量对其方式    cout << setw(10‘#‘ << endl;//左对齐    cout << setw(10‘#‘ << endl;    cout << setw(10‘#‘ << endl;//还原成右对齐    //cout << setw(10) << resetiosflags(ios::left) << n << ‘#‘ << endl << endl;//还原成右对齐

3. Fill control

    //填充控制会影响下面的输出    cout << setw(10) << setfill(‘@‘‘#‘ << endl;    cout << setw(10‘#‘ << endl;    cout << setw(10) << setfill(‘ ‘‘#‘//还原成空格填充

4. Precision Control

    //精度控制会影响下面的输出    cout << setprecision(4//控制有效数字的位数    cout << setprecision(2) << d2 << endl;//四舍五入,而不是简单的截断    cout << d << endl;    cout << setiosflags(ios::fixed);      //控制小数点后面的位数    cout << setprecision(4//同样是四舍五入    cout << setprecision(2) << d2 << endl;

5. Control of the input system

    cout<<"Dec:"<< n << Endl;cout<<"Oct:"&LT;&LT;OCT << n << Endl;cout<<"Hex:"<< hex << n << endl;cout<< n << Endl;cout<< setiosflags (ios::showbase);//Display the symbol in front of the corresponding system    cout<<"Dec:"<< Dec <<n << Endl;cout<<"Oct:"<< Oct << n << Endl;cout<<"Hex:"<< hex << n << endl;cout<< SetBase (8) << n << Endl;//Binary output    cout<< SetBase (Ten) << n << Endl;cout<< SetBase ( -) << n << Endl;

Two. In the way of stream member functions (without Iomanip header file)
1. Width control

    cout << n << endl;    cout.width(10);    cout‘#‘ << endl;

2. Alignment control

    cout << n << endl;    cout.width(10);    cout.setf(ios::left);    cout‘#‘ << endl;//  cout.width(10);//  cout.setf(ios::right);//  cout << n << ‘#‘ << endl;    cout.width(10);    cout.unsetf(ios::left);    cout‘#‘ << endl;

3. Fill control

    cout << n <<‘#‘<< endl;    cout.width(10);    cout.fill(‘@‘);    cout‘#‘ << endl;    cout.width(10);    cout‘#‘ << endl;    cout.width(10);    cout.fill(‘ ‘);    cout‘#‘ << endl;

4. Precision Control

    cout.precision(2);   //这个会影响下面的输出    cout << d << endl;    cout << d2 << endl;    cout.setf(ios::fixed);  //控制小数点位数    cout << d << endl;    cout << d2 << endl;

5. Control of the input system

    cout//要切换进制的时候需要先去掉原先的进制    cout.setf(ios::oct);    cout << n << endl;

The output is 100.

C + + flow operator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.