DEV Community

Cover image for Setw() & Setfill() function | C++
Imron
Imron

Posted on

Setw() & Setfill() function | C++

C++ da setw() va setfill()chiroyli formatlashda ishlatiladi.

C++ da oddiy cout bilan ma’lumot chiqarish oson. Lekin natijani chiroyli, tekis va tartibli ko‘rsatish kerak va setw() va setfill() bu funksiyani bajaradi.

Bu funksiyalar <iomanip> kutubxonasiga tegishli.

setw() nima qiladi?

setw() — chiqarilayotgan qiymat uchun kenglik (width) belgilaydi.

Ya’ni, nechta joy egallashini aytamiz.

Misol

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    cout << setw(5) << 10 << endl;

    return 0;
}

Enter fullscreen mode Exit fullscreen mode
   10
Enter fullscreen mode Exit fullscreen mode

setw(5)5 ta joy ajratdi.

10 esa o‘ng tomonga tekislandi.

Oldiga 3 ta probel qo‘shildi.

setw() faqat bitta keyingi qiymat uchun ishlaydi.

Masalan

cout << setw(5) << 10 << 20;

Enter fullscreen mode Exit fullscreen mode

Bu yerda faqat 10 formatlanadi. 20 oddiy chiqadi.

setfill() nima qiladi?

setfill() — bo‘sh joyni qaysi belgi bilan to‘ldirishni belgilaydi.

Default holatda bo‘sh joy probel ( ) bilan to‘ladi.

Misol

cout << setfill('*') << setw(5) << 10;

Enter fullscreen mode Exit fullscreen mode

Natija

***10

Enter fullscreen mode Exit fullscreen mode

5 ta joy ajratildi

Bo‘sh joylar * bilan to‘ldirildi

Ikkalasini bir vohta ishlatish:

cout << setfill('0') << setw(5) << 42;

Enter fullscreen mode Exit fullscreen mode

Natija

00042

Enter fullscreen mode Exit fullscreen mode

Top comments (0)