Manipulators, C ++, in what order should they be used?

I'm trying to find out about manipulators ... is there a specific order for them?

For ex std::setwfollows after or before std::setfilland should they be on separate lines?

+4
source share
2 answers

There is no specific order, just make sure you turn on the library <iomanip>.

An example of your setw / setfil question:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    cout  << setw(10) << setfill('*');
    cout << 123;
}
+2
source

There is no specific order. But pay attention to this, for example, if you want to use std :: left and std :: right or write everything in one line, then everything can get a little confused.

For example, this will not print the expected result (prints only:) 7 :

std::cout << std::setw(10) << std::left << 7 << std::setfill('x') << std::endl;

, , . , , (: xxxxxxxxx7):

std::cout << std::setw(10) << std::setfill('x') << std::right << 7 << std::endl;
std::cout << std::right << std::setw(10) << std::setfill('x') << 7 << std::endl;
std::cout << std::setfill('x') << std::right << std::setw(10) << 7 << std::endl;

- .

#include <iostream>
#include <iomanip>

int main()
{
    std::cout << std::setw(15) << std::setfill('-') << "PRODUCT" << std::setw(15) << std::setfill('-') << "AMOUNT" << std::endl;
    std::cout << std::setw(15) << std::setfill('-') << "Brush"  << std::setw(15) << std::setfill('-') << 10 << std::endl;
    std::cout << std::setw(15) << std::setfill('-') << "Paste"  << std::setw(15) << std::setfill('-') << 8 << std::endl << std::endl;

    std::cout << std::setw(15) << std::left << std::setfill('-') << "PRODUCT" << std::setw(15) << std::left << std::setfill('-') << "AMOUNT" << std::endl;
    std::cout << std::setw(15) << std::left << std::setfill('-') << "Brush"  << std::setw(15) << std::left << std::setfill('-') << 10 << std::endl;
    std::cout << std::setw(15) << std::left << std::setfill('-') << "Paste"  << std::setw(15) << std::left << std::setfill('-') << 8 << std::endl << std::endl;

    std::cout << std::setw(15) << std::right << std::setfill('-') << "PRODUCT" << std::setw(15) << std::right << std::setfill('-') << "AMOUNT" << std::endl;
    std::cout << std::setw(15) << std::right << std::setfill('-') << "Brush"  << std::setw(15) << std::right << std::setfill('-') << 10 << std::endl;
    std::cout << std::setw(15) << std::right << std::setfill('-') << "Paste"  << std::setw(15) << std::right << std::setfill('-') << 8 << std::endl << std::endl;

    return 0;
}
+1

Source: https://habr.com/ru/post/1695436/


All Articles